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

function BattleControllerActpvp:getBoardConfig()
    return ConfigManager:getConfig("activity_pvp_board")
end

function BattleControllerActpvp:getChapterConfig()
    return ConfigManager:getConfig("activity_pvp_board")
end

function BattleControllerActpvp:getChapterId()
    return 1
end

function BattleControllerActpvp:getBlockIcon()
    return "battle_hinder_4"
end

function BattleControllerActpvp:getChessBoardBgName()
    return "chessboard_1"
end

function BattleControllerActpvp:getScene()
    return "activity_pvp"
end

function BattleControllerActpvp:getBuffs()
    if not self.initBuffs then
        self.initBuffs = {}
        local buffList = {}
        local effect = {
            {
                ["type"]="hpp_add",
                ["num"]=10000,
                ["ratio"]=10000,
                ["round"]=0
            },
            {
                ["type"]="end_dmg_dec_all_add",
                ["num"]=8000,
                ["ratio"]=10000,
                ["round"]=0
            }
        }
        for _, effect in ipairs(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
        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 BattleControllerActpvp:controllBattleEnd()
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.ACT_PVP,
        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.ActPvpManager:reqFightEnd(win, self.combatReport, self.taskProgress)
end

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

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

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

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

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

function BattleControllerActpvp:postFightStart()
    local startTimes = DataManager.ActPvpData:getTotalFightCount()
    BIReport:postFightBegin(GConst.BattleConst.BATTLE_TYPE.ACT_PVP, self:getWaveIndex(), self.chapterId, self.chapterId, startTimes)
end

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

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

        local boardCfg = self:getBoardConfig()
        local num = #boardCfg
        local boardId = math.random(1, num)
        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 BattleControllerActpvp: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 BattleControllerActpvp