-- 闯关主题
--db
--[=[
    human.db.breakThroughData = {
        breakThroughGetList = { -- 闯关奖励领取记录列表
            [cfgIdx1] = true,
            [cfgIdx2] = true,
        },

        advanceRewardGetList = { --进阶奖励领取记录表
            [day1] = true,
            [day2] = true,
        },

        isBuy = nil, -- 购买指定项后，值为true
        loginDays = nil, 购买指定项后才统计的登录天数
        isShowTips = nil, 用来记录客户端是否弹出过提示, 弹出过后为true
    }
]=]--


local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local HeroGrid = require("hero.HeroGrid")
local BreakThroughThemeConfig = require("excel.breakThroughTheme")
local BattleLogic = require("battle.BattleLogic")
local EliteDefine = require("battle.EliteDefine")
local BuyLogic = require("topup.BuyLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local RoleSystemConfig = require("excel.roleSystem").roleSystem


local LOGTYPE = "BreakThroughTheme"


local function getData(human)
    return human.db.breakThroughData
end

local function updateBreakThroughGetList(human, getIdx)
    human.db.breakThroughData = human.db.breakThroughData or {}
    human.db.breakThroughData.breakThroughGetList = human.db.breakThroughData.breakThroughGetList or {}
    human.db.breakThroughData.breakThroughGetList[getIdx] = true
end

local function updateAdvanceRewardGetList(human, getIdx)
    human.db.breakThroughData = human.db.breakThroughData or {}
    human.db.breakThroughData.advanceRewardGetList = human.db.breakThroughData.advanceRewardGetList or {}
    human.db.breakThroughData.advanceRewardGetList[getIdx] = true
end

local function updateBuyState(human, state)
    human.db.breakThroughData = human.db.breakThroughData or {}
    human.db.breakThroughData.isBuy = state
end

local function addLoginDays(human)
    human.db.breakThroughData = human.db.breakThroughData or {}
    human.db.breakThroughData.loginDays = (human.db.breakThroughData.loginDays or 0) + 1
end

local function updateShowTipsState(human, state)
    human.db.breakThroughData = human.db.breakThroughData or {}
    human.db.breakThroughData.isShowTips = state
end



local function isBuy(human)
    local breakThroughData = getData(human)
    if breakThroughData and breakThroughData.isBuy then
        return true
    end

    return false
end

-- 获取所有闯关奖励
local function getAllBreakThroughReward(human)
    if not isBuy(human) then
        return
    end

    local breakThroughData = getData(human)
    local breakThroughGetList = breakThroughData and breakThroughData.breakThroughGetList
    local passStageId = BattleLogic.GetBattleIdByType(human, EliteDefine.COPY_ELITE_NORMAL)

    local itemList, getIdArr

    for k,v in ipairs(BreakThroughThemeConfig.breakThroughReward) do
        if v.rewardCondStage <= passStageId and (not breakThroughGetList or not breakThroughGetList[k]) then
            getIdArr = getIdArr or {}
            getIdArr[#getIdArr+1] = k

            itemList = itemList or {}
            for _, itemInfo in ipairs(v.reward) do
                local itemId, itemCnt = itemInfo[1], itemInfo[2]
                itemList[itemId] = (itemList[itemId] or 0) + itemCnt
            end
        end
    end

    return itemList, getIdArr
end

-- 取所有进阶奖励
local function getAllAdvancedReward(human)
    if not isBuy(human) then
        return
    end

    local breakThroughData = getData(human)
    local advanceRewardGetList = breakThroughData.advanceRewardGetList
    local loginDays = breakThroughData.loginDays or 0

    local itemList, getIdArr

    for k,v in ipairs(BreakThroughThemeConfig.advancedReward) do
        if v.condDay <= loginDays and (not advanceRewardGetList or not advanceRewardGetList[k]) then
            getIdArr = getIdArr or {}
            getIdArr[#getIdArr+1] = k

            itemList = itemList or {}
            local itemId, itemCnt = v.reward[1], v.reward[2]
            itemList[itemId] = (itemList[itemId] or 0) + itemCnt
        end
    end

    return itemList, getIdArr
end

-- 填充推荐阵容协议数据
local function populateLineupMsg(net, heroArr)
    if not net or not heroArr then
        return
    end

    local grid = { id =  0}
    net[0] = #heroArr

    for i, heroId in ipairs(heroArr) do
        grid.id = heroId
        HeroGrid.makeHeroSimple(net[i], grid)
    end
end

-- 入口红点刷新
function EntranceRedDotUpdate(human)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_112)
end

-- 标签红点刷新
local function lableRedDotUpdate(human)
    local msgRet = Msg.gc.GC_BREATHROUGHTHEME_REDDOT
    msgRet.page1RedDotState = 0
    msgRet.page2RedDotState = 0

    if isBuy(human) then
        local itemList = getAllBreakThroughReward(human)
        if itemList then
            msgRet.page1RedDotState = 1
        end

        itemList = getAllAdvancedReward(human)
        if itemList then
            msgRet.page2RedDotState = 1
        end
    end

    Msg.send(msgRet, human.fd)
end

-- 所有红点刷新
local function redDotUpdate(human)
    EntranceRedDotUpdate(human)
    lableRedDotUpdate(human)
end

-- 入口关闭检查
local function entranceCloseCheck(human)
    RoleSystemLogic.isClose(human, RoleSystemDefine.ROLE_SYS_ID_112)
end

-- 领取所有闯关奖励
local function getBreakThroughReward(human)
    local itemList, getIdArr = getAllBreakThroughReward(human)
    if not itemList then
        return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
    end

    for _, getIdx in ipairs(getIdArr or {}) do
        updateBreakThroughGetList(human, getIdx)
    end
    BagLogic.addItemList(human, itemList, LOGTYPE)

    redDotUpdate(human)

    BreakThrough_BreakThroughReward_Query(human)

    entranceCloseCheck(human)
end

-- 领取所有进阶奖励
local function getAdvancedReward(human)
    local itemList, getIdArr = getAllAdvancedReward(human)
    if not itemList then
        return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
    end

    for _, getIdx in ipairs(getIdArr or {}) do
        updateAdvanceRewardGetList(human, getIdx)
    end
    BagLogic.addItemList(human, itemList, LOGTYPE)

    redDotUpdate(human)

    BreakThrough_AdvancedReward_Query(human)

    entranceCloseCheck(human)
end

-- 通知客户端弹出提示
local function showTips(human)
    local systemCfg = RoleSystemConfig[RoleSystemDefine.ROLE_SYS_ID_112]
    if human.db.lv < systemCfg.lv then
        return
    end

    local breakThroughData = getData(human)
    if breakThroughData and breakThroughData.isShowTips then
        return
    end

    local msgRet = Msg.gc.GC_BREATHROUGHTHEME_SHOW_TIPS
    msgRet.tipState = 1
    Msg.send(msgRet, human.fd)
end



-- 外部调用, 是否显示入口, 领取完所有免费/付费奖励, 入口才关闭
function ModuleisOpen(human)
    local breakThroughData = getData(human)
    if not breakThroughData or not breakThroughData.isBuy then
        return true
    end

    if not breakThroughData.breakThroughGetList or not breakThroughData.advanceRewardGetList then
        return true
    end

    local breakThroughGetList = breakThroughData.breakThroughGetList
    for k in ipairs(BreakThroughThemeConfig.breakThroughReward) do
        if not breakThroughGetList[k] then
            return true
        end
    end

    local advanceRewardGetList = breakThroughData.advanceRewardGetList
    for k in ipairs(BreakThroughThemeConfig.advancedReward) do
        if not advanceRewardGetList[k] then
            return true
        end
    end

    return false
end

-- 外部调用, 入口处是否有红点
function isDot(human)
    if not isBuy(human) then
        return false
    end

    local itemList = getAllBreakThroughReward(human)
    if itemList then
        return true
    end

    itemList = getAllAdvancedReward(human)
    if itemList then
        return true
    end

    return false
end

-- 外部调用,跨天
function updateDaily(human)
    if not isBuy(human) then
        return
    end

    addLoginDays(human)

    -- 刷新红点
    if ModuleisOpen(human) then
        redDotUpdate(human)
    end
end

-- 外部调用, 登录
function onLogin(human)
    showTips(human)
end

-- 外部调用,充值
function charge(human, nAllPrice, buyID)
    local var = BreakThroughThemeConfig.var[1]
    if var.buyId ~= buyID then
        return
    end

    if isBuy(human) then
        return
    end

    updateBuyState(human, true)
    addLoginDays(human)

    -- 红点
    redDotUpdate(human)

    BreakThrough_AdvancedReward_Query(human)
end

-- 外部调用,升级
function onLvUp(human)
    showTips(human)
end


-- 闯关阵容查询
function BreakThrough_Lineup_Query(human)
    local msgRet = Msg.gc.GC_BREATHROUGHTHEME_LINEUP_QUERY

    local lineupCfg1 = BreakThroughThemeConfig.recommendedLineup[1]
    populateLineupMsg(msgRet.lineup1, lineupCfg1.heroIdArr)

    local lineupCfg2 = BreakThroughThemeConfig.recommendedLineup[2]
    populateLineupMsg(msgRet.lineup2, lineupCfg2.heroIdArr)

    local lineupCfg3 = BreakThroughThemeConfig.recommendedLineup[3]
    populateLineupMsg(msgRet.lineup3, lineupCfg3.heroIdArr)

    Msg.send(msgRet, human.fd)

    lableRedDotUpdate(human)
end

-- 闯关奖励查询
function BreakThrough_BreakThroughReward_Query(human)
    local msgRet = Msg.gc.GC_BREATHROUGHTHEME_BREAKTHROUGHREWARD_QUERY
    msgRet.list[0] = 0
    msgRet.isEnd = 0
    msgRet.buyState = isBuy(human) and 1 or 0

    local breakThroughData = getData(human)
    local breakThroughGetList = breakThroughData and breakThroughData.breakThroughGetList
    local passStageId = BattleLogic.GetBattleIdByType(human, EliteDefine.COPY_ELITE_NORMAL)

    local len = 0
    local msgOnceLen = 5
    local rewardCnt = #BreakThroughThemeConfig.breakThroughReward

    for k,v in ipairs(BreakThroughThemeConfig.breakThroughReward) do
        len = len + 1
        msgRet.list[0] = len
        msgRet.list[len].condStage = v.rewardCondStage
        msgRet.list[len].reallyIdx = k
        msgRet.list[len].rewardStage = 0
        msgRet.list[len].rewardArr[0] = 0

        if breakThroughGetList and breakThroughGetList[k] then
            msgRet.list[len].rewardStage = 2
        else
            if breakThroughData and breakThroughData.isBuy and passStageId >= v.rewardCondStage then
                msgRet.list[len].rewardStage = 1
            end
        end

        msgRet.list[len].rewardArr[0] = #v.reward
        for i, itemInfo in ipairs(v.reward) do
            Grid.makeItem(msgRet.list[len].rewardArr[i], itemInfo[1], itemInfo[2])
        end

        if len >= msgOnceLen then
            rewardCnt = rewardCnt - len
            if rewardCnt <= 0 then
                msgRet.isEnd = 1
                return Msg.send(msgRet, human.fd)
            end

            Msg.send(msgRet, human.fd)
            len = 0
        end
    end

    if len >= 0 then
        msgRet.isEnd = 1
        Msg.send(msgRet, human.fd)
    end
end

-- 进阶奖励查询
function BreakThrough_AdvancedReward_Query(human)
    local var = BreakThroughThemeConfig.var[1]
    local advancedRewardCfg = BreakThroughThemeConfig.advancedReward

    local msgRet = Msg.gc.GC_BREATHROUGHTHEME_ADVANCEDREWARD_QUERY
    msgRet.list[0] = 0
    BuyLogic.fontBuyItem(human, msgRet.buyItem, var.buyId)
    msgRet.buyState = 0

    local breakThroughData = getData(human)
    local advanceRewardGetList = breakThroughData and breakThroughData.advanceRewardGetList
    if breakThroughData and breakThroughData.isBuy then
        msgRet.buyState = 1
    end

    msgRet.list[0] = #advancedRewardCfg
    for k, v in ipairs(advancedRewardCfg) do
        msgRet.list[k].rewardStage = 0
        msgRet.list[k].condDays = v.condDay
        Grid.makeItem(msgRet.list[k].reward, v.reward[1], v.reward[2])

        if advanceRewardGetList and advanceRewardGetList[k] then
            msgRet.list[k].rewardStage = 2
        else
            if msgRet.buyState == 1 and (breakThroughData and breakThroughData.loginDays or 0) >= v.condDay then
                msgRet.list[k].rewardStage = 1
            end
        end
    end

    Msg.send(msgRet, human.fd)
end

-- 领奖
function BreakThrough_GetReward(human, rewardType)
    if rewardType == 1 then
        getBreakThroughReward(human)
    elseif rewardType == 2 then
        getAdvancedReward(human)
    end
end

-- 客户端通知服务器已经弹出过提示了
function BreakThrough_UpdateShowTipsState(human)
    updateShowTipsState(human, true)
end