import {myBridge,previewMapBridge} from "./map.js";
import {getMapToken,getServiceToken} from "../../service/map.js";

export function invokeParams(ActionName,params){
    getMapToken().then(res =>{
        let token = res.data.msg[0].Rows[0].token;
        myBridge.bridgeContent.Invoke({
            'ActionName': ActionName,
            "Parameters": {
                token: token,
                ...params
            }
        })
    })
}
export function previewInvokeParams(ActionName, params) {
    getMapToken().then(res =>{
        let token = res.data.msg[0].Rows[0].token;
        previewMapBridge.bridgeContent.Invoke({
            'ActionName': ActionName,
            "Parameters": {
                token: token,
                ...params
            }
        })
    })
}

async function returnProxyUrl(url){
    let arr = url.split('/');
    let username = '';
    let password = '';
    debugger
    if(arr.indexOf('MapProxyApi') !== -1){ //不需要授权服务
        for(let i=0;i<arr.length;i++){
            if(arr[i] ==='getSceneServer'){
                username = arr[i+1];
                password = arr[i+2];
                const response = await getServiceToken(username,password);
                if(response?.data.length > 0){
                    arr.splice(i-1, 4);
                    return arr.join('/')+'/MapServiceProxy/'+response.data;
                }
            }
        }
    }else{
        return url
    }
}

export function SetBackground(){
    invokeParams('SetBackground', {
        "opacity": 1,
        "color": "transparent",
    })
}

//点查询
export function MapClickByType(status){
    invokeParams('MapClickAll', {
        "status": status,
        "disableLayerIdList":["districtArea","districtStreet"]
    })
}

export async function AddBaseMapLayer() {
    let url = await returnProxyUrl("https://cimweb.zjw.sh.cegn.cn:2008/MapProxyApi/getSceneServer/ptgl/93493321");
    console.log("1111",url);
    debugger
    invokeParams('AddBaseMapLayer', {
        "id": "BaseMap",
        "title": "BaseMap",
        "visible": true,
        "opacity": 1,
        url
    })
}

export async function AddZhenwuMapLayer() {
    let url = await returnProxyUrl("https://cimweb.zjw.sh.cegn.cn:2008/MapProxyApi/getSceneServer/ptgl/37072265");
    console.log("1111",url);
    debugger
    invokeParams('AddBaseMapLayer', {
        "id": "BaseMapZhenwu",
        "title": "BaseMapZhenwu",
        "visible": true,
        "opacity": 1,
        url
    })
}

export async function AddYaoganMapLayer() {
    let url = await returnProxyUrl("https://cimweb.zjw.sh.cegn.cn:2008/MapProxyApi/getSceneServer/ptgl/50115036");
    console.log("1111",url);
    debugger
    invokeParams('AddBaseMapLayer', {
        "id": "BaseMapYaoGan",
        "title": "BaseMapYaoGan",
        "visible": true,
        "opacity": 1,
        "type": "tile",
       url
    })
}




export function AddSingleLayer(params){
    if(params.token){
        params.url = "https://cimweb.zjw.sh.cegn.cn:2008/MapServiceProxy/" + params.token;
    }
    let wkid = "0"  // 0 代表上海城建 1：代表3857
    if(params.wkid){
        if(params.type == "动态地图" && params.wkid =="1"){
            params.type = "dynamic"
        }
        if(params.type == "切片地图" && params.wkid =="1"){
            params.type = "tile"
        }
    }

    if(params.type=="scene"||params.type=='3DObject'){
        invokeParams('AddSingleLayer', {
            "id": params.id,
            "title": params.title,
            "visible": params.visible,
            "opacity": params.opacity === undefined?1:params.opacity,
            "url": params.url,
            "type": "scene",
            "mapID": params.mapID
        })
    }
    else if(params.type=="bim"){
        switch (params.title) {
            case '朱家角车站':
                invokeParams('AddSingleLayer',
                    {
                        "id": params.id,
                        "title": params.title,
                        "visible": params.visible,
                        "opacity": params.opacity === undefined?1:params.opacity,
                        "url": params.url,
                        "elevationInfo": {   //下沉高度
                            mode:"absolute-height",
                            offset:-19.5
                        },
                        "type": "building",
                        "mapID": params.mapID
                    })
                break;
            default:
                invokeParams('AddSingleLayer',
                    {
                        "id": params.id,
                        "title": params.title,
                        "visible": params.visible,
                        "opacity": params.opacity === undefined?1:params.opacity,
                        "url": params.url,
                        "elevationInfo": {   //下沉高度
                            mode:"absolute-height",
                            offset:-19.5
                        },
                        "type": "building",
                        "mapID": params.mapID
                    })
                break
        }
    }
    else if(params.type=='mesh'){
        invokeParams('AddSingleLayer',
            {
                "id": params.id,
                "title": params.title,
                "visible": params.visible,
                "opacity": params.opacity === undefined?1:params.opacity,
                "url": params.url,
                //"renderer":locationRenderer,
                "type": "mesh",
                "mapID": params.mapID
            })
        return
    }
    else if(params.type=="dynamic"){
        invokeParams('AddSingleLayer', {
            "id": params.id,
            "title": params.title,
            "visible": params.visible,
            "opacity": params.opacity === undefined?1:params.opacity,
            "url": params.url,
            // "sublayers": [
            //     {
            //         "id": 0
            //     },{
            //         "id": 1
            //     }
            // ],
            "type": "dynamic",
            "mapID": params.mapID
        })
    }
    else if(params.type=="tile"){
        invokeParams('AddSingleLayer', {
            "id": params.id,
            "title": params.title,
            "visible": params.visible,
            "opacity": params.opacity === undefined?1:params.opacity,
            "url": params.url,
            "type": "tile",
            "mapID": params.mapID
        })
    }else if(params.type=="动态地图"){
        invokeParams('AddSingleLayer', {
            "id": params.id,
            "title": params.title,
            "visible": params.visible,
            "opacity": params.opacity === undefined?1:params.opacity,
            "url": params.url,
            "type": "shc_map",
            "mapID": params.mapID
        })
    }else if(params.type=="切片地图"){
        let size = [256,256];
        let origin = {
            "x": -9080400.0,
            "y": 10002300
        };
        invokeParams('AddSingleLayer',
            {
                "id": params.id,
                "title": params.title,
                "visible": params.visible,
                "opacity": params.opacity === undefined?1:params.opacity,
                "url": params.url,
                "type": "shc_tile",
                "mapID": params.mapID
            })
    }
}