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

local GET_PASS_INFO_INTERVAL = 600
-- 支线副本数据

function DungeonWeaponEntity:clear()
    DataManager:unregisterCrossDayFunc("DungeonWeaponEntity")
end

function DungeonWeaponEntity:init(data)
    self.maxPassedId = data.max_challenge_id or 0
    self.farmCount = data.farm_count or 0
    self.heroes = data.heroes or {}
    self.totalChallengeCount = data.total_challenge_count or {}
    self.data.isDirty = not self.data.isDirty

    self:updateGift(data.gift_info, true)
    DataManager.FormationData:initDungeonWeapon(self.heroes)

    DataManager:registerCrossDayFunc("DungeonWeaponEntity", function()
        self.farmCount = 0
        self:setDirty()
    end)
end

function DungeonWeaponEntity:refreshInfoOnSettlement(chapterId, result)
    if result.max_id > self.maxPassedId then
        ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_DUNGEON_WEAPON, result.max_id - self.maxPassedId)
    end
    self.totalChallengeCount[chapterId] = (self.totalChallengeCount[chapterId] or 0) + 1
    self.maxPassedId = result.max_id
    self:updateGift(result.gift_info)
    self:setDirty()
end

function DungeonWeaponEntity:refreshInfoOnFarm(result)
    self.farmCount = self.farmCount + 1
    self:updateGift(result.gift_info)
    self:setDirty()
end

-- 更新礼包状态
function DungeonWeaponEntity:updateGift(giftInfo, isInit)
    DataManager.ShopData:initGift(PayManager.PURCHARSE_ACT_TYPE.WEAPON_GIFT, giftInfo, isInit)
end

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

function DungeonWeaponEntity:getTotalChallengeCount()
    local count = 0
    for _, c in pairs(self.totalChallengeCount) do
        count = count + 1
    end
    return count
end

function DungeonWeaponEntity:getTodayChallengeCount()
    return 0
end

function DungeonWeaponEntity:getPassedMaxId()
    return self.maxPassedId
end

function DungeonWeaponEntity:getIsAllTimeOpen()
    return true
end

function DungeonWeaponEntity:getModuleKey()
    return ModuleManager.MODULE_KEY.DUNGEON_WEAPON
end

function DungeonWeaponEntity:getConfigName()
    return "chapter_dungeon_equip"
end

function DungeonWeaponEntity:getTitleString()
    return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_1)
end

function DungeonWeaponEntity:getRuleString()
    return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_2)
end

function DungeonWeaponEntity:getOpenWeekString()
    return I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_3)
end

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

function DungeonWeaponEntity:getOpenTextColor()
    return "#FFEDC5"
end

function DungeonWeaponEntity:getChallengeHpCost()
    return GFunc.getConstReward("dungeon_armor_limit")
end

function DungeonWeaponEntity:isNoTotalLimit()
    return true
end

function DungeonWeaponEntity:onClickFight()
    ModuleManager.DungeonWeaponManager:showMainUI()
end

function DungeonWeaponEntity:getDialyFarmLimit()
    return GFunc.getConstIntValue("dungeon_equip_limit")
end

function DungeonWeaponEntity:getFarmCount()
    return self.farmCount
end

function DungeonWeaponEntity:getRemianFarmCount()
    local count = self:getDialyFarmLimit() - self:getFarmCount()
    if count < 0 then
        count = 0
    end
    return count
end

function DungeonWeaponEntity:getTodayRemainLimitCount()
    return self:getRemianFarmCount()
end

function DungeonWeaponEntity:getLevelEnough(chapterId)
    local config = ConfigManager:getConfig(self:getConfigName())[chapterId]
    if not config then
        return false
    end
    if config.level > DataManager.PlayerData:getLv() then
        return false
    end

    return true
end

function DungeonWeaponEntity:canFightChapter(chapterId)
    if not chapterId then
        return false
    end

    if chapterId <= self.maxPassedId + 1 then
        if self:getLevelEnough(chapterId) then
            return true
        end
    end

    return false
end

function DungeonWeaponEntity:canFarmChapter(chapterId)
    if not chapterId then
        return false
    end

    return self.maxPassedId >= chapterId
end

function DungeonWeaponEntity:doCachePassInfo(chapterId, info)
    if not self.cachePassInfo then
        self.cachePassInfo = {}
    end
    if not info then
        return
    end
    if not self.cachePassInfo[chapterId] then
        self.cachePassInfo[chapterId] = {
            info = info,
            getTime = Time:getServerTime()
        }
    else
        self.cachePassInfo[chapterId].info = info
        self.cachePassInfo[chapterId].getTime = Time:getServerTime()
    end
    self:setDirty()
end

function DungeonWeaponEntity:getCachePassInfo(chapterId)
    if not self.cachePassInfo then
        self.cachePassInfo = {}
    end

    if not self.cachePassInfo[chapterId] then
        return
    end

    if self.cachePassInfo[chapterId].getTime + GET_PASS_INFO_INTERVAL < Time:getServerTime() then -- 过期
        return
    end

    return self.cachePassInfo[chapterId].info
end

function DungeonWeaponEntity:setCurFightChapterId(chapterId)
    self.curFightchapterId = chapterId or 1
end

function DungeonWeaponEntity:getCurFightChapterId()
    return self.curFightchapterId or 1
end

function DungeonWeaponEntity:getChapterFightCount(chapterId)
    return self.totalChallengeCount[chapterId] or 0
end

function DungeonWeaponEntity:getChallengeHpCost()
    return GFunc.getConstReward("dungeon_equip_cost")
end

return DungeonWeaponEntity