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

function BattleControllerDungeonWeapon:getBoardConfig()
    return ConfigManager:getConfig("chapter_board_dungeon_equip")
end

function BattleControllerDungeonWeapon:getChapterConfig()
    return ConfigManager:getConfig("chapter_dungeon_equip")
end

function BattleControllerDungeonWeapon:getChapterId()
    local weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
    return weaponData:getCurFightChapterId()
end

function BattleControllerDungeonWeapon:controllBattleEnd()
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.DUNGEON_WEAPON,
        wave = self:getWaveIndex(),
        victory = self.victory,
    }
    local atkReport = {}
    local teamEntity = self.battleData:getAtkTeam()
    local members = teamEntity:getAllMembers()
    for k, v in pairs(members) do
        local report = {
            heroId = v:getId(),
            dmg = 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.DungeonWeaponManager:reqEndChallenge(self.chapterId, self.combatReport, self.taskProgress)
end

function BattleControllerDungeonWeapon: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 weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
    local startTimes = weaponData:getChapterFightCount(self.chapterId)
    local isFirstWin = false
    if weaponData:getPassedMaxId() < self.chapterId and self.victory then
        isFirstWin = true
    end

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

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

function BattleControllerDungeonWeapon:postFightStart()
    local weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
    local startTimes = weaponData:getChapterFightCount(self.chapterId)
    local unlockMaxChapter = weaponData:getPassedMaxId() + 1
    BIReport:postFightBegin(GConst.BattleConst.BATTLE_TYPE.DUNGEON_WEAPON, self:getWaveIndex(), self.chapterId, unlockMaxChapter, startTimes)
end

return BattleControllerDungeonWeapon