import { Service } from 'egg';
import { STATUS } from '@consts';
import { GameModel } from '@db/Game';
import { ServerlistModel, ServerlistUpdate } from '@db/Serverlist';
import { gameData } from '@pubUtils/data';
import { DicHero } from '@pubUtils/dictionary/DicHero';
import { DicRMB } from '@pubUtils/dictionary/DicRMB';
import { DicActivityType } from '@pubUtils/dictionary/DicActivityType';
import { DicTaskType } from '@pubUtils/dictionary/DicTaskType';
import { ServerStategyModel, ServerStategyTypeParam } from '@db/ServerStategy';
import { NoticeModel, NoticeTypeParam } from '@db/Notice';

/**
 * Test Service
 */
export default class Game extends Service {

    /**
     * 获取正式服，测试服，开发服等环境
     */
    public async getServerEnv() {
        const { ctx, app } = this;
        const list = await GameModel.getServerEnvList();

        return ctx.service.utils.resResult(STATUS.SUCCESS, {
            list,
            env: app.config.env
        });
    }

    /**
     * 获取正式服，测试服，开发服等环境
     */
    public async getServerListByEnv(serverType?: string) {
        const { ctx } = this;
        const list = await ServerlistModel.findByServerType(serverType);

        return ctx.service.utils.resResult(STATUS.SUCCESS, {
            list
        });
    }


    public async getServerList(page: number, pageSize: number, sortField: string, sortOrder: string, form: { id?: number, serverId?: string|number, name?: string, groupName?: string, groupId?: number, serverType?: string } = {}) {
        const { ctx } = this;

        const list = await ServerlistModel.findByCondition(page, pageSize, sortField, sortOrder, form);
        const total = await ServerlistModel.countByCondition( form )
        return ctx.service.utils.resResult(STATUS.SUCCESS, {
            list, total
        });
    }

    public async getServerStategyList(page: number, pageSize: number, sortField: string, sortOrder: string, form: { id?: number, name?: string }) {
        const { ctx } = this;

        const list = await ServerStategyModel.findByCondition(page, pageSize, sortField, sortOrder, form);
        const total = await ServerStategyModel.countByCondition( form )
        return ctx.service.utils.resResult(STATUS.SUCCESS, {
            list, total
        });
    }

    public async updateServerStategy(values: ServerStategyTypeParam & {id: string|number}) {
        const { ctx } = this;

        try {
            await ServerStategyModel.updateServerStategy(values, ctx.user?.uid);
    
        } catch(e) {
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR, null, e.stack);
        }
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async createNewServer(name: string, openTime: number, serverType: string, stategyId: number ) {
        const { ctx } = this;

        try {
            let stategy = await ServerStategyModel.findBySId(stategyId);
            if(!stategy) return ctx.service.utils.resResult(STATUS.GM_STATEGY_NOT_FOUND);

            await ServerlistModel.newServer({ name, openTime: new Date(openTime), serverType }, stategy, ctx.user?.uid);
    
        } catch(e) {
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR, null, e.stack);
        }
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async updateServer(id: number, name: string, groupName: string, serverStatus: number ) {
        const { ctx } = this;

        try {
            let server = await ServerlistModel.findByServerId(id);
            if(!server) return ctx.service.utils.resResult(STATUS.GM_SERVER_NOT_FOUND);

            if(server.groupName != groupName) {
                await ServerlistModel.updateGroupName(server.groupId, groupName);
            }
            let update: ServerlistUpdate = {};
            if(name) update['name'] = name;
            if(groupName) update['groupName'] = groupName;
            if(serverStatus) update['serverStatus'] = serverStatus;

            await ServerlistModel.updateByServerId(id, update);
    
        } catch(e) {
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR, null, e.stack);
        }
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async getDicHero() {
        let list: DicHero[] = [];
        for(let [heroId, hero] of gameData.hero) {
            if(heroId <= 300) {
                list.push(hero)
            }
        }
        return this.ctx.service.utils.resResult(STATUS.SUCCESS, {
            list
        })
    }

    public async getDicGoods() {
        let list = [];
        for(let [_, { good_id, name, pieceId, quality, itid, image_id }] of gameData.goods) {
            list.push({
                good_id, name, pieceId, quality, itid, image_id
            })
        }
        return this.ctx.service.utils.resResult(STATUS.SUCCESS, {
            list
        })
    }

    public async getDicRmb() {
        let list: DicRMB[] = [];
        for(let [_, dicRmb] of gameData.rmb) {
            list.push(dicRmb);
        }
        return this.ctx.service.utils.resResult(STATUS.SUCCESS, {
            list
        })
    }

    public async getDicActivityType() {
        let list: DicActivityType[] = gameData.activityType;
        return this.ctx.service.utils.resResult(STATUS.SUCCESS, {
            list
        })
    }

    public async getDicTaskType() {
        let list: DicTaskType[] = gameData.taskTypeDesc;
        return this.ctx.service.utils.resResult(STATUS.SUCCESS, {
            list
        })
    }

    public async getNoticeList(page: number, pageSize: number, sortField: string, sortOrder: string, form: { type?: number, content?: string }) {
        const { ctx } = this;
        const list = await NoticeModel.findByCondition(page, pageSize, sortField, sortOrder, form);
        const total = await NoticeModel.countByCondition( form )
        return ctx.service.utils.resResult(STATUS.SUCCESS, {
            list, total
        });
    }

    public async updateNotice(id: string|number, params: NoticeTypeParam) {
        const { ctx } = this;
        let result = await NoticeModel.updateNotice(id, params);
        if(!result) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async delNotice(id: number) {
        const { ctx } = this;
        let result = await NoticeModel.delNotice(id);
        if(!result) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }
}
