import { 
	getWelcomePage,
	getChannelInfo,
	getMenuList,
	getAdvertList,
	getWatchLoginInfo,
	PostInfo,
} from '@/plugins/API/apiAll'
import QuickStorage from '@/plugins/QuickStorage/index';
const Cookie = process.client ? require('js-cookie') : undefined;

const actions = {
  	async get_control({state, commit, dispatch}, obj){
  		await Promise.all([
			dispatch('get_channerInfo', {id: obj.id}),
			dispatch('get_welcomeInfo', {id: obj.id})
		])
  	},
  	async get_welcomeInfo({state, commit, dispatch}, obj){
		const res = await getWelcomePage({
			includeId: obj.id,
			type: state.type
		})
		if(res.code == 200 && res.errorCode == 0){
			commit('set_welcomeInfo', res.data)
		}
	},
	async get_channerInfo({state, commit, dispatch}, obj){
		const res = await getChannelInfo({
			id: obj.id
        })
		if(res.code == 200 && res.errorCode == 0){
			if(res.data.status != 1){
				commit('set_status', 404);
				return false;
            }
			commit('set_channelInfo', res.data)
			await Promise.all([
				dispatch('get_menuList'),
				dispatch('get_advertList')
			])
		}else if(res.code == 200 && res.errorCode == 3){
			commit('set_status', 408);
        }else{
			commit('set_status', 404);
        }
	},
	async get_menuList({state, commit}){
		const res = await getMenuList({
			id: state.channelInfo.id,
            uin: state.channelInfo.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, dispatch}){
		const channelInfo = state.channelInfo;
		const advertTop = {status: channelInfo.advertTopStatus, list: [], close: channelInfo.advertTopIsclose};
		const advertMiddle = {status: channelInfo.advertMiddleStatus, list: [], close: channelInfo.advertMiddleIsclose};
		const advertText = {status: channelInfo.advertTextStatus, words: channelInfo.advertWords, link:channelInfo.advertWordsLink};

		if(channelInfo.advertMiddleStatus || channelInfo.advertTopStatus){
            const res = await getAdvertList({
				id: state.channelInfo.id,
                uin: state.channelInfo.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)
		commit('set_advertText', advertText)
	},
	async get_userInfo({state, commit, dispatch}, obj){
		const res = await getWatchLoginInfo({
			id: state.channelInfo.id,
            uin: state.channelInfo.uin,
            refererId: obj.refererId || 0
		})
		if(res.code == 200 && res.errorCode == 0){
            let info = res.data.watchUserInfo;
            if(res.data.userTimeKey){
                info.userTimeKey = res.data.userTimeKey
            }
            commit('set_userInfo', info)

            dispatch('PostInfo')
        }
	},
	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){
	           
	        }
		}
	}
}
export default actions;