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

export default async function (call: ApiCall<ReqTaskRec, ResTaskRec>) {
    let _hd = await Christmasfun.getCon(call)
    let taskid = call.req.taskid;
    let _con = _hd.data.task[taskid];
    if (!_con) {
        // 任务id 不存在
        return call.error('', { code: -1, message: globalThis.lng.yangchengmubiao_2 })
    }
    let _mydata = await Yuandanfun.getData(call, _hd.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 _setData = {}
    _setData["taskfinish"] = _mydata.taskfinish
    await Yuandanfun.setData(call.uid, _hd.hdid, { $set: _setData })
    let _prize = _con.prize

    await PlayerFun.sendPrize(call, _prize);
    let changedata = { data: _mydata, prize: _prize}

    call.succ(changedata);

    // 推送红点
    HongDianChange.sendChangeKey(call.uid, ['huodonghd']);
}