import Layer from 'ol/layer/Vector'
import Source from 'ol/source/Vector'
import Select from 'ol/interaction/Select.js';
import {singleClick} from 'ol/events/condition'
import * as olEvents from 'ol/events';
import config from "@/api/config.js"
import * as KMap from '@/utils/ol-map/KMap';
import Style from "ol/style/Style";
import Photo from "ol-ext/style/Photo";
import {base_img_url2} from "../../api/config";
import Stroke from "ol/style/Stroke";
import * as util from "@/common/ol_common.js"
import {newAreaFeature, newAreaPoint, newPolymerFeature, newPoint} from "../zhgl/map";

/**
 * @description 智能巡园地图层对象
 */
class AllotNsMap {

    constructor(){
        let that = this
        this.vectorStyle = new KMap.VectorStyle()
        let pointSimpleStyle = this.vectorStyle.getPointSimpleStyle(8,"#999999","#d5d5d0","1")
        this.areaLayer = new KMap.VectorLayer("areaLayer",999,{style:(f)=> that.vectorStyle.getPolygonStyle("#ffffff10", "#ffffff",3)})
        this.areaPointLayer =  new KMap.VectorLayer("areaPointLayer",1002,{style:(f)=> that.vectorStyle.getPointTextStyle(f.get("name"), "#120046","#FFFFFF",2, 30)})
        this.treeLayer = new KMap.VectorLayer("treeLayer",1001,{style:pointSimpleStyle})
        this.treeFeatures = []
    }

    initMap(location, target)  {
        let level = 18
        let coordinate = util.wktCastGeom(location).getFirstCoordinate()
        this.kmap = new KMap.Map(target,level,coordinate[0], coordinate[1],null,10,22);
        let xyz = config.base_img_url + 'map/lby/{z}/{x}/{y}.png';
        this.kmap.addXYZLayer(xyz,{minZoom:15,maxZoom:22});
        let xyz2 = config.base_img_url3 + 'map/lby/{z}/{x}/{y}.png';
        this.kmap.addXYZLayer(xyz2,{minZoom:12,maxZoom:22});
        this.kmap.addLayer(this.areaLayer.layer)
        this.kmap.addLayer(this.treeLayer.layer)
        this.kmap.addLayer(this.areaPointLayer.layer)
    }

    initArea(organId, callback){
        let that = this
        this.areaLayer.refresh()
        VE_API.area.list({organId}).then(({data,code})=>{
            for(let item of data){
                that.areaLayer.addFeature(newAreaFeature(item))
                that.areaPointLayer.addFeature(newAreaPoint(item))
            }
            callback && callback()
        })
    }


    initTree(organId, callback){
        let that = this
        this.treeLayer.refresh()
        that.treeFeatures = []
        VE_API.tree.treeList({organId,areaId:-1}).then(({data,code})=>{
            for(let item of data){
                let point = newPoint(item);
                that.treeFeatures.push(point)
                that.treeLayer.addFeature(point)
            }
            callback && callback()
        })
    }

    addToggleTreeSelect(listener){
        let that = this
        let selectStyle = this.vectorStyle.getPointSimpleStyle(8,"#cc3f2e","#e18b7c","2")
        this.treeLayer.addToggleSelect(function({deselected,selected, preventDefault}){
            if(selected.length > 0){
                listener && listener(selected[0],deselected[0])
            }
            preventDefault()
        },this.kmap.map, selectStyle)
        that.treeLayer.toggleSelect.setActive(false)
    }

    addToggleAreaSelect(listener){
        let that = this
        this.areaLayer.addToggleSelect(function({deselected,selected,preventDefault}){
            if(selected.length > 0){
                that.treeLayer.source.forEachFeature((f)=>{
                    if(f.get("areaId") == selected[0].get("id")){
                        that.treeLayer.toggleSelect.getFeatures().remove(f)
                    }
                })
                that.treeLayer.source.forEachFeature((f)=>{
                    if(f.get("areaId") == selected[0].get("id")){
                        that.treeLayer.toggleSelect.getFeatures().push(f)
                    }
                })
            }
            if(deselected.length > 0){
                that.treeLayer.source.forEachFeature((f)=>{
                    if(f.get("areaId") == deselected[0].get("id")){
                        that.treeLayer.toggleSelect.getFeatures().remove(f)
                    }
                })
            }
            listener && listener(selected[0],deselected[0])
            preventDefault()
        },this.kmap.map)
        that.areaLayer.toggleSelect.setActive(false)

    }

    setTreeStyle(style){
        this.treeLayer.layer.setStyle(style)
        this.treeLayer.layer.changed()
    }
    fit(geomOrExtent){
        this.kmap.fit(geomOrExtent)
    }
}

export default AllotNsMap
