import { ApiCall } from "tsrpc";
import { ReqTaskRec, ResTaskRec } from "../../../shared/protocols/event/christmas/PtlTaskRec";
import {Christmasfun} from "./fun";
import {HongDianChange} from "../../hongdian/fun";
import {PlayerFun} from "../../../public/player";

export default async function (call: ApiCall<ReqTaskRec, ResTaskRec>) {
    let initCon = await Christmasfun.getCon(call)
    let taskid = call.req.taskid;
    let _con = initCon[call.req.hdid].data.task[taskid];
    if (!_con) {
        // 任务id 不存在
        return call.error('', { code: -1, message: globalThis.lng.yangchengmubiao_2 })
    }
    let _mydata = await Christmasfun.getMyData(call, call.req.hdid)
    if (_mydata.taskval[taskid] < _con.pval) {
        // 任务未完成
        return call.error('', { code: -2, message: globalThis.lng.yangchengmubiao_3 })
    }
    if (_mydata.taskfinish.includes(taskid)) {
        // 任务已领取
        return call.error('', { code: -3, message: globalThis.lng.yangchengmubiao_4 })
    }
    _mydata.taskfinish.push(taskid)
    let _addval = _con.addval
    let _setData = {}
    _mydata["val"] += _addval
    _setData["val"] = _mydata.val
    _setData["taskfinish"] = _mydata.taskfinish
    await Christmasfun.setMyData(call.uid, call.req.hdid, { $set: _setData })
    let _prize = _con.prize

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


}