import AppX from '@/config/configure'
import APIs from '@/api/APIs'
import {geteSessionStorage,setSessionStorage, removeSessionStorage } from '@/utils/auth'
const getDefaultState = () => {
    return {
        tdttoken: '75293e320524bb2be0a5f14fa887e46f',
        AppX: {
            appConfig: {
                infowindow: null
            },
            runtimeConfig: {
                currentTool: null  //该属性记录有地图交互事件的功能，避免不同功能间的地图交互事件冲突
            }
        },
        tdtlayer: { base: 'vec_w', poi: 'cva_w' },
        sceneMode: "3d",//3d 三维模式，2dwith3d:二三维分屏
        mapLegendOpen: false,//图例是否展示
        mapContainerId: "",//地图容器Id
    }
}
const state = getDefaultState()
const mutations = {
    SET_GISSOURCE: (state, payload) => {
        state.AppX.appConfig = payload.appConfig
    },
    SET_MAPLEGEND: (state, payload) => {
        state.mapLegendOpen = payload;
    },
    SET_TDTLAYER: (state, payload) => {
        state.tdtlayer = payload;
    },
    SET_SCENEMODE: (state, payload) => {
        state.sceneMode = payload;
    },
    SET_CURRENTTOOL: (state, payload) => {
        if (state.AppX.runtimeConfig.currentTool) {
            state.AppX.runtimeConfig.currentTool();
        }
        state.AppX.runtimeConfig.currentTool = payload;
    }
}

const actions = {
    mapLegendStatus({ commit }, data) {
        commit('SET_MAPLEGEND', data);
    },
    tdtlayerChange({ commit }, data) {
        commit('SET_TDTLAYER', data);
    },
    sceneModeChange({ commit }, data) {
        commit('SET_SCENEMODE', data);
    },
    changeCurrentTool({ commit }, data) {
        commit('SET_CURRENTTOOL', data);
    },
    initGISResource({ commit,state }) {
        return new Promise((resolve, reject) => {
            if (AppX.appConfig.infowindow) {
                AppX.appConfig.infowindow.remove();
                AppX.appConfig.infowindow = null;
            }
            if (AppX.appConfig.onlineconfig) {
                APIs.GetUserInitData({ platform: 1 }).then((result) => {
                    const resData = result.result;
                    if (resData) {
                        AppX.appConfig.initPosition = JSON.parse(sessionStorage.getItem('initPosition'));
                        AppX.appConfig.currentPrj = JSON.parse(sessionStorage.getItem('currentPrj'));
                        for (var pro in AppX.appConfig.gisResource) {
                            AppX.appConfig.gisResource[pro].config = {};
                            if (resData.progisresource && resData.progisresource.length > 0) {
                                for (
                                    var index = 0;
                                    index < resData.progisresource.length;
                                    index++
                                ) {
                                    if (resData.progisresource[index].code == pro) {
                                        // var configs = (AppX.appConfig.gisResource[pro].config =
                                        //   resData.progisresource[index].config);
                                        var configs = resData.progisresource[index].config;
                                        for (var i = 0; i < configs.length; i++) {
                                            AppX.appConfig.gisResource[pro].config[configs[i].key] = configs[i];
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        commit('SET_GISSOURCE', AppX);
                        resolve(AppX);
                    }
                }).catch((err) => {
                    reject(err);
                })
            } else {
                commit('SET_GISSOURCE', AppX);
                resolve(AppX);
            }
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

