//加载地图控件
import CityGis from "@/utils/map/CityGis.Bridge";
let bridge
let calculateFinish = false

export function initBridge(){
    bridge = new CityGis.Bridge({
        id: "i_map",
        url: "http://10.83.68.108:8090/mapVue/#/map", //政务网
        // url: "http://localhost:8081/#/map", //政务网
        onReady: function(){
            console.log("地图创建完成");
        }
    });
    bridge.addEventListener((arg) => {
        switch (arg.action) {
            case "MapIsReady":
                break;
            case "MapExtentChanged":
                console.log(arg)
                // 地图标记
                this.mapExtent = arg.data.result
                if(this.showBoat){
                    this.djldShip()
                }
                break;
            default:
                console.log(JSON.stringify(arg.data, null, 4))
        }
    });
}
/*切换底图*/
export function changeMap(type){
    let params = {
        "ActionName": "ChangeBaseMap",
        "Parameters": {
            "title": type,
            "opacity": 1
        }
    };
    bridge.Invoke(params);
}

/* 定位船舶 */
/*参数格式：
[
    {
        "mmsi": "413266820",
        "shipNameCn": "苏州号",
        "shipTypeName": "客船",
        "direction": 245,
        "velocity": 0,
        "shipBreadth": 22,
        "shipLength": 154,
        "mapx": 2755.61767,
        "mapy": 1521.00687
    }
]*/
export function addBoats(boats){
    let params = {
        "ActionName": "AddBoat",
        "Parameters": {
            "scale": boats.scale,
            "is_goto": boats.goto,
            "data": boats.data
        }
    };
    bridge.Invoke(params);
}

/* 绘制船舶轨迹 */
/*参数格式：
[
    {
        "mapx": 2755.61767,
        "mapy": 1521.00687,
        "time": "2021/1/18 9:40:53"
    },
    {
        "mapx": 2705.61767,
        "mapy": 1481.00687,
        "time": "2021/1/18 10:07:19"
    }
]*/
export function setBoatHistory(datas){
    let params = {
        "ActionName": "SetBoatHistory",
        "Parameters": {
            "data":datas
        }
    };
    bridge.Invoke(params);
}

/**
 * 开始：start，
 * 暂停：pause，
 * 继续：resume，
 * 停止：stop
 * @param type
 */
export function playBoatHistory(type){
    let params = {
        "ActionName": "PlayBoatHistory",
        "Parameters": {
            "status": type
        }
    };
    bridge.Invoke(params);
}

/*查询服务*/
/*参数格式：
{
    "ActionName": "LayerQuery",
    "Parameters": {
        "title": "ghjg_cxall",
        "layerid": "0",
        "where": "视频点名称='华江石油码头'",
        "outFields": [
            "*"
        ],
        "returnGeometry": true,
        "is_draw": true,
        "symbol": {
            "type": "simple-marker",
            "style": "circle",
            "color": [
                255,
                0,
                0
            ],
            "size": 10
        }
    }
}*/
export function layerQuery(paramData){
    let params = {
        "ActionName": "LayerQuery",
        "Parameters": {
            "title": paramData.title,
            "layerid": paramData.layerId,
            "where": paramData.where,
            "outFields": [
                "*"
            ],
            "returnGeometry": true,
            "is_draw": paramData.is_draw == null?true:paramData.is_draw,
            "is_clear": paramData.is_clear == null?true:paramData.is_clear,
            "symbol": paramData.symbol
        }
    };
    console.log("params",params)
    bridge.Invoke(params);
    bridge.addEventListener((arg) => {
        if(arg.action == "ReturnDataList"){
            console.log(arg)
            if(arg.status == false){
                this.$message({
                    showClose: true,
                    message: "未查询到结果！",
                    type: "warning"
                })
                return
            }
            // 回调函数
            if(paramData.callback == 'pjylPortHandle'){
                this.pjylPortHandle(arg)
            }else if(paramData.callback == 'djldPortHandle'){
                this.djldPortHandle(arg)
            }
        }
    });
}
/*重置*/
export function resetMap(){
    let params = {
        "ActionName": "ResetMap"
    };
    bridge.Invoke(params);
}

/*全景*/
export function fullExtent(){
    let params = {
        "ActionName": "FullExtent"
    };
    bridge.Invoke(params);
}

/*测距/测面积*/
/*测距：distance   测面积：area*/
export function calculation(type){
    let g_type
    calculateFinish = false
    if(type == "distance"){
        g_type = "polyline"
    }
    if(type == "area"){
        g_type = "polygon"
    }

    let params = {
        "ActionName": "Calculation",
        "Parameters": {
            "type": g_type,
            "is_show": false
        }
    };
    bridge.Invoke(params);
    bridge.addEventListener((arg) => {
        if(arg.action == "Calculation" && calculateFinish == false){
            calculateFinish = true
            alert("测量结果：" + arg.data.result)
        }
    });
}

/*图层开关*/
export function layerControl(data){
    if(data == "" || data == null){
        return
    }
    this.mapClickLayers = []
    if(data.visible == true){
        // this.mapClickLayers = data.sublayers
        let sublayers = data.sublayers.split(",")
        for(let i=0;i<sublayers.length;i++){
            this.mapClickLayers.push(sublayers[i])
        }
    }
    let params = {
        "ActionName": "LayerControl",
        "Parameters": {
            "title": data.title,
            "visible": data.visible,
            "opacity": data.opacity,
            "sublayers": data.sublayers  // 倒序
        }
    };
    bridge.Invoke(params);
}

/*绘制点线面*/
/*参数说明：
    point 点；
    polyline 线；
    polygon 面；*/
export function draw(type){
    let params = {
        "ActionName": "Draw",
        "Parameters": {
            "type": type
        }
    };
    bridge.Invoke(params);
    bridge.addEventListener((arg) => {
        if(arg.action == "DrawComplete"){
            // 地图标记
            if(this.componentsStatus.showMapMark){
                if(!arg.data.geometry){
                    return
                }
                let data = arg.data.geometry
                let path = ''
                if(type == "point"){
                    path = data.x.toFixed(2)+":"+data.y.toFixed(2)
                }else if(type == "polyline"){
                    let paths = data.paths[0][0]
                    for(let i in paths){
                        path = paths[i][0]+":"+paths[i][1]+"/"
                    }
                    path = path.substring(0,path.length-1)
                }else if(type == "polygon"){
                    let rings = data.rings[0][0]
                    for(let i in rings){
                        path = rings[i][0]+":"+rings[i][1]+"/"
                    }
                    path = path.substring(0,path.length-1)
                }
                this.$refs.divMark.setPath(path)
            }
            // 区域统计
            if(this.componentsStatus.showMapStatistics){
                if(!arg.data.geometry){
                    return
                }
                this.$refs.divStatistics.setGeometry(arg.data.geometry)
            }
            return this.drawGeometry = arg.data.geometry
        }
    });
}

// 标记图形
export function addGraphic(datas){
    console.log("addGraphic:",datas)
    let params = {
        "ActionName": "AddGraphic",
        "Parameters": {
            "type": datas.type,
            "edit": datas.edit,
            "temp": datas.temp,
            "clear": datas.clear,
            "goto": datas.goto,
            "data": datas.data,
            "attributes": datas.attributes,
            "symbol": datas.symbol,
            "title": datas.title
        }
    };
    bridge.Invoke(params);
}

// 设置摄像机
export function setCamera(data){
    let params = {
        "ActionName": "SetCamera",
        "Parameters": {
            "position": {
                "x": data.x,
                "y": data.y,
                "z": data.z,
                "spatialReference": 102100
            },
            "heading": 0,
            "tilt": 60
        }
    };
    bridge.Invoke(params);
}

/*清空地图*/
export function clearMap(){
    let params = {
        "ActionName": "ClearMap",
        "Parameters": {
            "is_draw": true,
            "is_search": true,
            "is_position": true
        }
    };
    bridge.Invoke(params);
}

export function getDrawGeometry(){
    return this.drawGeometry
}
