local FundLevelData = class("FundLevelData", BaseData)

FundLevelData.PRO_GIFT_IDS = {
    [1] = 1014000001,
    [2] = 1014000101,
    [3] = 1014000201,
    [4] = 1014000301,
    [5] = 1014000401,
    [6] = 1014000501,
    [7] = 1014000601,
    [8] = 1014000701,
    [9] = 1014000801,
    [10] = 1014000901,
    [11] = 1014001001,
    [12] = 1014001101,
    [13] = 1014001201,
    [14] = 1014001301,
    [15] = 1014001401,
}
function FundLevelData:ctor()
    self.data.isDirty = false
end

function FundLevelData:clear()

end

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

function FundLevelData:getConfig(id)
    if self.giftCfgs then
        if  self.giftCfgs[id] then
            return self.giftCfgs[id]
        else
            return self.giftCfgs
        end
    end
    local cfgs = ConfigManager:getConfig("act_level_gift")
    self.giftCfgs = {}
    local index = 1
    for k, v in pairs(cfgs) do
        if v.season == self.season then
            v.id = k
            self.giftCfgs[index] = v
            index = index + 1
        end
    end
    return self.giftCfgs
end

function FundLevelData:getStageById(id)
    return self:getConfig(id).stage
end

function FundLevelData:initData(data)
    if EDITOR_MODE then
        Logger.logHighlight("---------------基金------------------")
        Logger.printTable(data)
    end
    data = data or {}
    local fund = data.fund or {}
    self.season = data.season or 1
    self.claimed = fund.claimed or {}

    self:findStage()

    self:setDirty()
end

function FundLevelData:getIsOpen(stage)
    if GFunc.isShenhe() then
        return
    end
    if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.ACT_LEVEL_GIFT, true) then
        return false
    end

    stage = stage or self:getCurStage()
    local list = self:getListByStage(stage)
    return list ~= nil
end

-- 获取总阶段个数
function FundLevelData:getStageCount()
    local count = 0
    for id, info in pairs(self:getConfig()) do
        count = math.max(count, info.stage)
    end
    return count
end

function FundLevelData:isStageUnlock(stage)
    if self:getIsAllClaimedByStage(stage) then
        return false
    end

    if stage > 1 and self:getIsAllCanGetByStage(stage - 1) then
        return true
    end

    local list = self:getListByStage(stage)
    if self:getNeedLevel(list[1]) > DataManager.PlayerData:getLv() then
        return false
    end

    return true
end

function FundLevelData:getUnlockStageList()
    self:getListByStage(1)-- 用于初始化stageList
    local stages = {}
    for stage = 1, self:getStageCount() do     
        if self:isStageUnlock(stage) then
            table.insert(stages, stage)
        end
    end
    table.sort(stages)
    return stages
end

function FundLevelData:findStage()
    local stage = 1
    while true do
        local list = self:getListByStage(stage)
        if not list then
            break
        else
            local lastId = list[#list]
            if not self:getFreeGot(lastId) or not self:getProlGot(lastId) then
                break
            end
            stage = stage + 1
        end
    end
    self.curStage = stage
end

function FundLevelData:getListByStage(stage)
    if not self.stageList then
        self.stageList = {}
        for id, info in pairs(self:getConfig()) do
            if not self.stageList[info.stage] then
                self.stageList[info.stage] = {}
            end
            table.insert(self.stageList[info.stage], id)
        end
        for stage, _ in pairs(self.stageList) do
            table.sort(self.stageList[stage], function(a, b)
                return a < b
            end)
        end
    end

    return self.stageList[stage]
end

-- 是否已领奖
function FundLevelData:isRewardReceived(id, grade)
    if self.claimed[id] == nil then
        return false
    end
    if self.claimed[id].grade[grade] == nil then
        return false
    end

    return true
end

function FundLevelData:getFreeCanGet(id)
    if self:getFreeGot(id) then
        return false
    end

    local cfg = self:getConfig(id)
    return cfg.level <= DataManager.PlayerData:getLv()
end

function FundLevelData:getFreeGot(id)
    return self:isRewardReceived(id, 1)
end

function FundLevelData:getFreeRewards(id)
    local cfg = self:getConfig(id)
    return cfg.reward_free
end

function FundLevelData:getNeedLevel(id)
    local cfg = self:getConfig(id)
    return cfg and cfg.level
end

function FundLevelData:getProCanGet(id)
    if not self:getProlBought(self:getStageById(id)) then
        return false
    end

    if self:getProlGot(id) then
        return false
    end

    local cfg = self:getConfig(id)
    return cfg.level <= DataManager.PlayerData:getLv()
end

function FundLevelData:getProlGot(id)
    return self:isRewardReceived(id, 2)
end

function FundLevelData:getProlBought(stage)
    return DataManager.PaymentData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, self:getProActId(stage)) > 0
end

function FundLevelData:getProActId(stage)
    stage = stage or self:getCurStage()
    return self.PRO_GIFT_IDS[stage]
end

function FundLevelData:getBigCanGet(id)
    if not self:getBigBought() then
        return false
    end

    if self:getBigGot(id) then
        return false
    end

    local cfg = self:getConfig(id)
    return cfg.level <= DataManager.PlayerData:getLv()
end
function FundLevelData:getConfigIdByIndex(currStage, index)
    local id = self:getListByStage(currStage)[index]
    if id then
        return self:getConfig(id).id
    end
end
function FundLevelData:getProGot(id)
    return self:isRewardReceived(id, 2)
end

function FundLevelData:getProBought(stage)
    return DataManager.PaymentData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, self:getBigActId(stage)) > 0
end

function FundLevelData:getProRewards(id)
    local cfg = self:getConfig(id)
    return cfg.reward
end

function FundLevelData:getBigActId(stage)
    stage = stage or self:getCurStage()
    return self.PRO_GIFT_IDS[stage]
end

function FundLevelData:getCurStage()
    return self.curStage
end

function FundLevelData:getRedPoint()
    if not self:getIsOpen() then
        return false
    end

    return self:getMinUnclaimedStage() ~= nil
end

function FundLevelData:canGetRewards(stage)
    stage = stage or self:getCurStage()
    local list = self:getListByStage(stage)
    if not list then
        return false
    end

    for _, id in ipairs(list) do
        if self:getFreeCanGet(id) then
            return true
        end
        if self:getProCanGet(id) then
            return true
        end
    end

    return false
end

-- 获取可领奖励的最小阶段
function FundLevelData:getMinUnclaimedStage()
    for i, stage in pairs(self:getUnlockStageList()) do
        if self:canGetRewards(stage) then
            return stage
        end
    end
    return nil
end
-- 获取可领奖励的最小阶段
function FundLevelData:getMaxUnclaimedStage()
    local toStage = 1
    for i, stage in pairs(self:getUnlockStageList()) do
        toStage = stage
    end
    return toStage
end
-- 获取可领奖励的最小索引
function FundLevelData:getMinUnclaimedRewardIndex(stage)
    stage = stage or self:getCurStage()
    local list = self:getListByStage(stage)
    if not list then
        return 0
    end
    for index, id in ipairs(list) do
        if self:getFreeCanGet(id) then
            return index
        end
        if self:getProCanGet(id) then
            return index
        end
    end
    for i = 1, #list do
        if not self:getFreeGot(list[i]) then
            return i
        end
    end
    return #list
end
--获取最大可领取的id
function FundLevelData:getMaxUnclaimedId()
    local list = self:getConfig()
    local id = 0
    for k, v in pairs(list) do
        local value = DataManager.PlayerData:getLv() >= v.level
        if value then
            if id == nil or id < v.id then
                id = v.id
            end
        end
    end
    return id
end
-- 获取可领奖励的最大索引
function FundLevelData:getMaxUnclaimedRewardIndex(stage)
    stage = stage or self:getCurStage()
    local list = self:getListByStage(stage)
    if not list then
        return 0
    end
    for i = #list, 1, -1 do
        if self:getFreeCanGet(list[i]) then
            return i
        end
        if self:getProCanGet(list[i]) then
            return i
        end
    end
    for i = 1, #list do
        if not self:getFreeGot(list[i]) then
            return i
        end
    end
    return #list
end

-- 是否已经领取了全部奖励，包括付费的
function FundLevelData:getIsAllClaimed()
    for stage = 1, self:getStageCount() do
        local list = self:getListByStage(stage)
        if not self:getFreeGot(list[#list]) or not self:getProlGot(list[#list]) then
            return false
        end
    end

    return true
end

-- 阶段奖励是否领取完毕
function FundLevelData:getIsAllClaimedByStage(stage)
    stage = stage or self:getCurStage()
    local list = self:getListByStage(stage)
    if not list then
        return false
    end

    if self:getFreeGot(list[#list]) and self:getProlGot(list[#list]) then
        return true
    end

    return false
end

-- 阶段奖励是否全部已领取or可领取
function FundLevelData:getIsAllCanGetByStage(stage)
    stage = stage or self:getCurStage()
    local list = self:getListByStage(stage)
    if not list then
        return false
    end

    for _, id in ipairs(list) do
        if not self:getFreeGot(id) and not self:getFreeCanGet(id) then
            return false
        end
        if not self:getProlGot(id) and not self:getProCanGet(id) then
            return false
        end
    end

    return true
end

return FundLevelData