import {ApiCall} from "tsrpc";
import {ReqOpen, ResOpen} from "../../../shared/protocols/hbzb/zbs/PtlOpen";
import {PublicShared} from '../../../shared/public/public';
import {number, string} from "mathjs";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    if (G.time < PublicShared.getToWeekMondayZeroTime() + G.gc.hbzb.zbsOpenTime[0]) return call.error(globalThis.lng.hbzb_11);

    let req =(await G.clientCross.callApi('hbzb/zbs/GetStatus', {uid: call.uid}));
    let status = req?.res?.status;
    //没有资格
    if (typeof status == 'number' && status == -999999) return call.errorCode(-1);

    let data = await G.mongodb.cPlayerInfo('hbzbZbs').findOne({uid: call.uid, type: 'hbzbZbs'}) as any;
    if(data){
        data = data?.data;
    }
    if (!data || !data?.refreshTime || data?.refreshTime < PublicShared.getToDayZeroTime()) {
        data = {};
        data.winNum = 0;
        data.buyFightNum = 0;
        data.useFightNum = 0;
        data.refreshTime = G.time;
        data.winRecPrize = [];

        let callRes = await G.clientCross.callApi('hbzb/zbs/GetEnemy', {uid: call.uid});
        data.enemy = callRes.res.enemy;

        G.mongodb.cPlayerInfo('hbzbZbs').updateOne(
            {uid: call.uid, type: 'hbzbZbs'},
            {$set: {data: data}},
            {upsert: true}
        );
    }

    data.rank = (await G.crossmongodb.collection("hbzb_user_cross").findOne({ uid: call.uid }))?.rank || -999999;
    call.succ(data);
}

export function checkIsOpen() {
    let weekZeroTime = PublicShared.getToWeekMondayZeroTime();
    return G.time >= weekZeroTime + G.gc.hbzb.zbsOpenTime[0] && G.time <= weekZeroTime + G.gc.hbzb.zbsOpenTime[1];
}