import { ApiCall } from "tsrpc";
import { PayFun } from '../../../public/pay';
import { PlayerFun } from '../../../public/player';
import { ReqReceive, ResReceive } from "../../../shared/protocols/event/jijin/PtlReceive";
import { PublicShared } from '../../../shared/public/public';
import { HongDianChange } from "../../hongdian/fun";
import { getVal } from './ApiOpen';

export default async function (call: ApiCall<ReqReceive, ResReceive>) {
    if (call.req.recIndexs.length < 1) return call.error(globalThis.lng.pata_getprize_1);
    let data = await G.mongodb.cEvent(call.req.type).findOne({ uid: call.uid, type: call.req.type });

    let conf = G.gc.jijin[call.req.type];

    let isErr = conf.map((c, i) => i).intersection(call.req.recIndexs).length != call.req.recIndexs.length;
    if (isErr) return call.error(globalThis.lng.pata_getprize_1);

    let payId = call.req.type == 'tianshujijin' ? 'zhongshenka' : call.req.type;
    let logs = await PayFun.getPayLog(call.uid,payId);
    let rec = data?.rec || {};
    let val = await getVal(call, logs);
    let isPay = logs?.length > 0;
    let prize: atn[] = [];

    for (let index of call.req.recIndexs) {
        if (val < conf[index].total) return call.error(globalThis.lng.event_jijin_1);

        // 配置获取奖励，判断是否存在
        let hasPt = conf[index].pt.length > 0;
        let hasTq = conf[index].tq.length > 0;
        // 奖励不存在或奖励已被领取
        let getPt = !hasPt || rec[index]?.pt == true;
        let getTq = !hasTq || rec[index]?.tq == true;
        // 非特权，普通奖励已经被领取，跳过当前循环
        if (getPt && !isPay) continue;
        // if (getPt && !getTq && !isPay) return call.error(globalThis.lng.event_jijin_2);
        // 特权，全部奖励已被领取，跳过当前循环
        if (getPt && getTq && isPay) continue;
        // if (getPt && getTq) return call.error(globalThis.lng.event_jijin_3);

        if (!rec[index]) rec[index] = { pt: false, tq: false };

        if (hasPt && !getPt) {
            prize.push(...conf[index].pt);
            rec[index].pt = true;
        }

        if (hasTq && !getTq && isPay) {
            prize.push(...conf[index].tq);
            rec[index].tq = true;
        }
    }
	// 没有领取到奖励，返回错误消息。
    if(prize.length == 0) return call.error(globalThis.lng.event_jijin_3)
    prize = PublicShared.mergePrize(prize);
    await PlayerFun.sendPrize(call, prize);

    await G.mongodb.cEvent(call.req.type).updateOne(
        { uid: call.uid, type: call.req.type },
        { $set: { rec: rec } },
        { upsert: true }
    );

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

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

    let ptLen = conf.filter(c => c.pt.length > 0).length;
    let tqLen = conf.filter(c => c.tq.length > 0).length;
    let recPtLen = Object.values(rec).filter(r => r.pt == true).length;
    let recTqLen = Object.values(rec).filter(r => r.tq == true).length;

    //奖励领取完 更新异步按钮
    if (recPtLen >= ptLen && recTqLen >= tqLen) {
        G.mongodb.collection('syncBtns').updateOne({ uid: call.uid }, { $set: G.mongodb.createTreeObj({ key: call.req.type, val: { active: false } }) });
    }
}