import {
  getWelcomePage,
  getChannelInfo,
  getChannelConfig,
  getMenuList,
  getAdvertList,
  invitation,
  PostInfo
} from '@/plugins/API/apiAll';
import QuickStorage from '@/plugins/QuickStorage/index';

const actions = {
  async get_control({ dispatch }, obj) {
    await Promise.all([dispatch('get_channerInfo', { id: obj.id })]);
  },
  async get_channerInfo({ state, commit, dispatch }, obj) {
    const { id } = obj;
    const res = await getChannelInfo({ id });
    if (res.code === 200 && res.errorCode === 0) {
      if (res.data.status !== 1) {
        commit('set_status', 404);
        return false;
      }
      commit('set_channelInfo', res.data);
      const advertText = {
        status: 1,
        words: res.data.advertWords,
        link: res.data.advertWordsLink
      };
      commit('set_advertText', advertText);
      await Promise.all([
        dispatch('get_welcomeInfo'),
        dispatch('get_menuList'),
        dispatch('get_channelConfig')
      ]);
    } else if (res.code === 200 && res.errorCode === 3) {
      commit('set_status', 408);
    } else {
      commit('set_status', 404);
    }
  },
  async get_welcomeInfo({ state, commit }, obj) {
    const { id } = state.channelInfo;
    const { type } = state;
    const res = await getWelcomePage({ includeId: id, type });
    if (res.code === 200 && res.errorCode === 0) {
      commit('set_welcomeInfo', res.data);
    }
  },
  async get_channelConfig({ state, commit }, configType) {
    const { id } = state.channelInfo;
    const res = await getChannelConfig({
      channelId: id,
      type: 1,
      configType: configType
    });
    if (res.code === 200 && res.errorCode === 0) {
      const info = res.data || {};
      commit('set_channelConfig', info);
    }
  },
  async get_menuList({ state, commit }) {
    const { id, uin } = state.channelInfo;
    const res = await getMenuList({ id, uin });
    if (res.code === 200 && res.errorCode === 0) {
      let list = [];
      if (res.data.length) {
        list = res.data;
      } else {
        list = [
          {
            menuName: '聊天互动',
            menuType: 'chat'
          }
        ];
      }
      commit('set_menuList', list);
    }
  },
  async get_advertList({ state, commit }) {
    const { channelInfo } = state;
    const { id, uin } = channelInfo;
    const advertTop = {
      status: 1,
      list: [],
      close: channelInfo.advertTopIsclose
    };
    const advertMiddle = {
      status: 1,
      list: [],
      close: channelInfo.advertMiddleIsclose
    };
    const res = await getAdvertList({ id, uin });
    if (res.code === 200 && res.errorCode === 0) {
      advertTop.list = res.data.advertTopList || [];
      advertMiddle.list = res.data.advertMiddleList || [];
    }
    commit('set_advertTop', advertTop);
    commit('set_advertMiddle', advertMiddle);
  },
  async invitation({ state }, obj) {
    const res = await invitation({
      uin: state.channelInfo.uin,
      channelId: state.channelInfo.id,
      refererId: obj.refererId || 0
    });
    if (res.code === 200 && res.errorCode === 0) {
      console.log(res);
    }
  },
  async PostInfo({ state }) {
    if (process.client) {
      const res = await PostInfo({
        channelId: state.channelInfo.id,
        uin: state.channelInfo.uin,
        userId: state.userInfo.id || 0,
        type: 0, // live
        deviceid: QuickStorage.localGet('DEVICE_ID')
      });
      if (res.code === 200 && res.errorCode === 0) {
        console.log(res);
      }
    }
  },
  set_nowDate({ commit }) {
    setInterval(() => {
      commit('set_nowDate');
    }, 1000);
  }
};
export default actions;
