import { OptionalId, UpdateFilter } from 'mongodb';
import { ApiCall } from "tsrpc";
import { CollectionPlayerInfo } from '../../module/collection_palyerInfo';
import { PlayerFun } from '../../public/player';
import { ReqLottery, ResLottery } from "../../shared/protocols/jiaotang/PtlLottery";
import { ResOpen } from '../../shared/protocols/jiaotang/PtlOpen';
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[];
    let result = await G.mongodb.collection('playerInfo', 'jiaotang').findOne({ uid: call.uid, type: 'jiaotang' });
    let update: UpdateFilter<OptionalId<CollectionPlayerInfo<"jiaotang">>>;
    let changeData: Partial<ResOpen>;

    if (call.req.type == 1) {
        let isReplace = false;
        let conf = G.gc.choujiang.jiaotang[1];
        let toDayZeroTime = PublicShared.getToDayZeroTime();
        let oneReplaceNum = result?.oneReplaceNum || 0;

        if (toDayZeroTime <= result?.freeTime) {
            let meet = await PlayerFun.checkNeedByArgs(call, conf.need1, conf.need2);
            need = [meet.atn];
        }

        prize = [];
        prize = prize.concat(PublicShared.randomDropGroup(oneReplaceNum == conf.replace.num ? conf.replace.diaoluo : result?.oneNum ? conf.firstdiaoluo : conf.diaoluo));

        for (let atn of prize) {
            let atnConf = PublicShared.getAtnConf(atn) as any;
            if (atnConf.colour >= conf.replace.colour) {
                isReplace = true;
                break;
            }
        }

        update = {
            $inc: { oneNum: 1 },
            $set: { oneReplaceNum: isReplace ? 0 : oneReplaceNum + 1 }
        };

        changeData = {
            oneNum: (result?.oneNum || 0) + 1
        };

        if (!result || !result.freeTime || result.freeTime < toDayZeroTime) {
            Object.assign(update.$set, { freeTime: G.time });
            Object.assign(changeData, { freeTime: G.time });
        }
    } else if (call.req.type == 10) {
        let conf = G.gc.choujiang.jiaotang[2];

        let meet = await PlayerFun.checkNeedByArgs(call, conf.need1, conf.need2);
        need = [meet.atn];

        let dlArr = result?.tenNum ? conf.firstdiaoluo : conf.diaoluo;

        dlArr.sort(() => { return 0.5 - Math.random(); });
        prize = [];
        for (let id of dlArr) {
            prize = prize.concat(PublicShared.randomDropGroup(id));
        }

        update = {
            $inc: { tenNum: 1 }
        };

        changeData = {
            tenNum: (result?.tenNum || 0) + 1
        };
    }

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

    G.mongodb.collection('playerInfo', 'jiaotang').updateOne(
        {
            uid: call.uid,
            type: 'jiaotang'
        },
        {
            ...update
        },
        {
            upsert: true
        }
    );
    HongDianChange.sendChangeKey(call.uid, ['jiaotang', 'taskhd', 'huodonghd']);

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