local ChapterFundData = class("ChapterFundData", BaseData)

BIReport.EVENT_NAME_CHAPTER_FUND = "client_chapter_fund"

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

function ChapterFundData:clear()

end

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

function ChapterFundData:initData(data)
    data = data or {}
    if EDITOR_MODE then
        Logger.logHighlight("章节基金数据：")
        Logger.printTable(data)
    end
    local fund = data.fund or {}
    self.claimed = fund.claimed or {}

    self:findStage()

    self:setDirty()
end

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

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

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

    return self:getMinUnclaimedStage() ~= nil
end

function ChapterFundData:isStageUnlock(stage)
    if stage == nil then
        return false
    end
    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)
    local chapter = self:getNeedChapter(list[1])
    if not DataManager.ChapterData:getChapterPassed(chapter) then
        return false
    end

    return true
end

function ChapterFundData: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 ChapterFundData: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:getProGot(lastId) then
                break
            end
            stage = stage + 1
        end
    end
    self.curStage = stage
end

function ChapterFundData: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 self:getConfig(a).chapter < self:getConfig(b).chapter
            end)
        end
    end

    return self.stageList[stage]
end

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

    return true
end

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

    return DataManager.ChapterData:getChapterPassed(self:getNeedChapter(id))
end

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

function ChapterFundData:getProCanGet(id)
    if not self:getProBought(self:getStageById(id)) then
        return false
    end

    if self:getProGot(id) then
        return false
    end

    return DataManager.ChapterData:getChapterPassed(self:getNeedChapter(id))
end

function ChapterFundData:getProGot(id)
    return self:isRewardReceived(id, 2)
end

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

function ChapterFundData:getCurStage()
    return self.curStage
end

function ChapterFundData: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:canGetRewardById(id) then
            return true
        end
    end

    return false
end

function ChapterFundData:canGetRewardById(id)
    if self:getFreeCanGet(id) then
        return true
    end
    if self:getProCanGet(id) then
        return true
    end

    return false
end

-- 获取可领奖励的最小阶段
function ChapterFundData:getMinUnclaimedStage()
    for i, stage in pairs(self:getUnlockStageList()) do
        if self:canGetRewards(stage) then
            return stage
        end
    end
    return nil
end
-- 获取目前最大阶段
function ChapterFundData:getMaxUnclaimedStage()
    local toStage = 1
    for i, stage in pairs(self:getUnlockStageList()) do
        toStage = stage
    end
    return toStage
end
-- 获取可领奖励的最小索引
function ChapterFundData: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

-- 获取可领奖励的最大索引
function ChapterFundData: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 ChapterFundData:getIsAllClaimed()
    for stage = 1, self:getStageCount() do
        local list = self:getListByStage(stage)
        if not self:getFreeGot(list[#list]) or not self:getProGot(list[#list]) then
            return false
        end
    end

    return true
end

-- 阶段奖励是否领取完毕
function ChapterFundData: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:getProGot(list[#list]) then
        return true
    end

    return false
end

-- 阶段奖励是否全部已领取or可领取
function ChapterFundData: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:getProGot(id) and not self:getProCanGet(id) then
            return false
        end
    end

    return true
end

--region 配置

function ChapterFundData: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("fund_chapter")
    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
--获取最大可领取的id
function ChapterFundData:getMaxUnclaimedId()
    local list = self:getConfig()
    local id = 0
    for k, v in pairs(list) do
        local value = DataManager.ChapterData:getChapterPassed(v.chapter)
        if value then
            if id == nil or id < v.id then
                id = v.id
            end
        end
    end
    return id
end
-- 获取总阶段个数
function ChapterFundData:getStageCount()
    local count = 0
    for id, info in pairs(self:getConfig()) do
        count = math.max(count, info.stage)
    end
    return count
end

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

function ChapterFundData:getProGiftId(stage)
    if self.stageMap == nil then
        self.stageMap = {}
        for id, info in pairs(self:getConfig()) do
            self.stageMap[info.stage] = info.act_gift
        end
    end
    return self.stageMap[stage]
end

function ChapterFundData:getNeedChapter(id)
    return self:getConfig(id).chapter
end
function ChapterFundData:getConfigIdByIndex(currStage, index)
    local id = self:getListByStage(currStage)[index]
    if id then
        return self:getConfig(id).id
    end
end
function ChapterFundData:getFreeRewards(id)
    return self:getConfig(id).reward_free
end

function ChapterFundData:getProRewards(id)
    return self:getConfig(id).reward
end

--endregion

return ChapterFundData