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";
import { GARDEN_STATUS_ENUM as STATUS_ENUM } from "@/api/enum.js"
import Icon from "ol/style/Icon";

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

    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("#00000010", "#000000",3)})
        this.subAreaLayer = new KMap.VectorLayer("subAreaLayer",1000,{style:(f)=> {
                return that.vectorStyle.getPolygonStyle(f.get("fillColor"), f.get("strokeColor"), 2)
        }})
        this.afterDayNum = 3
        this.areaPointLayer =  new KMap.VectorLayer("areaPointLayer",1002,{style:(f)=> that.vectorStyle.getPointTextStyle(f.get("name"), "#120046","#FFFFFF",2, 30)})
        this.treeCacheStyle = {}
        this.treeStyle= (f)=> {
            let key = f.get("imgSrc");
            if(that.treeCacheStyle[key]){
                return that.treeCacheStyle[key]
            }
            let style = new Style({
                image: new Icon({
                    src: f.get("imgSrc"),
                    scale:1,
                    anchor:[0.5,1],
                })
            });
            that.treeCacheStyle[key] = style
            return style
        }
        this.treeLayer = new KMap.VectorLayer("statusPointLayer",999,{style:that.treeStyle})
        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.subAreaLayer.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()
        })
    }

    initSubArea(organId, callback){
        let that = this
        this.subAreaLayer.refresh()
        VE_API.sub_area.list({organId}).then(({data,code})=>{
            for(let item of data){
                item.fillColor = that.handleStatusColor(item.status).fillColor
                item.strokeColor = that.handleStatusColor(item.status).strokeColor
                that.subAreaLayer.addFeature(newPolymerFeature(item))
            }
            callback && callback()
        })
    }


    initTree(organId, callback){
        let that = this
        this.treeLayer.refresh()
        that.treeFeatures = []
        VE_API.sub_area.treeList({gardenId: organId}).then(({data}) => {
            for(let item of data){
                let result = that.handleStatusIcon(item)
                if (result.hasStatus) {
                    item.imgSrc = result.imgSrc
                    let point = newPoint(item);
                    that.treeLayer.addFeature(point)
                    that.treeFeatures.push(point)
                }
            }
            callback && callback()
        })
    }

    // 状态图例
    handleStatusIcon(obj) {
        let key = this.afterDayNum == 3 ? "ycStatus" : this.afterDayNum == 5 ? "ycStatus5d" : "ycStatus7d"
        let hasStatus = false
        let imgSrc = null
        if (obj[key] == 1) {
            hasStatus = true
            imgSrc = require('@/assets/status/status_bcyc.png')
            return {hasStatus, imgSrc}
        }
        if (obj[key] == 2) {
            hasStatus = true
            imgSrc = require('@/assets/status/status_szyc.png')
            return {hasStatus, imgSrc}
        }
        if (obj.review) {
            hasStatus = true
            imgSrc = require('@/assets/status/status_dfh.png')
            return {hasStatus, imgSrc}
        }
        if (obj.fly) {
            hasStatus = true
            imgSrc = require('@/assets/status/status_xfdw.png')
            return {hasStatus, imgSrc}
        }
        return {hasStatus, imgSrc}
    }

    // 区级颜色
    handleStatusColor(key) {
        let fillColor = '#ffffff00'
        let strokeColor = '#ffffff'
        switch (key) {
            case STATUS_ENUM.DONE:
                fillColor = 'rgba(95,255,197,0)'
                strokeColor = '#ffffff'
                break;
            case STATUS_ENUM.ABNORMAL_GROWTH:
                fillColor = 'rgba(255,189,5,0.2)'
                strokeColor = '#FFE44E'
                break;
            case STATUS_ENUM.ABNORMAL_Pest:
                fillColor = 'rgba(51,11,3,0.52)'
                strokeColor = '#F55A5A'
                break;
            case STATUS_ENUM.PENDING_REVIEW:
                fillColor = 'rgba(3,40,51,0.2)'
                strokeColor = '#032833'
                break;
            case STATUS_ENUM.REVIEWED:
                fillColor = 'rgba(95,255,197,0.1)'
                strokeColor = '#5FFFC5'
                break;
            default:
                break;
        }
        return {fillColor, strokeColor}
    }

    addToggleTreeSelect(listener){
        let that = this
        this.treeLayer.addToggleSelect(function({deselected,selected, preventDefault}){
            if(selected.length > 0){
                listener && listener(selected[0],deselected[0])
            }
            preventDefault()
        },this.kmap.map, that.treeStyle)
    }

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

    refreshTree(organId,day,callback){
        this.afterDayNum = day
        this.initTree(organId, callback)

    }
    fit(geomOrExtent){
        this.kmap.fit(geomOrExtent)
    }
}

export default EditNsMap
