import { ApiCall } from "tsrpc";
import { ReqZhanLingRec, ResZhanLingRec } from "../../../shared/protocols/event/christmas/PtlZhanLingRec";
import {Christmasfun} from "./fun";
import {PlayerFun} from "../../../public/player";
import {HongDianChange} from "../../hongdian/fun";

export default async function (call: ApiCall<ReqZhanLingRec, ResZhanLingRec>) {
    let initCon = await Christmasfun.getCon(call)
    let index = call.req.index;
    let _con = initCon[call.req.hdid].data.zhanling[index];
    if (!_con) {
        // 礼包id 不存在
        return call.error('', { code: -1, message: globalThis.lng.yangchengmubiao_2 })
    }
    let _mydata = await Christmasfun.getMyData(call, call.req.hdid)
    if (_mydata.val < _con.val) {
        return call.error('', { code: -1, message: globalThis.lng.yangchengmubiao_2 })
    }
    let _prize = []

    if (!_mydata.pt.includes(index)){
        // 如果普通奖励没有领取

         _prize = _prize.concat(_con.pt)
        _mydata.pt.push(index)
    }
    if (_mydata.pay && !_mydata.gj.includes(index)){
        // 如果普通奖励没有领取
        _prize = _prize.concat(_con.gj)

        _mydata.gj.push(index)
    }
    if (_prize.length <= 0) {
        // 没有奖励可以领取
        return call.error('', { code: -1, message: globalThis.lng.yangchengmubiao_2 })
    }
    let _setData = {}
    _setData["gj"] = _mydata.gj
    _setData["pt"] = _mydata.pt
    await Christmasfun.setMyData(call.uid, call.req.hdid, { $set: _setData })

    await PlayerFun.sendPrize(call, _prize);
    let changedata = { mydata: _mydata, prize: _prize}
    // 推送红点
    HongDianChange.sendChangeKey(call.uid, ['huodonghd']);
    call.succ(changedata);

}