import {Module} from "vuex";

import $request from '$utils/request';

import cate from '$config/cate';

export default <Module<any,any>>{

    state:{
        gameCate:[],
        recommendGameCate:[],
        gameStatusCate:[],
        roomCate:[],
        recommendRoomCate:[]
    },

    mutations:{

        setGameCatePromise(state,data){

            let gameCate = [
                {
                    label:'游戏类',
                    children:[]
                }
            ];

            let recommendGameCate = [];

            let maxSize = cate.gameCate.config.size - cate.gameCate.config.footer.length;

            data.unshift(...cate.gameCate.config.header);


            gameCate.map((item,index)=>{

                item.children = data;

                let resultObject:Record<string, any> = {};

                data.map((item)=> {
                    resultObject[item.gid]=item;

                    if (recommendGameCate.length < maxSize) {
                        recommendGameCate.push(item);
                    }
                });

                cate.gameCate.object = resultObject;

            });

            recommendGameCate.push(...cate.gameCate.config.footer);

            state.gameCate = gameCate;
            state.recommendGameCate = recommendGameCate;

        },

        setGameStatusCate(state,data){
            let gameCate = [
                {
                    label:'游戏类',
                    children:[]
                }
            ];

            gameCate.map((item,index)=>{

                item.children = data;

            });

            state.gameStatusCate = gameCate;
        },

        setGameItemStatus(state,{
            gid,
            status
        }){

            state.gameStatusCate.map((item)=>{

                for (let i=0,count=item.children.length;i<count;i++) {
                    if (item.children[i].gid === gid) {

                        if (item.children[i].status !== status) {
                            item.children[i].status = status;
                        }

                        break;
                    }
                }

            });

            state.gameStatusCate = JSON.parse(JSON.stringify(state.gameStatusCate));

        },

        setRoomCate(state,data){

            let maxSize = cate.roomCate.config.size - cate.roomCate.config.footer.length;
            let recommendRoomCate = [];

            data.unshift(...cate.roomCate.config.header);
            let resultObject:Record<string, any> = {};
            data = data.map((item)=>{

                if (item.type_name) {
                    item = {
                        label: item.type_name,
                        tid: item.tid,
                        image: item.image
                    };
                    resultObject[item.tid]=item;
                }



                if (recommendRoomCate.length < maxSize) {
                    recommendRoomCate.push(item);
                }

                return item;
            });

            cate.roomCate.object = resultObject;
            recommendRoomCate.push(...cate.roomCate.config.footer);


            state.roomCate = data;
            state.recommendRoomCate = recommendRoomCate;
        }

    },

    actions:{

        initializationRoomCatePromise({commit}){

            return new Promise(function (resolve, reject) {
                $request({
                    url:'room/get_room_type',
                    cache:true,
                    failMessage:true,
                    token:true
                }).then((data)=>{
                    commit('setRoomCate',data.data);
                    return resolve(data.data);
                }).catch(reject)
            });

        },

        initializationCatePromise({commit}){

            return new Promise(function (resolve, reject) {
                $request({
                    url:'game/get_game_list',
                    cache:true,
                    failMessage:true
                }).then((data)=>{
                    commit('setGameCatePromise',data.data);
                    return resolve(data.data);
                }).catch(reject)
            });

        },

        initializationGameStatusPromise({commit,dispatch}) {

            return new Promise(function (resolve, reject) {
                $request({
                    url:'player/get_player_game_list',
                    cache:true,
                    token:true,
                    failMessage:true
                }).then((data)=>{
                    commit('setGameStatusCate',data.data);
                    return resolve(data.data);
                }).catch(reject)
            });
        },

        updateGameStatusPromise({commit}) {
            return new Promise(function (resolve, reject) {
                $request({
                    url:'player/get_player_game_list',
                    token:true,
                    cache:{
                      updated:true
                    },
                }).then((data)=>{
                    commit('setGameStatusCate',data.data);
                    return resolve(data.data);
                }).catch(reject)
            });
        }

    }

}