local DungeonBaseEntity = require "app/userdata/dungeon/dungeon_base_entity"
local DungeonRuneEntity = class("DungeonRuneEntity", DungeonBaseEntity)

local TASK_TYPE = GConst.DungeonRuneConst.TASK_TYPE

function DungeonRuneEntity:ctor()
    self.data.isDirty = false
    self.maxPassedId = 0
    self.runeInfo = {}
end

function DungeonRuneEntity:clear()
    self.data.isDirty = false
    DataManager:unregisterCrossDayFunc("DungeonRuneEntity")
end

function DungeonRuneEntity:init(data)
    if EDITOR_MODE then
        Logger.logHighlight("-----DungeonRuneEntity------")
        Logger.printTable(data)
    end

    self.totalChallengeCount = data.total_challenge_count or 0
    self.fightCountMap = data.stat_counts or {}
    self.maxPassedId = data.max_challenge_id and (data.max_challenge_id - 1) or 0
    self.runeInfo = data.turns or {}
    self.heroes = data.heroes or {}
    self.buySilverCount = data.buy_silver_count or 0

    DataManager.FormationData:initFormationByType(GConst.BattleConst.FORMATION_TYPE.DUNGEON_RUNE, self.heroes)
    DataManager:registerCrossDayFunc("DungeonRuneEntity", function()
        self.buySilverCount = 0
        self:setDirty()
    end)
end

function DungeonRuneEntity:setDirty()
    self.data.isDirty = not self.data.isDirty
end

function DungeonRuneEntity:getModuleKey()
    return ModuleManager.MODULE_KEY.RUNES_OPEN
end

function DungeonRuneEntity:getConfig(chapterId)
    return ConfigManager:getConfig(self:getConfigName())[chapterId]
end

function DungeonRuneEntity:getConfigName()
    return "chapter_dungeon_rune"
end

function DungeonRuneEntity:getTitleString()
    return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_TITLE)
end

function DungeonRuneEntity:getRuleString()
    return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_HELP)
end

function DungeonRuneEntity:getOpenWeekString()
    return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC)
end

function DungeonRuneEntity:getBanner()
    return "assets/arts/textures/background/dungeon/dungeon_bg_5.png"
end

function DungeonRuneEntity:getOpenTextColor()
    return "#FFFFFF"
end

function DungeonRuneEntity:isNoTotalLimit()
    return true
end

function DungeonRuneEntity:isNotShowLimitCount()
    return true
end

function DungeonRuneEntity:getIsAllTimeOpen()
    return true
end

function DungeonRuneEntity:onClickFight()
    ModuleManager.DungeonRuneManager:showMainUI()
end

function DungeonRuneEntity:getTodayRemainLimitCount()
    return DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_GLOD_WING)
end

function DungeonRuneEntity:getRebirthAddRoundCount()
    if not self.rebirthAddRoundCount then
        self.rebirthAddRoundCount = GFunc.getConstIntValue("dungeon_rune_revival")
    end
    return self.rebirthAddRoundCount
end

function DungeonRuneEntity:getBattleMaxlv()
    if not self.battleMaxLv then
        self.battleMaxLv = GFunc.getConstIntValue("dungeon_rune_lvlimit")
    end
    return self.battleMaxLv
end

function DungeonRuneEntity:getBuySliverWingCost()
    if not self.buySliverWingCost then
        self.buySliverWingCost = GFunc.getConstReward("dungeon_rune_cost")
    end
    return self.buySliverWingCost
end

function DungeonRuneEntity:getPassedMaxId()
    return self.maxPassedId
end

function DungeonRuneEntity:updatePassedMaxId(maxId)
    if not maxId then
        return
    end
    self.maxPassedId = math.max(maxId - 1, 1)
end

function DungeonRuneEntity:canSweep(id)
    if id > self.maxPassedId then
        return false
    end
    if not self:isBossChapter(id) then
        return false
    end
    return DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_SLIVER_WING) > 0
end

function DungeonRuneEntity:canFight(id)
    if self.maxPassedId + 1 >= id then
        return true
    end
    return false
end

function DungeonRuneEntity:passedChapter(id)
    return self.maxPassedId >= id
end

function DungeonRuneEntity:curFight(id)
    return id == self.maxPassedId + 1
end

function DungeonRuneEntity:getChapterPassRound(id)
    if not self.runeInfo[id] then
        return 0
    end
    return self.runeInfo[id] or 0
end

function DungeonRuneEntity:updatePassRound(id, round)
    local cur = self.runeInfo[id]
    if not cur or cur > round then
        self.runeInfo[id] = round
        self:tagRoundNew()
    end
end

function DungeonRuneEntity:tagRoundNew()
    self.tagNew = true
end

function DungeonRuneEntity:getTagRoundNew()
    return self.tagNew
end

function DungeonRuneEntity:getChapterRewards(id)
    return self:getConfig(id).first_reward
end

function DungeonRuneEntity:getChapterSweepRewards(id)
    return self:getConfig(id).sweep_reward
end

function DungeonRuneEntity:isBossChapter(id)
    local cfg = self:getConfig(id)
    return not cfg.monster[2] -- 只有一个怪物的就是boss关卡
end

function DungeonRuneEntity:getChapterCondition(id)
    local cfg = self:getConfig(id)
    if not self.cacheChaperCondition then
        self.cacheChaperCondition = {}
    end

    if not self.cacheChaperCondition[id] then
        self.cacheChaperCondition[id] = {}
        table.insert(self.cacheChaperCondition[id], {0, cfg.round, 0})
        if cfg.requirement then
            for _, condition in ipairs(cfg.requirement) do
                table.insert(self.cacheChaperCondition[id], GFunc.getTable(condition))
            end
        end
    end

    return self.cacheChaperCondition[id]
end

function DungeonRuneEntity:getConditionIcon(taskInfo)
    local iconSprite
    local iconConst = GConst.DungeonRuneConst.TASK_ICON[taskInfo[1]]
    if type(iconConst) == "table" then
        iconSprite = iconConst[taskInfo[2]]
    else
        iconSprite = iconConst
    end

    return GConst.ATLAS_PATH.UI_DUNGEON_RUNE, iconSprite
end

function DungeonRuneEntity:getConditionDesc(taskInfo, taskNum)
    local taskType = taskInfo[1]
    local taskParams1 = taskInfo[2]
    local taskParams2 = taskInfo[3]
    if taskType == TASK_TYPE.PASS_ROUND then
        if taskNum and taskNum > 0 then
            taskParams1 = taskNum
        end
        return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_1, taskParams1)
    elseif taskType == TASK_TYPE.ELIMINATION_ELEMENT then
        if taskNum and taskNum > 0 then
            local num = taskParams2 - taskNum
            if num > 0 then
                taskParams2 = num
            end
        end
        local desc = ModuleManager.HeroManager:getMatchTypeName(taskParams1, true)
        return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_2, taskParams2, desc)
    elseif taskType == TASK_TYPE.BREAK_GRID_TYPE then
        if taskNum and taskNum > 0 then
            local num = taskParams2 - taskNum
            if num > 0 then
                taskParams2 = num
            end
        end
        return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_3, taskParams2)
    elseif taskType == TASK_TYPE.KILL_MONSTER then
        if taskNum and taskNum > 0 then
            local num = taskParams2 - taskNum
            if num > 0 then
                taskParams2 = num
            end
        end
        return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_QUEST_4, taskParams2)
    end
end

function DungeonRuneEntity:getChapterMonsterSpine(id)
    local cfg = self:getConfig(id)
    local monsterId = cfg.monster[1]
    local info = ConfigManager:getConfig("monster")[monsterId]
    return info.model_id, info.model_ui
end

function DungeonRuneEntity:getChapterMonsterI18N(id)
    local cfg = self:getConfig(id)
    local monsterId = cfg.monster[1]
    local info = ConfigManager:getConfig("monster")[monsterId]
    local monsterBase = info.monster_base
    return I18N:getConfig("monster_base")[monsterBase]
end

function DungeonRuneEntity:getCurFightChapterId()
    return self.curFightChapterId or 1
end

function DungeonRuneEntity:setCurFightChapterId(chapterId)
    self.curFightChapterId = chapterId
end

function DungeonRuneEntity:getChapterFightCount(id)
    return self.fightCountMap[id] or 0
end

function DungeonRuneEntity:getSliverWingBuyCount()
    return self.buySilverCount or 0
end

function DungeonRuneEntity:addSliverWingBuyCount(count)
    self.buySilverCount = self.buySilverCount + count
end

function DungeonRuneEntity:getRemainSliverWingCount()
    if not self.todayLimitSliverCount then
        self.todayLimitSliverCount = GFunc.getConstIntValue("dungeon_rune_buylimit")
    end
    local count = self.todayLimitSliverCount - self:getSliverWingBuyCount()
    if count <= 0 then
        count = 0
    end
    return count
end

function DungeonRuneEntity:getBuySliverCost()
    if not self.todayBuySliverCost then
        self.todayBuySliverCost = GFunc.getConstReward("dungeon_rune_cost")
    end

    return self.todayBuySliverCost
end

return DungeonRuneEntity