import { OptionalId, UpdateFilter } from 'mongodb';
import { ApiCall } from "tsrpc";
import { FightFun } from '../../public/fight';
import { KuangDongfun } from '../../public/kuangdong';
import { ReqKdInfo, ResKdInfo } from '../../shared/protocols/kuangdong/PtlKdInfo';
import { KuangDongKaiCaiListTmp } from '../../shared/protocols/kuangdong/PtlOpen';
import { getGud } from '../../public/gud';

export default async function (call: ApiCall<ReqKdInfo, ResKdInfo>) {
    let hdid = call.req.hdid
    let con = await KuangDongfun.getCon(hdid)

    let kdInfo
    if (con.public_map == 1){
        kdInfo = await KuangDongfun.getKaiCaiInfo(hdid)
    }else {
        kdInfo = await KuangDongfun.getKaiCaiInfo(hdid, call.uid)
    }

    if (!kdInfo) {
        return call.error('', { code: -1, message: globalThis.lng.kudangdong_1 })
    }

    kdInfo._id = kdInfo._id.toString()
    let userinfo: KuangDongKaiCaiListTmp
    if (kdInfo) {
        let _tmp: KuangDongKaiCaiListTmp
        //let _gud = await G.mongodb.collection('user').findOne({ uid: kdInfo.uid })
        let _gud = await getGud(kdInfo.uid)
        _tmp = {
            name: _gud.name,
            lv: _gud.lv,
            power: _gud.power,
            ghId: _gud.ghId,
            ghName: _gud.ghName,
            head: _gud.head,
            headFrame: _gud.headFrame,
        }
        userinfo = _tmp
    }
    let changeData = {
        kdinfo: kdInfo
    }

    if (userinfo) {
        changeData["userinfo"] = userinfo
    }

    call.succ(changeData);
}