import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { SignFun } from "../../public/sign";
import { ReqGetBoxPrize, ResGetBoxPrize } from "../../shared/protocols/sign/PtlGetBoxPrize";
import { ctype } from "../../shared/protocols/sign/type";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqGetBoxPrize, ResGetBoxPrize>) {
    let key = call.req.key;
    let con = await SignFun.getBoxCon()

    /**参数错误 */
    if (!con[key]) {
        return call.error('', { code: -3, message: globalThis.lng.sign_boxprize_1 })
        // return call.error(globalThis.lng.sign_boxprize_1)
    }
    let mydata = await SignFun.getInfo(call.uid, {ctype: ctype});

    /**登录天数不足 */
    if (mydata["login"] < key) {
        return call.error('', { code: -1, message: globalThis.lng.sign_boxprize_2 })
        // return call.error(globalThis.lng.sign_boxprize_2)
    }
    /**已经领取 */
    if (mydata["boxgotarr"].indexOf(key) != -1) {
        return call.error('', { code: -2, message: globalThis.lng.sign_boxprize_3 })
        // return call.error(globalThis.lng.sign_boxprize_3)
    }

    // 设置数据
    let prize = []
    mydata["boxgotarr"].push(key)
    await SignFun.setInfo(call.uid, mydata);
    prize = await PlayerFun.sendPrize(call, con[key].prize)

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

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