local SummonData = class("SummonData", BaseData)

function SummonData:ctor()
	self.data.isDirty = false
end

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

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

function SummonData:initCrossDay()
    DataManager:registerCrossDayFunc("SummonData", function()
        self:setDirty()
    end)
end

function SummonData:init(summon, isInit)
    -- if EDITOR_MODE then
    --     Logger.logHighlight("召唤初始化")
    --     Logger.printTable(summon)
    -- end

    local lastLv = self.level

    summon = summon or {}
    self.level = summon.level or 1
    self.exp = summon.exp or 0

    if not isInit then
        if lastLv and lastLv < self.level then
            self:markNeedPopBoxLvUpUI()
        end
    end

    self:setDirty()
end

function SummonData:getSummonLevel()
    return self.level
end

function SummonData:getSummonExp()
    return self.exp
end

function SummonData:getSummonConfig()
    return ConfigManager:getConfig("summon")
end

function SummonData:getSummonExpConfig()
    return ConfigManager:getConfig("summon_exp")
end

-- 读表获取宝箱消耗
function SummonData:getSummonCost(summonType)
    local cfgInfo = self:getSummonConfig()[summonType]
    return cfgInfo.item_cost, cfgInfo.cost
end

-- 读表获取宝箱经验
function SummonData:getSummonAddExp(summonType)
    local cfgInfo = self:getSummonConfig()[summonType]
    return cfgInfo.summon_exp
end

-- 读表获取召唤奖励
function SummonData:getSummonRewardByLv(summonType, level)
    level = level or self:getSummonLevel()
    local cfgInfo = self:getSummonConfig()[summonType]
    if level == 1 then
        return cfgInfo.box_lv_base
    else
        if not self.cacheSummonRewardByLv then
            self.cacheSummonRewardByLv = {}
        end
        self.cacheSummonRewardByLv[level] = {}
        local rewards = cfgInfo["box_lv_" .. tostring(level)]
        for _, reward in ipairs(rewards) do
            local find = false
            for index, rewardBase in ipairs(cfgInfo.box_lv_base) do
                if reward.type == rewardBase.type and reward.id == rewardBase.id and reward.num >= rewardBase.num then
                    local num  = reward.num - rewardBase.num
                    if num ~= 0 then
                        table.insert(self.cacheSummonRewardByLv[level], {
                            id = reward.id,
                            type = reward.type,
                            num = num
                        })
                    end
                    find = true
                    break
                end
            end
            if not find then
                table.insert(self.cacheSummonRewardByLv[level], {
                    id = reward.id,
                    type = reward.type,
                    num = reward.num
                })
            end
        end
        return self.cacheSummonRewardByLv[level]
    end
end

function SummonData:getSummonTotalRewardByLv(summonType, level)
    level = level or self:getSummonLevel()
    local rewards = {}
    local baseRewards = self:getSummonRewardByLv(summonType, 1) -- 基础奖励
    for _, reward in ipairs(baseRewards) do
        table.insert(rewards, reward)
    end
    if level > 1 then
        local addRewards = self:getSummonRewardByLv(summonType, level)
        for _, reward in ipairs(addRewards) do
            table.insert(rewards, reward)
        end
    end
    rewards = GFunc.mergeRewards(rewards)
    return rewards
end

function SummonData:getSummonMaxLv()
    return #self:getSummonExpConfig()
end

-- 读表获取特定等级的最大经验值
function SummonData:getSummonMaxExp(level)
    level = level or self:getSummonLevel()
    local cfg = self:getSummonExpConfig()
    for lv, data in ipairs(cfg) do
        if lv == level then
            return data.exp or 0
        end
    end
    return cfg[#cfg].exp or 0
end

-- 如果宝箱等级提升了 则标记
function SummonData:markNeedPopBoxLvUpUI()
    self.needPopBoxLvUpUI = true
end

function SummonData:clearNeedPopBoxLvUpUI()
    self.needPopBoxLvUpUI = false
end

function SummonData:checkNeedPopBoxLvUpUI()
    return self.needPopBoxLvUpUI
end

return SummonData