import {ApiCall} from "tsrpc";
import {WangZheRongYaofun} from "../../public/wzry";
import {ReqUpdateFight, ResUpdateFight} from "../../shared/protocols/wzry/PtlUpdateFight";
import {PublicShared} from "../../shared/public/public";

export default async function (call: ApiCall<ReqUpdateFight, ResUpdateFight>) {
    if (PublicShared.getToWeekMondayZeroTime() > G.time - 165600) {
        // 周三到周天可以更换
        return call.error('', {code: -1, message: ''/* globalThis.lng.wzry_17 */});
    }

    let canChangeZr = G.gc.wangzherongyao.wangzhe.noupdate
    let status = await WangZheRongYaofun.getWangZheStatus();
    // 每个状态开始的前5后5分钟 无法更换
    if ((canChangeZr.includes(status.status + 1) && G.time >= (status.etime - 5 * 60)) || (canChangeZr.includes(status.status - 1) && G.time <= (status.stime + 5 * 60))) {
        return call.error('', {code: -2, message: ''/* globalThis.lng.wzry_17 */});
    }

    let _wzFight = await WangZheRongYaofun.getMyFight(call);
    if (!_wzFight) {
        return call.error('', {code: -4, message: ''/* globalThis.lng.wzry_16 */});
    }

    let _setData = {
        jifen: _wzFight.jifen,
        zkey: _wzFight.zkey,
        data: await call.conn.getDefaultFightData(),    // 我的战斗信息
        uid: call.uid
    };
    await WangZheRongYaofun.setWzryDldFight(call, _setData);
    Object.assign(_wzFight, _setData);

    call.succ({
        // wzfight: _wzFight
    });
}