import {ApiCall} from "tsrpc";
import {HuoDongFun} from "../../../public/huodongfun";
import {ReqReceive, ResReceive} from "../../../shared/protocols/event/leichonglibao/PtlReceive";
import {PlayerFun} from "../../../public/player";
import {PublicShared} from "../../../shared/public/public";

export type LeiChongLiBaoData = {
    intr: string
    intr2: string
    payRewardNum: number
    dlz: { [key: string]: atn }[]
    gift: {
        id: string, name: string, need: atn[], free: boolean, payId: string, scale: number, buynum: number, prize: atn[]
    }[]
}
export default async function (call: ApiCall<ReqReceive, ResReceive>) {
    let hdinfo = await HuoDongFun.gethdList(call, 12);

    if (hdinfo.length <= 0) {
        // 无此活动
        return call.error('', {code: -1, message: globalThis.lng.huodong_open_1})
    }

    let hddata = hdinfo.filter(i => i.hdid == call.req.hdid)[0].data as LeiChongLiBaoData;

    let data = await G.mongodb.cEvent(`leichonglibao${call.req.hdid}`).findOne(
        {type: `leichonglibao${call.req.hdid}`, uid: call.uid}
    )

    if (data.sc) {
        // 自选奖励已领取
        return call.error('', {code: -1, message: globalThis.lng.task_finsh_3})
    }

    if (data.buy.length < hddata.payRewardNum) {
        // 累计购买礼包次数不足
        return call.error("", {code: -1, message: globalThis.lng.ljlibaotips_8})
    }

    let _prize: atn[] = [];
    for (let i = 0; i < hddata.dlz.length; i++) {
        _prize.push(hddata.dlz[i][call.req.select[i] || "1"])
    }

    // 修改领取标识
    await G.mongodb.cEvent(`leichonglibao${call.req.hdid}`).updateOne(
        {type: `leichonglibao${call.req.hdid}`, uid: call.uid}, {$set: {sc: true}}
    )

    PlayerFun.sendPrize(call, _prize)
    call.succ({prize: _prize})
}

export async function LeiChongLiBaoBuyGift(call: ApiCall, payId: string) {
    let hdinfo = await HuoDongFun.gethdList(call, 12);

    if (hdinfo.length <= 0) {
        console.log(call.uid, "购买累充礼包 但是活动没有开启", payId);
        return
    }

    let gift: LeiChongLiBaoData["gift"][0];

    let hdid = hdinfo[0].hdid;
    let hddata = hdinfo[0].data as LeiChongLiBaoData;
    for (let g of hddata.gift) {
        if (g.payId == payId) {
            gift = g;
            break;
        }
    }

    if (!gift) return;  // 不是购买累充礼包

    let mydata = await G.mongodb.cEvent(`leichonglibao${hdid}`).findOne(
        {type: `leichonglibao${hdid}`, uid: call.uid}
    )

    if (mydata && mydata.buy.includes(gift.payId)) {
        console.log(call.uid, "购买累充礼包 重复购买", payId);
        return
    }

    let update;
    if (mydata) {
        update = {$push: {buy: payId}}
    } else {
        update = {$set: {opentime: G.time, sc: false, buy: [payId]}}
    }

    PlayerFun.sendPrize(call, gift.prize)
    G.mongodb.cEvent(`leichonglibao${hdid}`).updateOne(
        {uid: call.uid, type: `leichonglibao${hdid}`}, update
    )
}

export async function LeiChongLiBaoGetHongDian(call: ApiCall) {
    let hdinfo = await HuoDongFun.gethdList(call, 12);

    if (hdinfo.length <= 0) {
        return {show: false}
    }

    let hdid = hdinfo[0].hdid;
    let hddata = hdinfo[0].data as LeiChongLiBaoData;

    let mydata = await G.mongodb.cEvent(`leichonglibao${hdid}`).findOne(
        {type: `leichonglibao${hdid}`, uid: call.uid}
    )

    if (!mydata || mydata.opentime < PublicShared.getToDayZeroTime()) {
        return {show: true}
    }

    if (!mydata.sc && mydata.buy.length >= hddata.payRewardNum) {
        return {show: true}
    }

    return {show: false}
}