import VectorSource from 'ol/source/Vector.js';
import WKT from 'ol/format/WKT.js';
import Feature from 'ol/Feature.js';
import store from '@/store'
import VectorLayer from 'ol/layer/Vector.js';
import Draw from "ol/interaction/Draw";
import Text from "ol/style/Text";
import Icon from "ol/style/Icon";
import {Circle, Fill, Stroke, Style} from 'ol/style.js';

/*
 * @Author: your name
 * @Date: 2021-01-12 09:38:09
 * @LastEditTime: 2022-01-20 10:37:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-element-admin\src\utils\map.js
 */
/**
 * 过滤不应该出现在属性列表的字段
 * @param data
 * @param key
 * @returns {boolean}
 */
function filterWktProp(data,key){
    if(key == "regionWkt" || key == "wkt" || key == "pointWkt" || key == "geom" || !data[key]){
        return false
    }
    return true
}

export const newAreaFeature = (data)=>{
    let geom = new WKT().readGeometry(data["wkt"])
    let feature = new Feature({
        geometry: geom
    });
    feature.set("nodeType","area");
    feature.setId(data.id)
    for(let key in data){
        if(filterWktProp(data,key)){
            feature.set(key,data[key])
        }
    }
    return feature;
}

export const newAreaPoint = (data)=>{
    let point = new WKT().readGeometry(data["pointWkt"])
    let feature = new Feature({
        geometry: point
    });
    feature.set("nodeType","area");
    feature.set("isPoint",1)
    for(let key in data){
        if(filterWktProp(data,key)){
            feature.set(key,data[key])
        }
    }
    return feature;
}

export const newPolymerFeature = (data)=>{
    let geom = new WKT().readGeometry(data["geom"])
    let feature = new Feature({
        geometry: geom
    });
    feature.set("nodeType","polymer");
    feature.setId(data.id)
    for(let key in data){
        if(filterWktProp(data,key)){
            feature.set(key,data[key])
        }
    }
    return feature;
}

export const newRegionFeature = (data)=>{
    let geom = new WKT().readGeometry(data["regionWkt"])
    let feature = new Feature({
        geometry: geom
    });
    feature.set("nodeType","region");
    feature.setId(data.id)
    for(let key in data){
        if(filterWktProp(data,key)){
            feature.set(key,data[key])
        }
    }
    return feature;
}


export const newPoint = (data)=>{
    let point = new WKT().readGeometry(data["wkt"])
    let feature = new Feature({
        geometry: point
    });
    feature.setId(data.id)
    feature.set("nodeType","tree")
    for(let key in data){
        if(filterWktProp(data,key)){
            feature.set(key,data[key])
        }
    }
    return feature;
}

/**
 * 按treeId分组
 * @param data
 * @returns {[]}
 */
export const groupByTreeId = (data) => {
    let res = []
    let cur = {treeId:-1,data:[]}
    for(let item of data){
        if(cur.treeId != item.treeId){
            cur = {treeId: item.treeId, data:[]}
            res.push(cur)
        }
        cur.data.push(item)
    }
    for(let item of res){
        item.data.sort((a,b) => {
            return Date.parse(b.uploadDate) - Date.parse(a.uploadDate)
        })
    }
    return res
}

/**
 * 按createDate分组
 * @param data
 * @returns {[]}
 */
export const groupByCreateDate = (data) => {
    let res = {}
    let arr = []
    for(let item of data){
        if(res[item.uploadDate]){
            res[item.uploadDate].push(item)
        }else{
            res[item.uploadDate] = [item]
            arr.push({uploadDate:item.uploadDate, data: res[item.uploadDate]})
        }
    }
    arr.sort((a,b) => {
        return Date.parse(b.uploadDate) - Date.parse(a.uploadDate)
    })
    return arr
}


export const setPeriodAttr = (periodMap, data) => {
    for(let item of data){
        item["attrs"] = []
        let periodObj = periodMap[item.periodId]
        item["periodName"] = periodObj.name
        for(let i=0;i<periodObj.attrFields.length;i++){
            item["attrs"].push({name:periodObj.attrNames[i],field:periodObj.attrFields[i]})
        }
    }
}


export const bboxToFeature = ( x1,  y1,  x2,  y2) => {
    let wkt = "POLYGON (("+x1+" "+y1+", "+x1+" "+y2+", "+x2+" "+y2+", "+x2+" "+y1+", "+x1+" "+y1+"))";
    let feature = new Feature({
        geometry: new WKT().readGeometry(wkt)
    });
    return feature
}
/**
 * 红框样式
 * @returns {Style}
 */
export const redBoxStyle = ()=>{
    const fill = new Fill({
        color: 'rgba(255,255,255,0.1)',
    });
    const stroke = new Stroke({
        color: '#f5024f',
        width: 0.5,
    });
    let  style = new Style({
        fill: fill,
        stroke: stroke,
    })
    return style
}


