export default {
  namespaced: true,
  state() {
    return {
      // 场景列表
      list: [],
      //当前场景
      currentScene: {},
      //访问密码
      password: "",
      //场景数据
      metadata: {},
      //当前一级分组
      currentCatalogRoot: {},
      //当前二级分组
      currentSecondary: {},
      //二级分组
      secondaryList: {},
      //当前场景分组
      currentScenesList: {},
      //当时场景的版本
      fdkkCurrentVersion: 'v4',


    };
  },
  getters: {
    list: (state) => state.list,
    secondaryList: (state) => state.secondaryList,
    currentCatalogRoot: (state) => state.currentCatalogRoot,
    currentScenesList: (state) => state.currentScenesList,
    currentSecondary: (state) => state.currentSecondary,
    currentScene: (state) => state.currentScene,
    fdkkCurrentVersion: (state) => state.fdkkCurrentVersion,
    password: (state) => state.password,
    metadata: (state) => state.metadata,
    fdkkBGM: (state) => state.fdkkBGM,

    customLink: (state, getters, rootState, rootGetters) => {
      let metadata = getters.metadata
      if (metadata.customButton) {
        let temp = JSON.parse(JSON.stringify(metadata.customButton))
          return temp.find(item=>item.type == '链接')
      }
 
      return {}
    },

    
    customTelephone: (state, getters, rootState, rootGetters) => {
      let metadata = getters.metadata
      if (metadata.customButton) {
        console.log(metadata.customButton);
        let temp = JSON.parse(JSON.stringify(metadata.customButton))
          return temp.find(item=>item.type == '电话')
      }
 
      return {}
    },
    musicURL: (state, getters, rootState, rootGetters) => {
      let metadata = getters.metadata
      if (metadata.backgroundMusic && metadata.backgroundMusic.id) {
        return metadata.backgroundMusic.ossPath
      }
      return null
    },

    earthMask: (state, getters) => {
      let metadata = getters.metadata
      if (metadata.customMask && metadata.customMask.earth) {
        return metadata.customMask.earth
      }
      return null
    },

    skyMask: (state, getters) => {
      let metadata = getters.metadata
      if (metadata.customMask && metadata.customMask.sky) {
        return metadata.customMask.sky
      }
      return null
    },
  },
  mutations: {
    setScenes(state, payload) {
      state.list = payload;
    },
    setPassword(state, payload) {
      state.password = payload;
    },
    setFdkkCurrentVersion(state, payload) {
      state.fdkkCurrentVersion = payload;
    },
    

    // 设置当前场景
    setCurrentScene(state, payload) {
      if (payload.someData) {
        try {
          // someData旧数据有可能是字符串，要parse一下
          payload.someData = typeof payload.someData == 'string' ? JSON.parse(payload.someData) : payload.someData
        } catch (error) {
        }
      }
      state.currentScene = payload;
      this.commit('fdkk/setReset')
    },
    // 设置当前二级分组
    setCurrentSecondary(state, payload) {
      state.currentSecondary = payload;
      let arr = state.list.filter((item) => {
        return state.currentSecondary.id == item.category;
      });

      this.commit('scene/setCurrentScenesList', arr.sort((a, b) => a.weight - b.weight))
    },

    // 设置当前场景列表
    setCurrentScenesList(state, payload) {
      state.currentScenesList = payload;
    },

    // 设置当前一级分组
    setCurrentCatalogRoot(state, payload) {
      state.currentCatalogRoot = payload;
      let temp = [];
      payload.children &&
        payload.children.forEach((item) => {
          state.metadata.catalogs.forEach((sub) => {
            if (item == sub.id) {
              if (state.list.some(iii => iii.category == sub.id)) {
                temp.push(sub);
              }
            }
          });
        });

      this.commit('scene/setSecondaryList', temp)
    },

    // 设置当前二级分组列表
    setSecondaryList(state, payload) {
      state.secondaryList = payload
      if (payload.length > 0) {
        this.commit('scene/setCurrentSecondary', payload[0])
      }
    },
    setMetaData(state, payload) {
      state.metadata = payload;
      document.title = payload.name || '无标题'
    },
  
  },
};
