import { ApiCall } from "tsrpc";
import { PayFun } from '../../../public/pay';
import { PlayerFun } from '../../../public/player';
import { ReqRec, ResRec } from "../../../shared/protocols/event/qirichongzhi/PtlRec";
import { HongDianChange } from "../../hongdian/fun";

export default async function (call: ApiCall<ReqRec, ResRec>) {
    let db = await G.mongodb.cEvent('qirichongzhi').findOne({ uid: call.uid, type: 'qirichongzhi' });
    let round = db?.round || 0;
    let conf = G.gc.qirichongzhi[round];

    if (!db) return call.error('', { code: 0 });
    if (!conf.tasks[call.req.index]) return call.error('', { code: -1 });
    if (db.recIndexs.includes(call.req.index)) return call.error('', { code: -2 });

    let finished = await PayFun.getPayDaysAllPayNum(call.uid, db.sTime, db.sTime + G.gc.qirichongzhi[round].time);
    if (finished < conf.tasks[call.req.index].total) return call.error('', { code: -3 });

    await PlayerFun.sendPrize(call, conf.tasks[call.req.index].prize);

    G.mongodb.cEvent('qirichongzhi').updateOne(
        { uid: call.uid, type: 'qirichongzhi' },
        { $push: { recIndexs: call.req.index } }
    );

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

    call.succ({
        prize: conf.tasks[call.req.index].prize
    });
}