local BattleHelper = require "app/module/battle/helper/battle_helper"
local BattleController = require "app/module/battle/controller/battle_controller"
local BattleControllerDailyChallenge = class("BattleControllerDailyChallenge", BattleController)
local BattleBuffEntity = require "app/userdata/battle/skill/battle_buff_entity"

function BattleControllerDailyChallenge:getBoardConfig()
    return ConfigManager:getConfig("chapter_board_daily_challenge")
end

function BattleControllerDailyChallenge:getChapterConfig()
    return ConfigManager:getConfig("chapter_daily_challenge")
end

function BattleControllerDailyChallenge:getChapterId()
    return DataManager.DailyChallengeData:getChapterDailyId()
end

function BattleControllerDailyChallenge:getBuffs()
    if not self.initBuffs then
        self.initBuffs = {}
        local buffs = DataManager.DailyChallengeData:getTodayBuffIds()
        local cfg = ConfigManager:getConfig("buff_daily_challenge")
        for _, buffId in ipairs(buffs) do
            local effect = cfg[buffId] and cfg[buffId].effect
            if effect then
                local buffEntity = BattleBuffEntity:create()
                buffEntity:init(effect[1])
                buffEntity:setTargetSide(cfg[buffId].obj)
                buffEntity:setIsCantRemove(true)
                buffEntity:setNotShowIcon(true)

                table.insert(self.initBuffs, {buffEntity = buffEntity, effect = effect[1]})
            end
        end
    end

    return self.initBuffs
end

function BattleControllerDailyChallenge:onLoadComplete(...)
    BattleController.onLoadComplete(self, ...)
    ModuleManager.DailyChallengeManager:showBattleBuffUI()
end

function BattleControllerDailyChallenge:initOther()
    local chapterId = DataManager.DailyChallengeData:getFixedChapterId()
    local cfg = ConfigManager:getConfig("chapter")[chapterId]
    self.monsterAtkAddition = 0
    self.monsterHpAddition = 0
    if cfg and cfg.daily_challenge_difficulty then
        self.monsterAtkAddition = cfg.daily_challenge_difficulty[2]
        self.monsterHpAddition = cfg.daily_challenge_difficulty[1]
    end
end

-- 怪物攻击力加成
function BattleControllerDailyChallenge:getMonsterAtkAddition()
	return self.monsterAtkAddition
end

-- 怪物血量加成
function BattleControllerDailyChallenge:getMonsterHpAddition()
	return self.monsterHpAddition
end

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

        local config = self:getChapterConfig()[self.chapterId]
        local boardCfg = self:getBoardConfig()
        for _, boardId in ipairs(config.chapter_board_daily_challenge) do
            local cfg = boardCfg[boardId]
            if cfg then
                table.insert(self.boradList, {board = GFunc.getTable(cfg.board_daily_challenge), gridEdge = GFunc.getTable(cfg.grid_edge)})
            end
        end
    end

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

function BattleControllerDailyChallenge:controllBattleEnd()
    self.combatReport = {
        battleType = GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE,
        wave = self:getWaveIndex(),
        victory = self.victory,
    }
    local atkReport = {}
    local teamEntity = self.battleData:getAtkTeam()
    local members = teamEntity:getAllMembers()
    local heroInfo = {}
    for k, v in pairs(members) do
        local report = {
            heroId = v:getId(),
            dmg = v:getDamageCount(),
        }
        table.insert(atkReport, report)
        heroInfo[v:getId()] = {
            skill_cast = v:getActiveSkillReleaseCount(),
            Damage = v:getDamageCount(),
        }
    end
    self.combatReport.atkReport = atkReport
    if not self.victory then
        self.combatReport.wave = self.combatReport.wave - 1
    end
    local taskCurProgress = ModuleManager.DailyChallengeManager:getCurTaskProgress(self)
    BIReport:postFightHeroReport(self.combatReport.battleType, atkReport)
    ModuleManager.DailyChallengeManager:endChallenge(self.chapterId, self.combatReport, self.taskProgress, heroInfo, taskCurProgress)
end

function BattleControllerDailyChallenge: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.DailyChallengeData:getTotalFightCount()
    local isFirstWin = false -- TODO 策划说不需要 因为系数在变

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

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

function BattleControllerDailyChallenge:postFightStart()
    BIReport:postFightBegin(GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE, self:getWaveIndex(), self.chapterId, nil, 1)
end

return BattleControllerDailyChallenge