import { ApiCall } from "tsrpc";
import { PayFun } from '../../public/pay';
import { PlayerFun } from '../../public/player';
import { ReqHelp, ResHelp } from "../../shared/protocols/chat/PtlHelp";
import { PublicShared } from '../../shared/public/public';
import { getGud } from "../../public/gud";

export default async function (call: ApiCall<ReqHelp, ResHelp>) {
    call.req.helps = call.req.helps.filter(h => {
        return G.time >= PublicShared.getToDayZeroTime(h.cTime) && call.uid != h.uid;
    });
    // 'chat list is null'
    if (call.req.helps.length < 1) return call.error('', { code: -1, message: globalThis.lng.chat_1 });

    let chatList = await G.mongodb.collection('chat').findOne({ type: 'guild' + call.conn.gud.ghId });
    if (!chatList || chatList.list.length < 1) return call.error('', { code: -2, message: globalThis.lng.chat_1 });

    let list = [];
    let _isQz: 0 | 1 = 0
    chatList.list.remove(v => {
        // 只需要移除过期的即可 (不是当天的就是过期)
        if (PublicShared.getToDayZeroTime(v.time) == PublicShared.getToDayZeroTime(G.time)) {
            // 帮助次数不够
            _isQz = (v.sender.uid == call.uid && v.otherData.myhelpless >= call.req.helps.length) && _isQz == 0 ? 1 : _isQz
            list.push(v);
            return true;
        } else return false;
    });

    if (_isQz == 0) {
        // '请先求助或帮助次数不够'
        return call.error('', { code: -3, message: globalThis.lng.chat_2 });
    }

    // 新增条件，移除本玩家
    let listFilter = list.filter(li => li.otherData.helpUis.length < G.gc.shili_com.resHelp[li.otherData.helpIndex].num && !li.otherData.helpUis.includes(call.uid)
        && (li.sender.uid != call.uid));
    if (listFilter.length < 1) return call.error('', { code: -4, message: 'chat list is null' });

    let prize = [];
    let change = [];
    let need = [];

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

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

            // 扣除帮助次数
            if (li.sender.uid == call.uid) {
                let _lessNum = li.otherData.myhelpless - 1
                li.otherData.myhelpless = _lessNum < 0 ? 0 : _lessNum
            }
            // 修改被帮玩家数据 及 发奖
            if (li.sender.uid == element.uid) {
                change.push({
                    uid: li.sender.uid,
                    cTime: li.time,
                    pushUid: call.uid,
                });
                li.otherData.helpUis.push(call.uid);
                if (G.gc.shili_com.resHelp[li.otherData.helpIndex] <= li.otherData.helpUis.length) {
                    PlayerFun.sendPrize(PayFun.getCall(await getGud(li.sender.uid)), [G.gc.shili_com.resHelp[li.otherData.helpIndex].need]);
                }
                prize.push(...PublicShared.randomDropGroup(G.gc.shili_com.resHelp[li.otherData.helpIndex].helpPrizeDrop));
            }
        }
    }

    // 新改帮助无消耗
    // need = PublicShared.mergePrize(need);
    // await PlayerFun.checkNeedIsMeet(call, need);
    // await PlayerFun.cutNeed(call, need);

    prize = PublicShared.mergePrize(prize);
    await PlayerFun.sendPrize(call, prize);

    G.server.broadcastClusterMsg('msg_s2c/ChatHelp', { change: change }, {ghId:call.conn.gud.ghId});
    G.mongodb.collection('chat').updateOne({ type: 'guild' + call.conn.gud.ghId }, { $set: { list: list } });

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