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

function BattleControllerStage:getBoardConfig()
    return ConfigManager:getConfig("chapter_board")
end

function BattleControllerStage:getChapterConfig()
    return ConfigManager:getConfig("chapter")
end

function BattleControllerStage:getChapterId()
    return DataManager.ChapterData:getChapterId()
end

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

        local config = self:getChapterConfig()[self.chapterId]
        local boardCfg = self:getBoardConfig()
        for idx, boardId in ipairs(config.board) do
            local cfg = boardCfg[boardId]
            if cfg then
				local tab = {
					board = GFunc.getTable(cfg.board),
					mysteryBoard = GFunc.getTable(cfg.mystery_box_board),
					gridEdge = GFunc.getTable(cfg.grid_edge)
				}
				if config.mystery_box and config.mystery_box_reward and config.mystery_box[idx] and math.random(1, GConst.BattleConst.DEFAULT_FACTOR) <= config.mystery_box[idx] then
					self.mysteryBoxIndexMap[idx] = GFunc.randomDrop(config.mystery_box_reward)
				end
                table.insert(self.boradList, tab)
                table.insert(self.fixedRandomGrid, GFunc.getTable(cfg.control_element))
            end
        end

        -- if config.mystery_box and config.mystery_box_reward then
        --     for i, index in ipairs(config.mystery_box) do
        --         if not DataManager.ChapterData:getChapterMysteryBoxIsGot(self.chapterId, i) then
        --             self.mysteryBoxIndexMap[index] = config.mystery_box_reward[i]
        --         end
        --     end
        -- end
    end

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

function BattleControllerStage:getFixedRogueSkill()
    if not self.fixedRogueSkill then
        local config = ConfigManager:getConfig("chapter")[self.chapterId]
        if not DataManager.ChapterData:getChapterPassed(self.chapterId) and config.involved_skill then
            self.fixedRogueSkill = GFunc.getTable(config.involved_skill)
        else
            self.fixedRogueSkill = {}
        end
    end

    return self.fixedRogueSkill
end

function BattleControllerStage:controllBattleEnd()
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.STAGE,
        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.ChapterManager:endFight(self.chapterId, self.combatReport, self.gotMysteryBoxIndexs, self.taskProgress)
end

function BattleControllerStage: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.ChapterData:getChapterFightCount(self.chapterId)
    local isFirstWin = false
    if not DataManager.ChapterData:getChapterPassed(self.chapterId) and self.victory then
        isFirstWin = true
    end

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

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

function BattleControllerStage:postFightStart()
    local unlockMaxChapter = DataManager.ChapterData:getNewChapterId()
    BIReport:postFightBegin(GConst.BattleConst.BATTLE_TYPE.STAGE, self:getWaveIndex(), self.chapterId, unlockMaxChapter, DataManager.ChapterData:getChapterFightCount(self.chapterId))
end

return BattleControllerStage