local BattleController = require "app/module/battle/controller/battle_controller"
local BattleControllerBossRush = class("BattleControllerBossRush", BattleController)
local SIDE_ATK = GConst.BattleConst.SIDE_ATK
local MAX_LV = 41

function BattleControllerBossRush:getBoardConfig()
    return ConfigManager:getConfig("chapter_board_bossrush")
end

function BattleControllerBossRush:getChapterConfig()
    return ConfigManager:getConfig("activity_boss_rush_chapter")
end

---- 障碍格子图片
function BattleControllerBossRush:getBlockIcon()
    return "battle_hinder_4"
end

function BattleControllerBossRush:getChessBoardBgName()
    return "chessboard_1"
end

function BattleControllerBossRush:getScene()
    return "boss_rush"
end

function BattleControllerBossRush:getChapterId()
    return 1
end

function BattleControllerBossRush:getMaxWave()
    self:getInitBoard()
    return #self.monsterList
end

function BattleControllerBossRush:getInitBoard()
    if not self.boradList then
        self.boradList = {}
        self.fixedRandomGrid = {}
        self.mysteryBoxIndexMap = {}
        self.monsterList = {}

        local boardCfg = self:getBoardConfig()
        for _, id in ipairs(DataManager.ActBossRushData:getStageList()) do
            local info = self:getChapterConfig()[id]
            if info then
                for index, montserId in ipairs(info.monster) do
                    table.insert(self.monsterList, montserId)
                end
                local cfg = boardCfg[info.board]
                if cfg then
                    table.insert(self.boradList, {board = GFunc.getTable(cfg.board), gridEdge = GFunc.getTable(cfg.grid_edge)})
                end
            end
        end
    end

    return self.boradList, self.fixedRandomGrid, self.mysteryBoxIndexMap
end

function BattleControllerBossRush:onLoadComplete(...)
    -- 处理技能
    local unlockAllSkill = function(side)
        local skillPool = self.battleData:getSkillPool(side)
        if skillPool then
            for elementType, list in pairs(skillPool) do -- 先遍历一下未解锁的技能
                if not self.battleData:isUnlockedSkillElementType(elementType, side) then
                    local skillEntity = self.battleData:getSkillEntityByElement(elementType, side)
                    if skillEntity then
                        local skillId = skillEntity:getUnlockId()
                        if skillId then
                            self:dealSelectSkill(skillId, nil, side)
                        end
                    end
                end
            end
            self.battleUI:refreshSkill(nil, nil, side)
        end
    end
    unlockAllSkill(SIDE_ATK)

    BattleController.onLoadComplete(self, ...)
end

function BattleControllerBossRush:initOther()
    self.monsterAtkAddition1 = GFunc.getConstIntValue("activity_boss_rush_atk_add_1")
    self.monsterHpAddition1 = GFunc.getConstIntValue("activity_boss_rush_hp_add_1")
    self.monsterAtkAddition2 = GFunc.getConstIntValue("activity_boss_rush_atk_add_2")
    self.monsterHpAddition2 = GFunc.getConstIntValue("activity_boss_rush_hp_add_2")
    self.monsterAtkAddition3 = GFunc.getConstIntValue("activity_boss_rush_atk_add_3")
    self.monsterHpAddition3 = GFunc.getConstIntValue("activity_boss_rush_hp_add_3")
    self.battleData:setMaxBattleLv(MAX_LV)

    self.battleData:setTimeScaleBase(1.5, true) -- 设置倍速
end

-- 怪物攻击力加成
function BattleControllerBossRush:getMonsterAtkAddition()
    local waveIndex = self:getWaveIndex()
    local index = waveIndex
    local addtion = 0
    if not self.lastAtkWaveIndex ~= waveIndex then
        self.lastAtkWaveIndex = waveIndex
        index = waveIndex + 1
    else
        index = self.lastAtkWaveIndex
    end
    if index <= 20 then
        addtion = self.monsterAtkAddition1
    elseif index <= 50 then
        addtion = self.monsterAtkAddition2
    else
        addtion = self.monsterAtkAddition3
    end
    return addtion * index
end

-- 怪物血量加成
function BattleControllerBossRush:getMonsterHpAddition()
    local waveIndex = self:getWaveIndex()
    local index = waveIndex
    local addtion = 0
    if not self.lastHpWaveIndex ~= waveIndex then
        self.lastHpWaveIndex = waveIndex
        index = waveIndex + 1
    else
        index = self.lastHpWaveIndex
    end
    if index <= 20 then
        addtion = self.monsterHpAddition1
    elseif index <= 50 then
        addtion = self.monsterHpAddition2
    else
        addtion = self.monsterHpAddition3
    end
    return addtion * index
end

function BattleControllerBossRush:getNextMonsterId(waveIndex)
    self:getInitBoard()
    waveIndex = waveIndex or self:getWaveIndex() + 1
    return self.monsterList[waveIndex]
end

function BattleControllerBossRush:showBossEnterAni(bornTime, bossName, monsterComp, callback)
    if callback then
        callback()
    end
end

function BattleControllerBossRush:addBattleExp(...)
    if self.battleData:getBattleLv() >= MAX_LV then -- 只能升级60次
        return
    end

    BattleController.addBattleExp(self, ...)
end

function BattleControllerBossRush:controllBattleEnd()
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.ACT_BOSS_RUSH,
        wave = self:getWaveIndex(),
        victory = self.victory,
    }
    local atkReport = {}
    local teamEntity = self.battleData:getAtkTeam()
    local members = teamEntity:getAllMembers()
    local heroInfo = {}
    for k, v in pairs(members) do
        local report = {
            heroId = v:getId(),
            dmg = v:getDamageCount(),
        }
        table.insert(atkReport, report)
        heroInfo[v:getId()] = {
            skill_cast = v:getActiveSkillReleaseCount(),
            Damage = v:getDamageCount(),
        }
    end
    self.combatReport.atkReport = atkReport
    if not self.victory then
        self.combatReport.wave = self.combatReport.wave - 1
    end
    BIReport:postFightHeroReport(self.combatReport.battleType, atkReport)
    ModuleManager.ActBossRushManager:reqEndBattle(self.chapterId, self.combatReport, self.taskProgress)
end

function BattleControllerBossRush:postWaveOver(atkDead, isQuit)
    local deathType = BIReport.FIGHT_DEATH_TYPE.SURVIVE
    local waveEndType = BIReport.FIGHT_WAVE_END_TYPE.WIN
    if atkDead then
        if self.isBossWave then
            deathType = BIReport.FIGHT_DEATH_TYPE.BOSS_FAIL
        else
            deathType = BIReport.FIGHT_DEATH_TYPE.NORMAL_FAIL
        end
        waveEndType = BIReport.FIGHT_WAVE_END_TYPE.FAIL
    end

    if isQuit then
        waveEndType = BIReport.FIGHT_WAVE_END_TYPE.QUIT
    end

    local duration = self.waveDurationTime
    local totalTime = self.totalDurationTime
    local startTimes = DataManager.ActBossRushData:getTotalFightCount()
    local isFirstWin = false

    local isFianlStep = self:getWaveIndex() >= self.maxWaveIndex

    BIReport:postFightEnd(GConst.BattleConst.BATTLE_TYPE.ACT_BOSS_RUSH, self.battleData, self.chapterId, self:getWaveIndex(), duration, totalTime, self.eliminateCount, self.eliminateTotalCount, waveEndType, deathType, startTimes, isFirstWin, isFianlStep, self.maxLinkCount)
end

function BattleControllerBossRush:postFightStart()
    BIReport:postFightBegin(GConst.BattleConst.BATTLE_TYPE.ACT_BOSS_RUSH, self:getWaveIndex(), self.chapterId, nil, 1)
end

return BattleControllerBossRush