import { ApiCall } from "tsrpc";
import { HeroFun } from '../public/hero';
import { PlayerFun } from '../public/player';
import { defaultUserAppend, UserFun } from '../public/user';
import { ReqBingo, ResBingo } from "../shared/protocols/PtlBingo";
import { ReqEmail } from "../monopoly/protocols/PtlEmail";
import { EmailFun } from "../public/email";

export default async function (call: ApiCall<ReqBingo, ResBingo>) {
    // TODO
    if (G.config.debug == false) return call.succ('fail');

    let shell = call.req.split(' ');

    if (shell[0] == 'all') {

        if (shell[1] == 'item') {
            if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);

            await PlayerFun.addItem(call, Object.keys(G.gc.item).map((v, index) => {
                return {
                    a: 'item',
                    t: v,
                    n: parseInt(shell[2]),
                };
            }));
        } else if (shell[1] == 'hero') {
            if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);

            await PlayerFun.addHero(call, Object.keys(G.gc.hero).map(v => {
                return {
                    a: 'item',
                    t: v,
                    n: parseInt(shell[2]),
                };
            }));
        } else if (shell[1] == 'equip') {
            if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);

            await PlayerFun.addEquip(call, Object.keys(G.gc.equip).map(v => {
                return {
                    a: 'equip',
                    t: v,
                    n: parseInt(shell[2]),
                };
            }));
        } else if (shell[1] == 'shiwu') {
            if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);
            if (isNaN(parseInt(shell[3]))) return call.error(`请输入正确的品质`);

            await PlayerFun.addShiwu(call, Object.keys(G.gc.shiwu).map(v => {
                return {
                    a: 'shiwu',
                    t: v,
                    n: parseInt(shell[2]),
                    colour: parseInt(shell[3])
                };
            }));
        } else if (shell[1] == 'peijian') {
            if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);

            await PlayerFun.addPeijian(call, Object.keys(G.gc.peijian).map(v => {
                return {
                    a: 'peijian',
                    t: v,
                    n: parseInt(shell[2]),
                };
            }));
        }

    } else if (shell[0] == 'item') {

        if (!G.gc.item[shell[1]]) return call.error(`道具id不存在：${shell[1]}`);
        if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);
        await PlayerFun.addItem(call, [{ a: 'item', t: shell[1], n: parseInt(shell[2]) }]);

    } else if (shell[0] == 'hero') {

        if (!G.gc.hero[shell[1]]) return call.error(`英雄id不存在：${shell[1]}`);
        if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);
        await PlayerFun.addHero(call, [{ a: 'item', t: shell[1], n: parseInt(shell[2]) }]);

    } else if (shell[0] == 'equip') {

        if (!G.gc.equip[shell[1]]) return call.error(`装备id不存在：${shell[1]}`);
        if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);
        await PlayerFun.addEquip(call, [{ a: 'equip', t: shell[1], n: parseInt(shell[2]) }]);

    } else if (shell[0] == 'shiwu') {

        if (!G.gc.shiwu[shell[1]]) return call.error(`饰物id不存在：${shell[1]}`);
        if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);
        if (isNaN(parseInt(shell[3]))) return call.error(`请输入正确的品质`);
        await PlayerFun.addShiwu(call, [{ a: 'shiwu', t: shell[1], n: parseInt(shell[2]), colour: parseInt(shell[3]) }]);

    } else if (shell[0] == 'peijian') {

        if (!G.gc.peijian[shell[1]]) return call.error(`配件id不存在：${shell[1]}`);
        if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);
        await PlayerFun.addPeijian(call, [{ a: 'peijian', t: shell[1], n: parseInt(shell[2]) }]);

    } else if (shell[0] == 'attr') {
        if (G.gc.attr[shell[1]] == undefined) return call.error(`道具id不存在：${shell[1]}`);
        if (isNaN(parseInt(shell[2]))) return call.error(`请输入正确的数量`);
        await PlayerFun.addAttr(call, [{ a: 'attr', t: shell[1], n: parseInt(shell[2]) }]);

    } else if (shell.length == 2) {

        let gud = Object.assign(UserFun.create(''), defaultUserAppend);
        if (gud[shell[0]] != undefined) {
            let obj = {};
            if (typeof gud[shell[0]] == 'string') {
                obj[shell[0]] = shell[1];
            } else if (typeof gud[shell[0]] == 'number' && !isNaN(parseInt(shell[1]))) {
                obj[shell[0]] = parseInt(shell[1]);
            } else {
                return;
            }
            await PlayerFun.addAttr(call, obj);
        }

    } else if (shell[0] == 'heroMaxLv') {

        let s = await G.mongodb.collection('hero').find({
            uid: call.uid
        }).toArray();
        let heros = s.map(h => G.mongodb.conversionIdObj(h));

        for (let [_id, hero] of Object.entries(heros)) {
            await HeroFun.changeHeroAttr(call, hero, {
                lv: Object.keys(G.gc.playerLv).length * 3,
                jieji: Object.keys(G.gc.herogrow[hero.heroId]).length - 1
            });
        }
    } else if (shell[0] == 'email') {
        let emailMsg: ReqEmail = {
            uid: call.uid,
            type: 'system',
            title: shell[1],
            content: shell[2]
        }
        if (shell[3]) emailMsg.prize = JSON.parse(shell[3])
        EmailFun.addEmail(emailMsg);
    } else if (shell[0] == "heroskin") {
        PlayerFun.sendPrize(call, [{ a: "heroskin", "t": shell[1], n: 1 }]);
    }

    call.succ('succ');
}