import { ApiCall } from "tsrpc";
import { PayFun } from "../../public/pay";
import { PlayerFun } from "../../public/player";
import { ReqRenownGetPrize, ResRenownGetPrize } from "../../shared/protocols/user/PtlRenownGetPrize";
import { PublicShared } from "../../shared/public/public";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqRenownGetPrize, ResRenownGetPrize>) {
    let conf = G.gc.mw_com['gift'][0];
    let buyLog = await PayFun.getPayLog(call.uid, conf.payId);
    if (!conf || buyLog.length <= 0) return call.error('', { code: -1, message: globalThis.lng.user_7 });

    let _mydata = {
        // 礼包相关数据
        record: {},
        // 领取时间
        tequan: {
            istq: buyLog.length > 0 ? 1 : 0,
            recTime: 0
        }
    }

    _mydata = await G.mongodb.collection('playerInfo', 'juewei').findOne({ uid: call.conn.uid, type: 'juewei' }) || _mydata;
    if (PublicShared.getToDayZeroTime(_mydata.tequan.recTime) == PublicShared.getToDayZeroTime(G.time)) {
        return call.error('', { code: -2, message: globalThis.lng.tanxian_6 });
    }

    let _sendPrize = G.gc.mw_com.giftdayprize.prize
    _mydata.tequan.recTime = G.time

    // 设置数据和发奖
    G.mongodb.collection('playerInfo', 'juewei').updateOne({ uid: call.conn.uid, type: 'juewei' }, { $set: { "tequan.recTime": _mydata.tequan.recTime } });
    await PlayerFun.sendPrize(call, _sendPrize);

    HongDianChange.sendChangeKey(call.uid, ['renown']);

    call.succ({
        prize: _sendPrize,
        mydata: _mydata
    });
}