import { ApiCall } from "tsrpc";
import { LingZhuLaiXifun } from "../../public/lingzhulaixi";
import { PlayerFun } from "../../public/player";
import { ReqSaoDang, ResSaoDang } from "../../shared/protocols/lingzhulaixi/PtlSaoDang";
import { PublicShared } from "../../shared/public/public";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqSaoDang, ResSaoDang>) {
    let _con = await LingZhuLaiXifun.getCon(call.req.bid)
    if (!_con) {
        // 参数错误，无相关配置
        return call.error('', { code: -1, message: globalThis.lng.lingzhulaixi_1 })
    }

    if (call.conn.gud.lv < _con.lv) {
        // 等级不足
        return call.error('', { code: -2, message: globalThis.lng.lingzhulaixi_2 })
    }

    // boss信息
    let _bossData = await LingZhuLaiXifun.getBossData(call.req.bid)
    if (_bossData.passtime != 0) {
        // boss 未复活
        return call.error('', { code: -3, message: globalThis.lng.lingzhulaixi_4 })
    }

    if (!_bossData.pkuser[call.uid]) {
        // 未挑战过，请先去挑战
        return call.error('', { code: -4, message: globalThis.lng.lingzhulaixi_5 })
    }

    let _myData = await LingZhuLaiXifun.getUsePKNum(call)
    let _maxNum = G.gc.com.PiratesFight_ChallengesNum.value
    let _logMaxDel = _myData.maxdps?.[_bossData.bid] || 0     // 历史最大伤害
    let _num = 0     // 实际扫荡次数
    let prize: atn[] = []
    for (let index = 0; index < call.req.num; index++) {
        _num += 1
        if (_myData.num + _num > _maxNum) {
            // 挑战次数不足直接跳出
            break
        }
        _bossData.delhp += _logMaxDel
        if (_bossData.delhp >= _bossData.maxhp) {
            // 击杀boss
            prize = prize.concat(await LingZhuLaiXifun.getPrize(_con.kill_drop))
            break
        } else {
            prize = prize.concat(await LingZhuLaiXifun.getPrize(_con.drop))
        }
        _bossData.pkuser[call.uid] += _logMaxDel
    }

    if (!prize[0]) {
        // 无奖励，没有挑战次数
        return call.error('', { code: -5, message: globalThis.lng.lingzhulaixi_6 })
    }

    let _setData   // 设置数据
    if (_bossData.delhp >= _bossData.maxhp) {
        _setData = {
            $set: {
                pkuser: {},
                delhp: 0,
                maxhp: await LingZhuLaiXifun.getBossMapHp(_bossData.bid),
                passtime: _con.member * 60 + G.time,
                randusernum: 0
            }
        }
        Object.assign(_bossData, _setData["$set"])
        // 清除玩家数据
        await G.mongodb.cPlayerInfo('lingzhulaixi').updateMany(
            { type: 'lingzhulaixi' },
            { $set: G.mongodb.createTreeObj({ key: `maxdps.${call.req.bid}`, val: 0 }) }
        )
    } else {
        _setData = {
            $set: {
                delhp: _bossData.delhp,
                pkuser: _bossData.pkuser

            }
        }
    }

    // 设置boss数据
    await LingZhuLaiXifun.setBossData(_bossData.bid, _setData)
    await LingZhuLaiXifun.setMyData(call, { $inc: { num: _num } })

    // 发奖
    prize = PublicShared.mergePrize(prize)
    await PlayerFun.sendPrize(call, prize)

    // 扣除挑战次数
    _myData.num += _num

    HongDianChange.sendChangeKey(call.uid, ['lingzhulaixihd'])

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