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

function BattleControllerDungeonShards:getBoardConfig()
    return ConfigManager:getConfig("chapter_board_dungeon_shards")
end

function BattleControllerDungeonShards:getChapterConfig()
    return ConfigManager:getConfig("chapter_dungeon_shards")
end

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

function BattleControllerDungeonShards:controllBattleEnd()
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.DUNGEON_SHARDS,
        wave = self:getWaveIndex(),
        victory = self.victory,
    }
    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
    BIReport:postFightHeroReport(self.combatReport.battleType, atkReport)
    ModuleManager.DungeonManager:reqEndChallengeShards(self.chapterId, self.combatReport, self.taskProgress, totalDamage)
end

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

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

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

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

return BattleControllerDungeonShards