import {ApiCall} from "tsrpc";
import {ChatFun} from '../../public/chat';
import {GongHuiBaoZang} from '../../public/gonghuibaozang';
import {PlayerFun} from '../../public/player';
import {ReqLottery, ResLottery} from "../../shared/protocols/gonghuibaozang/PtlLottery";
import {PublicShared} from '../../shared/public/public';
import {HongDianChange} from "../hongdian/fun";

export default async function (call: ApiCall<ReqLottery, ResLottery>) {
    let need: atn[];
    let prize: atn[];
    // @ts-ignore
    need = G.gc.com.GuildDraw_Cos.value;
    // let need = [{ a: 'item', t: '17', n: 1 }]
    let diaoluoid = G.gc.com.GuildDraw_Fall.value;
    let _tmp;

    if (call.req.type == 1) {
        // 抽奖一次
        await PlayerFun.checkNeedIsMeet(call, need);
        prize = [];
        prize = prize.concat(PublicShared.randomDropGroup(diaoluoid, call.req.type));
        _tmp = {
            prize: prize,
            name: call.conn.gud.name,
            ctime: G.time,
            uid: call.uid
        };
        // await G.mongodb.collection('ghbzcjlog').insertOne(_tmp);
        GongHuiBaoZang.newMsg(_tmp);

    } else if (call.req.type == 10) {
        // 抽奖十次
        need = need.map(_n => {
            return {..._n, n: _n.n * call.req.type};
        });

        await PlayerFun.checkNeedIsMeet(call, need);

        prize = [];
        prize = prize.concat(PublicShared.randomDropGroup(diaoluoid, call.req.type));
        let _setData = [];
        for (let index = 0; index < call.req.type; index++) {
            const element = prize[index];
            _tmp = {
                prize: [element],
                name: call.conn.gud.name,
                ctime: G.time,
                uid: call.uid
            };
            _setData.push(_tmp);
        }
        GongHuiBaoZang.newMsg(_setData);

    }

    need && await PlayerFun.cutNeed(call, need);
    await PlayerFun.sendPrize(call, prize);

    // let playName;
    // if (prize[0].a == "item") {
    //     playName = G.gc.item[prize[0].t].name;
    // } else if (prize[0].a == "hero") {
    //     playName = G.gc.hero[prize[0].t].name;
    // } else if (prize[0].a == "equip") {
    //     playName = G.gc.equip[prize[0].t].name;
    // } else if (prize[0].a == "shiwu") {
    //     playName = G.gc.shiwu[prize[0].t].name;
    // } else {
    //     playName = "钻石";
    // }

    for (let index = 0; index < prize.length; index++) {
        const element = prize[index];

        if (element.a == "item" && G.gc.item[element.t].type == 3) {
            let playName = G.gc.item[element.t].name
            let playColour = G.gc.item[element.t].colour
            ChatFun.newMsg({
                type: 'local',
                msg: G.gc.pmd.guild_pmd,
                time: G.time,
                sender: 'system',
                otherData: {
                    pmd: true,
                    args: [call.conn.gud.name, playName, playColour]
                }
            });
        }
    }

    HongDianChange.sendChangeKey(call.uid, ['gonghuihd'])

    call.succ({
        prize: prize
    });
}