local BattleController = require "app/module/battle/controller/battle_controller"
local BattleControllerDungeonGold = class("BattleControllerDungeonGold", BattleController)

function BattleControllerDungeonGold:getBoardConfig()
    return ConfigManager:getConfig("chapter_board_dungeon_gold")
end

function BattleControllerDungeonGold:getChapterConfig()
    return ConfigManager:getConfig("chapter_dungeon_gold")
end

function BattleControllerDungeonGold:getChapterId()
    return DataManager.DungeonData:getCurFightChapterId()
end

function BattleControllerDungeonGold:refreshWave()
end

function BattleControllerDungeonGold:enterRoundBegin(...)
    local nextWaveRound = 0
    if self.dungeonGoldMaxRoundCount then
        nextWaveRound = (self.waveRoundCount[self:getWaveIndex()] or 0) + 1
        if self.dungeonGoldMaxRoundCount < nextWaveRound then -- 超过最大回合, 直接结算
            self.victory = false
            self:postWaveOver(false)
            self:battleEnd()
            return
        end
    end
    if self.battleUI then
        self.battleUI:refreshWave(self.dungeonGoldMaxRoundCount - nextWaveRound + 1, GConst.ATLAS_PATH.COMMON, "common_dec_15")
    end
    BattleController.enterRoundBegin(self, ...)
end

-- 不同模块的战斗需要初始化的东西
function BattleControllerDungeonGold:initOther()
    self.dungeonGoldMaxRoundCount = self:getChapterConfig()[self.chapterId].wave_limit or 1

    if self.battleUI then
        self.battleUI:refreshWave(self.dungeonGoldMaxRoundCount, GConst.ATLAS_PATH.COMMON, "common_dec_15")
    end
end

function BattleControllerDungeonGold:controllBattleEnd()
    local remainRound = self.dungeonGoldMaxRoundCount - (self.waveRoundCount[1] or 0)
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.DUNGEON_GOLD,
        wave = self:getWaveIndex(),
        victory = self.victory,
        remainRound = remainRound,
    }
    local atkReport = {}
    local teamEntity = self.battleData:getAtkTeam()
    local members = teamEntity:getAllMembers()
    local totalDamage = 0
    for k, v in pairs(members) do
        local report = {
            heroId = v:getId(),
            dmg = v:getDamageCount(),
        }
        totalDamage = totalDamage + v:getDamageCount()
        table.insert(atkReport, report)
    end
    self.combatReport.atkReport = atkReport
    if not self.victory then
        self.combatReport.wave = self.combatReport.wave - 1
    end
    local remainingHp = self.defTeam:getMainUnit().unitEntity:getHp()
    BIReport:postFightHeroReport(self.combatReport.battleType, atkReport)
    ModuleManager.DungeonManager:reqEndChallengeGold(self.chapterId, self.combatReport, self.taskProgress, totalDamage, remainingHp)
end

function BattleControllerDungeonGold: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.DungeonData:getTotalCount(ModuleManager.MODULE_KEY.DUNGEON_GOLD, self.chapterId)
    local isFirstWin = false
    if DataManager.DungeonData:getPassedMaxId(ModuleManager.MODULE_KEY.DUNGEON_GOLD) < self.chapterId and self.victory then
        isFirstWin = true
    end

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

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

function BattleControllerDungeonGold:postFightStart()
    local unlockMaxChapter = DataManager.DungeonData:getUnlockMaxId(ModuleManager.MODULE_KEY.DUNGEON_GOLD)
    BIReport:postFightBegin(GConst.BattleConst.BATTLE_TYPE.DUNGEON_GOLD, self:getWaveIndex(), self.chapterId, unlockMaxChapter, DataManager.DungeonData:getTotalCount(ModuleManager.MODULE_KEY.DUNGEON_GOLD, self.chapterId))
end

return BattleControllerDungeonGold