import { wktToGeoJSON,geojsonToWKT  } from "@terraformer/wkt"
import * as turf from '@turf/turf'
import * as mars3d from 'mars3d';
import { EventBus } from '@/utils/eventBus';

// wkt 转 geojson
const WktToGeojson = (wktData)=> {
    return wktToGeoJSON(wktData);
}

// geojson 转 wkt
const GeojsonToWkt = (geojsonData)=> {
    return geojsonToWKT(geojsonData)
}

// 获取数据中心点
const GetGeometryCenter = (geometry) => {
    let geojson = null;

    // wkt
    if(typeof geometry ==  'string'){ 
        geojson = wktToGeoJSON(geometry);
    }

    let coordinates = null;
    let centerPointGeojson = null;

    switch(geojson?.type){

        case "Point":
            coordinates = geojson.coordinates;
            break;

        case "Polygon":
            let polygon = turf.polygon(geojson.coordinates);
            centerPointGeojson = turf.centerOfMass(polygon);
            coordinates = centerPointGeojson.geometry.coordinates;
            break;

        case "MultiPolygon":
            let multiPolygon = turf.multiPolygon(geojson.coordinates);
            centerPointGeojson = turf.centerOfMass(multiPolygon);
            coordinates = centerPointGeojson.geometry.coordinates;
            break;
    }
    return coordinates;
}





let drawingGraphicLayer = new mars3d.layer.GeoJsonLayer();

// 编辑图斑
const EditDrawingGeometry = (map,geometry) => {
  
    // 获取geojson
    let geojson = null;

    if(typeof geometry == 'string'){
        let jsonData = WktToGeojson(geometry)

        geojson = {
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "geometry": jsonData
                }
            ]
        }
    }
    console.log("geojson",geojson);

    // 加载图层
    drawingGraphicLayer = new mars3d.layer.GeoJsonLayer({
        name: "编辑图层",
        data:geojson,
        isAutoEditing:true,
        symbol: {
            styleOptions: {
                color: "#0d3685",
                outlineColor: "#0d3685",
                opacity: 0.8,
                clampToGround:true,
                "color": "#2f00b0",
                "pixelSize": 10,
                "opacity": 1,
                "outline": true,
                "outlineColor": "#ffffff",
                "outlineOpacity": 0.6,
                "outlineWidth": 2,
                "scaleByDistance_far": 1000000,
                "scaleByDistance_farValue": 0.1,
                "scaleByDistance_near": 1000,
                "scaleByDistance_nearValue": 1,
                "distanceDisplayCondition_far": 10000,
                "visibleDepth": true
            }
        },
        popup: [],
        popupOptions: {
            showNull: true
        },
        flyTo: true
    })

    drawingGraphicLayer.bindContextMenu([
        {
            text: "开始编辑对象",
            icon: "fa fa-edit",
            show: function (e) {
            const graphic = e.graphic
            if (!graphic || !graphic.hasEdit) {
                return false
            }
            return !graphic.isEditing
            },
            callback: (e) => {
            const graphic = e.graphic
            if (!graphic) {
                return false
            }
            if (graphic) {
                drawingGraphicLayer.startEditing(graphic)
            }
            }
        },
        {
            text: "停止编辑对象",
            icon: "fa fa-edit",
            show: function (e) {
            const graphic = e.graphic
            if (!graphic || !graphic.hasEdit) {
                return false
            }
            return graphic.isEditing
            },
            callback: (e) => {
            const graphic = e.graphic
            if (!graphic) {
                return false
            }
            if (graphic) {
                graphic.stopEditing()
            }
            }
        },
        {
            text: "还原编辑(还原到初始)",
            icon: "fa fa-pencil",
            show: (event) => {
            function hasRestore(graphic) {
                if (!graphic || !graphic.hasEdit || !graphic.isEditing) {
                return false
                }
                return graphic.editing?.hasRestore()
            }

            const graphic = event.graphic
            if (hasRestore(graphic)) {
                return true
            }
            if (graphic.isPrivate && graphic.parent) {
                return hasRestore(graphic.parent) // 右击是编辑点时
            }
            return false
            },
            callback: (event) => {
            const graphic = event.graphic
            if (graphic.editing?.restore) {
                graphic.editing.restore() // 撤销编辑，可直接调用
            } else if (graphic.parent?.editing?.restore) {
                graphic.parent.editing.restore() // 右击是编辑点时
            }
            }
        },
        {
            text: "撤销编辑(还原到上一步)",
            icon: "fa fa-pencil",
            show: (event) => {
            function hasRevoke(graphic) {
                if (!graphic || !graphic.hasEdit || !graphic.isEditing) {
                return false
                }
                return graphic.editing?.hasRevoke()
            }

            const graphic = event.graphic
            if (hasRevoke(graphic)) {
                return true
            }
            if (graphic.isPrivate && graphic.parent) {
                return hasRevoke(graphic.parent) // 右击是编辑点时
            }
            return false
            },
            callback: (event) => {
            const graphic = event.graphic
            if (graphic.editing?.revoke) {
                graphic.editing.revoke() // 撤销编辑，可直接调用
            } else if (graphic.parent?.editing?.revoke) {
                graphic.parent.editing.revoke() // 右击是编辑点时
            }
            }
        }
    ])
    map.addLayer(drawingGraphicLayer)

    // 获取编辑结果
    drawingGraphicLayer.on(mars3d.EventType.editStop, function (e) {
       let resultGeojson =  e.graphic.toGeoJSON({standard:true,noAlt:true})

       let resultWkt = GeojsonToWkt(resultGeojson.geometry)
       EventBus.emit("editLayerEnd",resultWkt)
    })

    // 
}

// 添加图斑
const AddDrawingGeometry = (map) => {
    
}

// 查看图斑
const DetailDrawingGeometryy = (map,geometry) => {
    // 获取geojson
    let geojson = null;

    if(typeof geometry == 'string'){
        let jsonData = WktToGeojson(geometry)

        geojson = {
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "geometry": jsonData
                }
            ]
        }
    }

    
}

// 结束停止编辑
const DrawingEnd = () => {
    drawingGraphicLayer.clear();
}


export {WktToGeojson,GeojsonToWkt,GetGeometryCenter,EditDrawingGeometry,AddDrawingGeometry,DrawingEnd}