local DungeonGoldData = class("DungeonGoldData", BaseData)

local FIRST_CHAPTER_ID = 1001

function DungeonGoldData:ctor()
    self.data.isDirty = false
    self.data.maxPassed = 0
    self.data.times = 0
    self.data.challengeId = 0
end

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

function DungeonGoldData:init(data)
    data = data or GConst.EMPTY_TABLE
    self.data.maxPassed = data.max_passed or 0 -- 最大通关的副本id
    self.data.itemTimes = data.item_times or 0 -- 今日道具挑战次数
    if self.data.maxPassed <= 0 then -- 一关都没打过
        self.data.challengeId = FIRST_CHAPTER_ID
    else
        local info = self:getConfig()[self.data.maxPassed]
        if info then
            local nextId = info.next_chapter
            if nextId then
                self.data.challengeId = nextId
            else
                self.data.challengeId = self.data.maxPassed
            end
        else
            self.data.maxPassed = 0
            self.data.challengeId = FIRST_CHAPTER_ID
        end
    end
    self.data.times = data.times or 0 -- 今日剩余挑战次数
end

function DungeonGoldData:isOpen(showToast)
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.DUNGEON_GOLD, not showToast) then
        return false
    end
    return true
end

function DungeonGoldData:getModuleKey()
    return ModuleManager.MODULE_KEY.DUNGEON_GOLD
end

function DungeonGoldData:getConfig()
    if self._chapterCungeonCfg == nil then
        self._chapterCungeonCfg = ConfigManager:getConfig("chapter_dungeon")
    end
    return self._chapterCungeonCfg
end

function DungeonGoldData:getAllChaptersInfo()
    if self._allChaptersInfo == nil then
        self._allChaptersInfo = {}
        local cfg = ConfigManager:getConfig("chapter_dungeon")
        for k, v in pairs(cfg) do
            if v.type == 1 then
                table.insert(self._allChaptersInfo, {
                    id = k,
                    cfg = v
                })
            end
        end
        table.sort(self._allChaptersInfo, function (a, b)
            return a.id < b.id
        end)
    end
    return self._allChaptersInfo
end

--只显示已通关 + 目前通关往后的十个
function DungeonGoldData:getLimitChaptersInfo()
    local list = self:getAllChaptersInfo()
    local limitList = {}
    for i,v in ipairs(list) do
        if self.data.maxPassed + 10 >= v.id or i <= 10 then
            table.insert(limitList, v)
        end
    end
    return limitList
end

function DungeonGoldData:getChapterInfo(index)
    return self:getAllChaptersInfo()[index]
end

function DungeonGoldData:getMaxPassedId()
    return self.data.maxPassed
end

function DungeonGoldData:getChallengeId()
    return self.data.challengeId
end

function DungeonGoldData:getTodayMaxTimes()
    if self._dungeonGoldTimes == nil then
        self._dungeonGoldTimes = GFunc.getConstIntValue("dungeon_gold_times")
    end
    if DataManager.PrivilegeCardData:getIsChallegeCardActive() then
        return self._dungeonGoldTimes + self:getPrivilegeCardAddTimes()
    else
        return self._dungeonGoldTimes
    end
end

function DungeonGoldData:getPrivilegeCardAddTimes()
    if self._privilegeCardAddTimes == nil then
        self._privilegeCardAddTimes = GFunc.getConstIntValue("card_fight_gold")
    end
    return self._privilegeCardAddTimes
end

function DungeonGoldData:getTodayRemainTimes()
    local maxTimes = self:getTodayMaxTimes()
    local times = maxTimes - self.data.times
    if times < 0 then
        times = 0
    end
    return times
end

function DungeonGoldData:getChapterName(chapterId)
    return I18N:getConfig("chapter_dungeon")[chapterId].name
end

function DungeonGoldData:onFightDungeonDailyFinish(chapterId, victory)
    if not victory then
        return
    end
    if self.data.maxPassed < chapterId then
        local info = self:getConfig()[chapterId]
        if info then
            self.data.maxPassed = chapterId
            local nextId = info.next_chapter
            if nextId then
                self.data.challengeId = nextId
            else
                self.data.challengeId = self.data.maxPassed
            end
        end
    end
    self.data.times = self.data.times + 1
    self:setDirty()
end

function DungeonGoldData:onSweepDungeonDailyFinish()
    self.data.times = self.data.times + 1
    self:setDirty()
end

function DungeonGoldData:getIsShowRedPoint()
    if not self:isOpen() then
        return false
    end
    return self:getTodayRemainTimes() > 0
end

function DungeonGoldData:onCrossDay()
    self.data.times = 0
    self:setDirty()
end

return DungeonGoldData