import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../../public/player';
import { ReqRefresh, ResRefresh } from "../../../shared/protocols/hbzb/jfs/PtlRefresh";
import { checkIsOpen } from './ApiOpen';

export default async function (call: ApiCall<ReqRefresh, ResRefresh>) {
    if (!checkIsOpen()) return call.error(globalThis.lng.hbzb_7);
    let dbData = await G.mongodb.cPlayerInfo('hbzb').findOne({ uid: call.uid, type: 'hbzb' });
    let refreshNum = dbData.data.useRefreshNum;

    if (refreshNum >= G.gc.hbzb.jfsRefreshNum) {
        await PlayerFun.checkNeedIsMeet(call, G.gc.hbzb.jfsRefreshNeed);
        await PlayerFun.cutNeed(call, G.gc.hbzb.jfsRefreshNeed);
    }

    let change: ResRefresh['change'] = {};
    change.useRefreshNum = dbData.data.useRefreshNum + 1;

    let callRes = await G.clientCross.callApi('hbzb/jfs/GetEnemy', { uid: call.uid, auto: false, gud: call.conn.gud});
    change.enemy = callRes.res.enemy.map(e => { return { ...e, result: null }; });

    await G.mongodb.cPlayerInfo('hbzb').updateOne(
        { uid: call.uid, type: 'hbzb' },
        { $set: { 'data.enemy': change.enemy, 'data.useRefreshNum': change.useRefreshNum } }
    );

    call.succ({
        change: change
    });
}