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 WKT from "ol/format/WKT";
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 Text from "ol/style/Text";
import Fill from "ol/style/Fill";
import Icon from "ol/style/Icon";
import Feature from "ol/Feature";
import {getDateText} from "../../utils/util"
import {transformFromGCJToWGS,pointWktToLocation,toPointWkt} from "../../utils/WSCoordinate"

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

    constructor(){
        let that = this
        this.vectorStyle = new KMap.VectorStyle()
        this.areaLayer = new KMap.VectorLayer("areaLayer",999,{style:(f)=> that.vectorStyle.getPolygonStyle("#ffffff10", "#ffffff",3)})
        this.workerLayer = new KMap.VectorLayer("workerLayer",999,{style:(f)=> {
                let timeAndLength = f.get("execTime")+"分钟" +"  "+ f.get("pathLength")+"m";
                let timeAndLengthObj = new Text({
                    offsetY:-120,
                    font:"20px sans-serif",
                    text:timeAndLength,
                    stroke: new Stroke({
                        color: "#00FFF0",
                        width: 1,
                    }),
                    fill: new Fill({
                        color: "#00FFF0"
                    }),
                })
                let timeAndLengthStyle = new Style({
                    text:timeAndLengthObj
                })
                let textObj = new Text({
                    font:"20px sans-serif",
                    text:f.get("name"),
                    stroke: new Stroke({
                        color: "#00FFF0",
                        width: 1,
                    }),
                    fill: new Fill({
                        color: "#00FFF0",
                    }),
                })
                let textStyle = new Style({
                    text:textObj
                })
                let style = new Style({
                    image: new Icon({
                        src:config.base_img_url3+"birdseye-look-vue/worker_path/worker_point.png",
                        scale:1,
                        anchor:[0.5,1],
                    })
                });
                return [timeAndLengthStyle, textStyle,style]
        }})
        this.pathLayer = new KMap.VectorLayer("pathLayer",999,{style:this.vectorStyle.getLineStyle("#4cb7fa", "#4cb7fa",2)})
    }
    initWorker(list){
        let that = this
        this.workerLayer.refresh()
        this.pathLayer.refresh()
        that.workerLayer.layer.setVisible(true)
        for(let item of list){
            if(item.wkt == ""){
                continue
            }
            let temp = pointWktToLocation(item.wkt)
            temp = transformFromGCJToWGS(temp)
            item.wkt = toPointWkt(temp)
            that.workerLayer.addFeature(newPoint(item))
        }
    }

    //加载执行人轨迹
    loadExecutorPath(allotExecutorId){
        let that = this
        that.pathLayer.refresh()
        this.pathLayer.layer.setVisible(true)
        VE_API.ns_record_allot_executor_path.points({allotExecutorId}).then(({data})=>{
            let i = 0;
            let wkt = "LINESTRING("
            for(let item of data.list){
                let {longitude,latitude} = that.pointWktToLocation(item.wkt)
                item.wkt = toPointWkt({longitude,latitude})
                let feature = newPoint(item)
                that.pathLayer.addFeature(feature)
                if(i == 0){
                    let text = "起"+getDateText(feature.get("execTime"))
                    feature.setStyle(that.vectorStyle.getPointTextStyle(text,"#FFFFFF","#3f9edb",3,16))
                }else if(i == data.list.length - 1){
                    let text = "终"+ getDateText(feature.get("execTime"))
                    feature.setStyle(that.vectorStyle.getPointTextStyle(text,"#FFFFFF","#3f9edb",3,16))
                }else{
                    let text = getDateText(feature.get("execTime"))
                    feature.setStyle(that.vectorStyle.getPointTextStyle(text,"#FFFFFF","#3f9edb",3,16))
                }
                wkt+=(i == 0 ? "" : ",") + longitude + " " + latitude
                i++
            }
            wkt+=")"
            let lineFeature = new Feature({
                geometry: new WKT().readGeometry(wkt)
            });
            that.pathLayer.addFeature(lineFeature)
            that.fit(that.pathLayer.source.getExtent())
            that.workerLayer.layer.setVisible(false)
        })
    }

    initClick(listener){
        let that = this
        this.kmap.map.on("click",function (event){
            let clickFeature = that.kmap.map.forEachFeatureAtPixel(event.pixel, function(feature){
                // 为点击到的feature发送自定义的click消息
                if(feature.dispatchEvent != undefined){
                    listener && listener(feature)
                }
                return feature
            })
        })
    }

    initMap(location, target)  {
        let level = 18
        let coordinate = util.wktCastGeom(location).getFirstCoordinate()
        this.kmap = new KMap.Map(target,level,coordinate[0], coordinate[1],null,5,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.workerLayer.layer)
        this.kmap.addLayer(this.pathLayer.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))
            }
            callback && callback()
        })
    }

    fit(geomOrExtent){
        this.kmap.fit(geomOrExtent,[10,10,10,10])
    }

    pointWktToLocation(wkt){
        let start = wkt.indexOf("(")
        let end = wkt.indexOf(")")
        let arr = wkt.substring(start + 1,end).trim().split(" ")
        return transformFromGCJToWGS({latitude: parseFloat(arr[1]), longitude: parseFloat(arr[0])})
    }
}

export default WorkerMap
