import { OptionalId, UpdateFilter } from 'mongodb';
import { userInfo } from 'os';
import { ApiCall } from 'tsrpc';
import { formatNpcData } from '../shared/fightControl/fightFun';
import { lingzhulaixi_BossList, lingzhulaixi_BOSSmmMoRenZhi, lingzhulaixi_CiSHU, ResOpen } from '../shared/protocols/lingzhulaixi/PtlOpen';
import { prizeType } from '../shared/protocols/type';
import { PublicShared } from '../shared/public/public';
import { PayFun } from './pay';
import { PlayerFun } from './player';


export class LingZhuLaiXifun {
    /**领主来袭配置 */
    static async getCon(bid: string) {
        let _con = G.gc.PiratesFight[bid]
        return _con
    }

    /**获取boss最大血量 */
    static async getBossMapHp(bid: string) {
        let _con = await this.getCon(bid)
        let _bossid = _con.army
        let _res = await formatNpcData(_bossid)
        let _keys = Object.keys(_res.roles)
        let maxHp: number = 0
        for (let index = 0; index < Object.keys(_res.roles).length; index++) {
            const element = _keys[index];
            maxHp += _res.roles[element].attr.hp
        }
        return maxHp
    }

    /**获取boss初始信息 */
    static async getBossDataDef(bid: string) {
        let _res: lingzhulaixi_BOSSmmMoRenZhi = {
            bid: bid,
            ctime: G.time,
            passtime: 0,
            maxhp: await this.getBossMapHp(bid),
            pkuser: {},
            delhp: 0,
            randusernum: 0
        }
        return _res
    }

    /**设置boss相关信息 */
    static async setBossData(bid: string, data: {}) {
        await G.mongodb.collection('lingzhulaixi').updateOne({ bid: bid }, data, { upsert: true });
    }
    /**设置玩家相关信息 */
    static async setMyData(call: ApiCall, data: {}) {
        await G.mongodb.collection('playerInfo', 'lingzhulaixi').updateOne({ uid: call.uid, type: 'lingzhulaixi' }, data, { upsert: true });
    }

    /**获取boss信息 */
    static async getBossData(bid: string) {
        let _bossInfo = await G.mongodb.collection('lingzhulaixi').findOne({ bid: bid });
        let _res: lingzhulaixi_BOSSmmMoRenZhi
        if (!_bossInfo || (_bossInfo.passtime != 0 && G.time > _bossInfo.passtime)) {
            _res = await this.getBossDataDef(bid)
            await this.setBossData(bid, { $set: _res })
        } else {
            delete _bossInfo._id
            _res = _bossInfo
        }
        return _res
    }

    /**获取玩家已经使用的挑战次数 以及挑战的最大伤害 */
    static async getUsePKNum(call: ApiCall) {
        let _myData = await G.mongodb.collection('playerInfo', 'lingzhulaixi').findOne({ uid: call.conn.uid, type: 'lingzhulaixi' })
        let _res: lingzhulaixi_CiSHU
        // 隔天清零
        if (_myData && PublicShared.chkSameDate(_myData.time, G.time)) {
            _res = {
                num: _myData.num || 0,
                time: _myData.time || G.time,
                maxdps: _myData.maxdps || {}
            }
        } else {
            _res = {
                num: 0,
                time: G.time,
                maxdps: _myData?.maxdps || {}
            }
        }
        return _res
    }

    /**格式化boss列表信息 */
    static async fmtBossList(call: ApiCall) {
        let _bossData = await G.mongodb.collection('lingzhulaixi').find({}).toArray()
        let _res: lingzhulaixi_BossList = {}
        for (let index = 0; index < _bossData.length; index++) {
            const element = _bossData[index];
            if (element.passtime != 0 && element.passtime < G.time) {
                continue
            }
            let _tmp = {
                bid: element.bid,
                passtime: element.passtime,
                maxhp: element.maxhp,
                delhp: element.delhp,
                pkusernum: Object.keys(element.pkuser).length + element.randusernum,
            }
            _res[element.bid] = _tmp

        }
        return _res
    }

    /**格式化boss列表信息 */
    static async getBossFightInof(bossFightid: number | string, hpjc: number) {
        let _res = await formatNpcData(bossFightid)
        let _keys = Object.keys(_res.roles)
        if (hpjc >= 1) return _res
        for (let index = 0; index < Object.keys(_res.roles).length; index++) {
            const element = _keys[index];
            // @ts-ignore
            let _tmpHp = parseInt(hpjc * _res.roles[element].attr.hp)
            // @ts-ignore
            // let _tmpMaxHp = parseInt(hpjc * _res.roles[element].attr.maxHp)
            _res.roles[element].attr.hp = _tmpHp
            // _res.roles[element].attr.maxHp = _tmpMaxHp
        }
        return _res
    }

    /**获取掉落组的奖励 */
    static async getPrize(dlz: number[]) {
        let prize = [];         // 发送的奖励
        for (let index = 0; index < dlz.length; index++) {
            const element = dlz[index];
            prize = prize.concat(PublicShared.randomDropGroup(element, 1));
        }
        return prize
    }

    /**排序 */
    static async getRankList(pkuser: { [uid: string]: number }) {
        let list = Object.keys(pkuser)
        let fmtpkuser = []
        for (let index = 0; index < list.length; index++) {
            const element = pkuser[list[index]]
            let d = {
                uid: list[index],
                maxdps: element
            }
            fmtpkuser.push(d)
        }
        // fmtpkuser.sort((a, b) => a.maxdps - b.maxdps)
        fmtpkuser.sort((a, b) => b.maxdps - a.maxdps)
        return fmtpkuser
    }

    /**红点 */
    static async getHongDian(call: ApiCall) {
        let _res = {
            show: false
        }
        let _con = await this.getCon("1")
        if (call.conn.gud.lv < _con.lv) return _res
        let _myData = await this.getUsePKNum(call)
        let _maxNum = G.gc.com.PiratesFight_ChallengesNum.value
        if (_myData.num < _maxNum) {
            _res.show = true
        }
        return _res
    }


}