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

/**
 * 黑市每日礼包改版，此接口只领取免费礼包
 * @param call
 */
export default async function (call: ApiCall<ReqRec, ResRec>) {
    let conf = G.gc.dixiaheishi['136Gift'][call.req.index];
    if (!conf || conf.payId) return call.error('', {code: -1});
    // if (!buy60 && conf.payId) return call.error('', { code: -2 });

    let prize: prizeType[] = [];
    let pushIndex: number[] = [];
    let db = await G.mongodb.cEvent('136Gift').findOne({uid: call.uid, type: '136Gift'});

    if (db.recIndex.includes(call.req.index)) return call.error('', {code: -3});
    prize.push(...(conf.payId ? G.gc.pay[conf.payId].prize : conf.prize));
    pushIndex.push(call.req.index);

    prize = PublicShared.mergePrize(prize);
    await PlayerFun.sendPrize(call, prize);

    G.mongodb.cEvent('136Gift').updateOne(
        {uid: call.uid, type: '136Gift'},
        {$push: {recIndex: {$each: pushIndex}}}
    );

    // HongDianChange.sendChangeKey(call.uid, ['heishihd', 'heishiMrjx'])
    HongDianChange.sendChangeKey(call.uid, ['heishihd'])

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