local BattleControllerPVP = require "app/module/battle/controller/battle_controller_pvp"
local BattleControllerArena = class("BattleControllerArena", BattleControllerPVP)
local BattleBuffEntity = require "app/userdata/battle/skill/battle_buff_entity"

function BattleControllerArena:getBoardConfig()
    return ConfigManager:getConfig("arena_board")
end

function BattleControllerArena:getChapterConfig()
    return ConfigManager:getConfig("arena_rank")
end

function BattleControllerArena:getChapterId()
    return DataManager.ArenaData:getGradingId()
end

function BattleControllerArena:getBuffs()
    if not self.initBuffs then
        self.initBuffs = {}
        local buffList = {}
        local config = self:getChapterConfig()[self.chapterId]
        if config and config.effect then
            for _, effect in ipairs(config.effect) do
                local atkBuffInfo = {
                    buff = GFunc.getTable(effect),
                    obj = GConst.BattleConst.SIDE_ATK
                }
                local defBuffInfo = {
                    buff = GFunc.getTable(effect),
                    obj = GConst.BattleConst.SIDE_DEF
                }
                table.insert(buffList, atkBuffInfo)
                table.insert(buffList, defBuffInfo)
            end
        end
        for _, info in ipairs(buffList) do
            local buffEntity = BattleBuffEntity:create()
            buffEntity:init(info.buff)
            buffEntity:setTargetSide(info.obj)
            buffEntity:setIsCantRemove(true)
            buffEntity:setNotShowIcon(true)

            table.insert(self.initBuffs, {buffEntity = buffEntity, effect = info.buff})
        end
    end

    return self.initBuffs
end


-- 战斗结束
function BattleControllerArena:controllBattleEnd()
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.ARENA,
        wave = self:getWaveIndex(),
        victory = self.victory,
        round = self.waveRoundCount[self:getWaveIndex()] or 0
    }
    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

    local defReport = {}
    local teamEntity = self.battleData:getDefTeam()
    local members = teamEntity:getAllMembers()
    for k, v in pairs(members) do
        local report = {
            heroId = v:getId(),
            dmg = v:getDamageCount(),
        }
        table.insert(defReport, report)
    end
    self.combatReport.defReport = defReport
    local win = self.victory
    BIReport:postFightHeroReport(self.combatReport.battleType, atkReport)
    ModuleManager.ArenaManager:reqSettlement(win, self.combatReport, self.taskProgress)
end

-- 一共有多少波
function BattleControllerArena:getMaxWave()
    return 1
end

function BattleControllerArena:getMaxRoundCount()
    if not self.arenaMaxRound then
        self.arenaMaxRound = GFunc.getConstIntValue("arena_max_round")
    end
    return self.arenaMaxRound
end

function BattleControllerArena:findNextDefUnit()
    self:enterRoundEnd(true)
end

function BattleControllerArena: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.ArenaData:getTotalFightCount()
    local isFirstWin = false
    local isFianlStep = self:getWaveIndex() >= self.maxWaveIndex

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

function BattleControllerArena:postFightStart()
    local startTimes = DataManager.ArenaData:getTotalFightCount()
    BIReport:postFightBegin(GConst.BattleConst.BATTLE_TYPE.ARENA, self:getWaveIndex(), self.chapterId, self.chapterId, startTimes)
end

function BattleControllerArena:getAtkMinRow()
    return GConst.BattleConst.PVP_ROW_COUNT // 2
end

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

        local config = self:getChapterConfig()[self.chapterId]
        local boardCfg = self:getBoardConfig()
        local boardId = config.rand_board[math.random(1, #config.rand_board)]
        local cfg = boardCfg[boardId]
        if cfg then
            local board = GFunc.getTable(cfg.board)
            for index, info in ipairs(board) do
                if info[2] == GConst.BattleConst.ELEMENT_TYPE.NONE then
                    board[index][2] = math.random(1, GConst.BattleConst.ELEMENT_TYPE_COUNT)
                end
            end
            table.insert(self.boradList, {board = board, mysteryBoard = GFunc.getTable(cfg.mystery_box_board), gridEdge = GFunc.getTable(cfg.grid_edge)})
            table.insert(self.fixedRandomGrid, GFunc.getTable(cfg.control_element))
        end
    end

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

function BattleControllerArena:onTouchEvent(eventType, posId, isVirtual)
    local isAtkAction = self.curActionSide == GConst.BattleConst.SIDE_ATK
    local r = ModuleManager.BattleManager:getPosRC(posId).r
    if isAtkAction then
        if r <= self:getAtkMinRow() then
            return
        end
    else
        if r > self:getAtkMinRow() then
            return
        end
    end
    BattleControllerPVP.onTouchEvent(self, eventType, posId, isVirtual)
end

return BattleControllerArena