
import { appconfig } from '@/config/bigScreenConfig'
import { GetUserInitData } from '@/api/APIs'
const groupPageInfo = () => {
  return {
    //集团、分公司信息
    groupProject: [],
    groupName: null,
    groupCodeValue: null,
  }
}
const getDefaultState = () => {
  return {
    //地图配置信息
    tdttoken: '75293e320524bb2be0a5f14fa887e46f',
    mapConfig: null,
    isInitViewer: false,//三维场景是否初始化
    mapContainerId: '',//地图容器Id
    //项目级信息
    currentPrjId: 45,
    currentActive: null,//项目当前激活模块
    customToolList: [],//自定义工具激活集合
    currentTool: '',//当前激活工具
    isShowPipeSpeedInfoCheck: false,//显示进度查看弹窗
    monitorTreeCheckHistory: null,//设备监测树历史记录
    currentDeviceShow: null,//当前查看设备
    isShowExcavationAnalysis: false,//显示开挖分析窗体
    isShowSearchBox: false,//显示搜索栏
    //-->统计分析模块
    currentStatisticalActive: null
  }
}
const state = { ...groupPageInfo(), ...getDefaultState() }
//检查是否存在已有数据方法
function some(arr, str) {
  const result = arr.some((item) => {
    if (item.name === str) {
      return true
    }
  })
  return result
}
function delOne(arr, str) {
  return arr.filter(item => item.name !== str)
}
const mutations = {
  SET_GISSOURCE: (state, payload) => {
    state.mapConfig = payload
  },
  SET_TOOL: (state, data) => {
    state.currentTool = data.name
    if (!some(state.customToolList, data.name)) {
      state.customToolList.push(data)
    } else {
      state.customToolList = state.customToolList.slice()
    }
  },
  DEL_TOOL: (state, data) => {
    state.customToolList = delOne(state.customToolList, data)
    if (state.customToolList.length > 0) {
      state.currentTool = state.customToolList[state.customToolList.length - 1].name
    }
  },
  DEL_ALLTOOL: (state) => {
    state.customToolList = []
  },
  RESET_ALL: (state) => {
    Object.assign(state, getDefaultState())
  }
}

function isEmpty(obj) {
  if (Object.keys(obj).length === 0) return true
  else return false
}
function short(key) {
  return key.substring(key.indexOf("_") + 1, key.length)
}
const actions = {
  initGISResource({ commit }) {
    return new Promise((resolve, reject) => {
      GetUserInitData({ platform: 1 }).then(res => {
        const result = res.result.progisresource.find(e => e.code == "groupPageMapService")
        if (!result) return
        let mapSource = appconfig.bigScreenMapSettings.gisResource
        let terrain = {}, img = {}, scenes = {}, maps = {},
          tiplayers = {}, geometry = {}, theme = {}
        result.config.forEach(item => {
          if (item.key.indexOf('terrain') != -1) terrain[short(item.key)] = { "name": item.name, "url": item.url }
          if (item.key.indexOf('tiled') != -1) img[short(item.key)] = { "name": item.name, "url": item.url }
          if (item.key.indexOf('scene') != -1) scenes[short(item.key)] = { "name": item.name, "url": item.url }
          if (item.key.indexOf('map') != -1) maps[short(item.key)] = { "name": item.name, "url": item.url }
          if (item.key.indexOf('data') != -1) tiplayers[short(item.key)] = { "name": item.name, "url": item.url }
          if (item.key.indexOf('spatialanalyst') != -1) geometry[short(item.key)] = { "name": item.name, "url": item.url }
          if (item.key.indexOf('theme') != -1) theme[short(item.key)] = { "name": item.name, "url": item.url, "isDisplay": item.isDisplay ? item.isDisplay : '0' }
        });
        Object.keys(mapSource).forEach(item => {
          switch (mapSource[item].type) {
            case "terrain": if (!isEmpty(terrain)) mapSource[item].config = terrain; break;
            case "tiled": if (!isEmpty(img)) mapSource[item].config = img; break;
            case "scene": if (!isEmpty(scenes)) mapSource[item].config = scenes; break;
            case "map": if (!isEmpty(maps)) mapSource[item].config = maps; break;
            case "data": if (!isEmpty(tiplayers)) mapSource[item].config = tiplayers; break;
            case "spatialanalyst": if (!isEmpty(geometry)) mapSource[item].config = geometry; break;
            case "theme": if (!isEmpty(theme)) mapSource[item].config = theme; break;
          }
        })
        appconfig.bigScreenMapSettings.gisResource = mapSource
        commit('SET_GISSOURCE', appconfig.bigScreenMapSettings);
        resolve(appconfig.bigScreenMapSettings);
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 添加工具集
  changeToolList({ commit, state }, value) {
    commit('SET_TOOL', value)
  },
  //移除工具集
  removeSingleTool({ commit, state }, value) {
    commit('DEL_TOOL', value)
  },
  //移除所有工具
  removeAllTool({ commit, state }, value) {
    commit('DEL_ALLTOOL', value)
  },
  //重置state数据
  resetState({ commit, state }, value) {
    commit('RESET_ALL', value)
  },
}

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