import { ApiCall } from "tsrpc";
import { PlayerFun } from "../../public/player";
import { WangZheRongYaofun } from "../../public/wzry";
import { ReqJingCai, ResJingCai } from "../../shared/protocols/wzry/PtlJingCai";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqJingCai, ResJingCai>) {
    let status = await WangZheRongYaofun.getWangZheStatus();
    if (status.status != 15) {
        // 未到时间
        return call.error('', { code: -1/* , message: globalThis.lng.wzry_11  */ });
    }

    let _con = G.gc.wangzherongyao.wangzhe.guess[call.req.guess];
    if (_con.needvip > call.conn.gud.vip) {
        // vip限制
        return call.error('', { code: -2/* , message: globalThis.lng.wzry_12 */ });
    }

    let _myData = await WangZheRongYaofun.getMyData(call);
    if (_myData?.isjingcai?.totalmoney) {
        // 已经竞猜
        return call.error('', { code: -3/* , message: globalThis.lng.wzry_13  */ });
    }
    let _jcList = await WangZheRongYaofun.getFightGroup(call, { deep: { $gte: 6 } });
    if (!_jcList) {
        // 未到时间
        return call.error('', { code: -1/* , message: globalThis.lng.wzry_11 */ });
    }
    let _jcuser = _jcList.filter(v => v.uid == call.req.touid);
    if (!_jcuser) {
        return call.error('', { code: -5/* , message: globalThis.lng.wzry_14 */ });
    }

    let need: atn[] = [{ "a": "attr", 't': "rmbmoney", "n": _con.addmoney }];
    need && await PlayerFun.cutNeed(call, need);
    let _setData = {
        isjingcai: {
            totalmoney: _con.addmoney,
            guessuid: call.req.touid
        }
    };
    Object.assign(_myData, _setData);

    // 设置竞猜数据
    await G.mongodb.cPlayerInfo("wzry").updateOne({ uid: call.uid, type: 'wzry' }, { $set: _setData });
    let _r = await G.clientCross.callApi('wzry/UpdateFight', { uid: call.uid, isjingcai: _con.addmoney });
    if (!_r.isSucc) {
        return call.error('', { code: -4/* , message: globalThis.lng.wzry_15  */ });
    }
    let prize = await PlayerFun.sendPrize(call, _con.prize);

    HongDianChange.sendChangeKey(call.uid, ['wzryhd']);

    call.succ({
        mydata: _myData,
        prize: prize
    });
}