import { ApiCall } from "tsrpc";
import { ReqWzFightData, ResWzFightData } from "../../cross/protocols/wzry/PtlWzFightData";
import { Rank } from "../../public/rank/rank";
import { PublicShared } from "../../shared/public/public";
import { getPlayerRank } from "./ApiRank";

export default async function (call: ApiCall<ReqWzFightData, ResWzFightData>) {
    // 王者参与数据
    let changeInfo: ResWzFightData = {}
    if (call.req.iswzfight) {
        let _myFight = await G.mongodb.collection('wzry_fight').findOne({ uid: call.req.uid })
        if (_myFight && Object.keys(_myFight).length > 0) {
            if (_myFight?._id) delete _myFight._id
            _myFight['rank'] = await getPlayerRank(call.req.uid) || -1
            changeInfo['info'] = _myFight
        }
    }

    // 王者之巅本期数据
    if (call.req.iswzzd) {
        let zkey = PublicShared.getToWeek()
        let _wzzd = await G.mongodb.collection('wzrywiner').findOne({ zkey: zkey })
        if (_wzzd && _wzzd._id) delete _wzzd._id
        changeInfo['wzzd'] = _wzzd
    }

    if (call.req.isdel) {
        // 删除本期数据
        await G.mongodb.collection('wzry_fight').deleteMany({})
        await G.mongodb.collection('crosskv').deleteMany({ type: 'wzryallnum' })
        await G.mongodb.collection('wzry_user_cross').deleteMany({})
        await G.mongodb.collection('wzry_fight_log').deleteMany({})
        await Rank.list.wzryCross.clear()
    }

    // 跨服邮件
    if (call.req.isemail) {
        let _crossEmail = await G.mongodb.collection('email').find({ isdel: { $exists: false } }).toArray()
        _crossEmail.forEach(email => {
            email._id.toString()
        })
        changeInfo['crossEmail'] = _crossEmail
    }

    call.succ(changeInfo)
}