import { ApiCall } from "tsrpc";
import { PataFun } from '../../public/pata';
import { ReqGetPrize, ResGetPrize } from "../../shared/protocols/pata/PtlGetPrize";
import { PlayerFun } from '../../public/player';
import { HongDianChange } from "../hongdian/fun";
// import { paTaType } from '../../shared/protocols/pata/type';

export default async function (call: ApiCall<ReqGetPrize, ResGetPrize>) {
    let idx = call.req.idx;
    let con = G.gc.patacom

    /**参数错误 */
    if (!con[idx]) {
        return call.error(globalThis.lng.pata_getprize_1)
    }
    let changeInfo = await PataFun.getInfo(call.uid);

    /**未通关此关卡 */  
    if (changeInfo.lv <= idx) {
        return call.error(globalThis.lng.pata_getprize_2)
    }
    /**已经领取 */
    if (changeInfo.gotarr.indexOf(idx) != -1) {
        return call.error(globalThis.lng.pata_getprize_2)
    }

    // 设置数据
    let prize = []
    changeInfo.gotarr.push(idx)
    let setData = {gotarr: changeInfo.gotarr}
    await PataFun.changeInfo(call.uid, changeInfo.sid, { $set: setData });
    prize = await PlayerFun.sendPrize(call, con[idx].prize)

    let data = {
        mydata: changeInfo,
        prize: prize
    }
    HongDianChange.sendChangeKey(call.uid, ['patahd'])
    call.succ(data);
}