import {ApiCall} from "tsrpc";
import {ReqOpen, ResOpen} from "../../shared/protocols/pushgift/PtlOpen";
import {PushGiftFun} from "../../public/pushgift";
import {Gift} from "../../module/collection_pushgift";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    let res: ResOpen = {gifts: []};

    // 过滤过期的礼包 和购买完毕的礼包
    (await PushGiftFun.getGift(call.uid)).forEach((gift) => {
        if (gift.passTime > G.time && chkBuyNum(gift.id, gift)) {
            res.gifts.push(gift)
        }
    })

    call.succ(res)
}

function chkBuyNum(gift_id: string, gift_info: Gift) {
    let conf = G.gc.tuisonglibao[gift_id];
    for (let i = 0; i < conf.payId.length; i++) {
        let pay = G.gc.pay[conf.payId[i]];
        let buy = gift_info.buy.map(i => i != 0);
        if (pay.buys > 0 && gift_info.buy[i] < pay.buys) {
            return true
        }
    }

    return false
}