import { ApiCall } from "tsrpc";
import { ReqGetPrize, ResGetPrize } from "../../shared/protocols/sign/PtlGetPrize";
import { PlayerFun } from '../../public/player';
import { SignFun } from "../../public/sign";
import { ctype } from "../../shared/protocols/sign/type";
import { PublicShared } from "../../shared/public/public";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqGetPrize, ResGetPrize>) {
    let idx = call.req.idx;
    let _con = await SignFun.getCon()
    let changeInfo = await SignFun.getInfo(call.uid, { ctype: ctype });
    let con = _con[changeInfo["key"]]

    /**参数错误 */
    if (!con[idx]) {
        return call.error(globalThis.lng.sign_boxprize_1)
    }

    /**登录天数不足 */
    if (changeInfo["login"] < idx) {
        return call.error(globalThis.lng.sign_boxprize_2)
    }
    /**已经领取 */
    if (changeInfo["gotarr"].indexOf(idx) != -1) {
        return call.error(globalThis.lng.sign_boxprize_3)
    }

    // 设置数据
    changeInfo["gotarr"].push(idx)
    await SignFun.setInfo(call.uid, changeInfo);

    let prize = con[idx].prize
    if (con[idx].vip && call.conn.gud.vip >= con[idx].vip) {
        prize = PublicShared.mergePrize(prize.concat(con[idx].prize))
    }
    prize = await PlayerFun.sendPrize(call, prize)

    HongDianChange.sendChangeKey(call.uid, ['qiandaohd', 'taskhd', 'huodonghd'])

    let data = {
        mydata: changeInfo,
        prize: prize
    }
    call.succ(data);
}