
import { appconfig } from '@/config/bigScreenConfig'
import { GetUserInitData } from '@/api/APIs'
const initConfig = JSON.parse(JSON.stringify(appconfig.bigScreenMapSettings.gisResource))

const groupPageInfo = () => {
  return {
    //集团、分公司信息
    groupProject: [],
    groupName: null,
    groupCodeValue: null,
    isShowGroupPage: null,
    //头部
    projectTreeData: null,
    currentMenuItem: null,//下拉切换
  }
}
const projectInfo = () => {
  return {
    //初始范围
    initView: {
      destination: { x: -2002099.8064685483, y: 5125053.48587751, z: 3240386.7848629407 },
      orientation: {
        heading: 6.241173904929705,
        pitch: -0.3248275425737035,
        roll: 6.283185307179586
      }
    },
    //三维图层
    sceneLayerList: [
      'NETWORK_SJ_PSWS@sxgk#1',
      'NETWORK_SJ_PSYS@sxgk#1',
      'NETWORK_SJ_PSWS_Node@sxgk#1',
      'NETWORK_SJ_PSYS_Node@sxgk#1',
      'TF_SJ_PSYS_JG_B_3D@sxgk#1',
      'TF_SJ_PSYS_JG_B_3D@sxgk#2',
      'TF_SJ_PSWS_JG_B_3D@sxgk#2',
      'TF_SJ_PSWS_JG_B_3D@sxgk#1'
    ],
    //解决点击冲突
    isClickingPipe: false,
    //地图配置信息
    tdttoken: '75293e320524bb2be0a5f14fa887e46f',
    mapConfig: null,
    isInitViewer: false,//三维场景是否初始化
    mapContainerId: '',//地图容器Id

    //项目级信息
    currentPrjId: null,
    currentProjectInfo: null,//当前项目信息
    currentProjectCode: null,
    currentActive: null,//项目当前激活模块
    customToolList: [],//自定义工具激活集合
    currentTool: '',//当前激活工具
    currentPipeInfo: null,//部件查看当前部件信息
    isShowPipeSpeedInfoCheck: false,//显示进度查看弹窗
    monitorTreeCheckHistory: null,//设备监测树历史记录
    currentDeviceShow: null,//当前查看设备
    isShowExcavationAnalysis: false,//显示开挖分析窗体
    isShowSearchBox: false,//显示搜索栏
    //-->统计分析模块
    currentStatisticalActive: null,
    statisticalAnalysisInfo: null,
    //-->智慧预警模块
    wisdomWarningInfo: null
  }
}
const state = { ...groupPageInfo(), ...projectInfo() }
//检查是否存在已有数据方法
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_PROJECTTREEDATA: (state, payload) => {
    state.projectTreeData = payload
    state.currentMenuItem = payload[0]
  },
  //部件查看设置
  Active_PipeUnitInfo: (state, data) => {

  },
  Deactive_PipeUnitInfo: (state, data) => {

  },
  //项目级工具集设置
  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_Project: (state) => {
    Object.assign(state, projectInfo())
  },
  RESET_ALL: (state) => {
    Object.assign(state, { ...groupPageInfo(), ...projectInfo() })
  }
}

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, state }) {
    return new Promise((resolve, reject) => {
      GetUserInitData({ platform: 1 }).then(res => {
        debugger
        //重置GIS资源值
        Object.keys(appconfig.bigScreenMapSettings.gisResource).forEach(item => appconfig.bigScreenMapSettings.gisResource[item].config = {})
        const result = res.result.progisresource.find(e => e.code == `groupPageMapService_${state.currentProjectCode}`)
        if (!result) {
          commit('SET_GISSOURCE', appconfig.bigScreenMapSettings);
          return resolve(appconfig.bigScreenMapSettings);
        }
        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, ...item }
          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, ...item }
        });
        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)
      })
    })
  },
  //设置项目列表
  setProjectTreeData({ commit }, value) {
    commit('SET_PROJECTTREEDATA', value)
  },
  //部件查看使用时功能启动
  activePipeUnitInfo({ commit, state }) {

  },
  //部件查看使用时功能结束
  deactivePipeUnitInfo({ commit, state }) {

  },
  // 添加工具集
  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数据
  resetProjectState({ commit, state }, value) {
    commit('RESET_Project', value)
  },
  //重置state数据
  resetState({ commit, state }, value) {
    commit('RESET_ALL', value)
  },
}

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