-- 闯关礼金

--db
--[=[
    human.db.battleGift = {
        dailyStoreVal = nil, -- 每日获得的存储礼金
        dailyGetVal = nil,  -- 每日已领取的礼金
        buyIdList = nil,    -- 充值过的购买项Id列表
    }
]=]--

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 BattleExcel = require("excel.battle")
local BattleExcel_Elite = require("excel.battle_elite")
local BattleExcel_Hard = require("excel.battle_hard")
local BattleLogic = require("battle.BattleLogic")
local EliteDefine = require("battle.EliteDefine")
local Util = require("common.Util")
local BuyLogic = require("topup.BuyLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")


local LOGTYPE = "BattleGift"


local function initData(human)
    human.db.battleGift = { dailyStoreVal = 0, dailyGetVal = 0 }
end

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

local function addDailyStoreVal(human, val)
    local battleGiftData = getData(human)
    if not battleGiftData then
        initData(human)
        battleGiftData = getData(human)
    end

    battleGiftData.dailyStoreVal = battleGiftData.dailyStoreVal + val
end

local function addDailyGetVal(human, val)
    local battleGiftData = getData(human)
    if not battleGiftData then
        initData(human)
        battleGiftData = getData(human)
    end

    battleGiftData.dailyGetVal = battleGiftData.dailyGetVal + val
end

local function insertBuyIdList(human, buyId)
    local battleGiftData = getData(human)
    if not battleGiftData then
        initData(human)
        battleGiftData = getData(human)
    end

    battleGiftData.buyIdList = battleGiftData.buyIdList or {}
    table.insert(battleGiftData.buyIdList, buyId)
end

local function resetData(human)
    local battleGiftData = getData(human)
    if not battleGiftData then
        return
    end

    battleGiftData.dailyStoreVal = 0
    battleGiftData.dailyGetVal = 0
end



-- 获取本关卡能获得的礼金数量
local function getStageVal(stageType, stageIdx)
    local conf
    if stageType == EliteDefine.COPY_LEVEL_ELITE then
        conf = BattleExcel_Elite.node
    elseif stageType == EliteDefine.COPY_LEVEL_ELITE then
        conf = BattleExcel_Hard.node
    else
        conf = BattleExcel.node
    end

    local stageCfg = conf[stageIdx]
    if stageCfg and stageCfg.giftStoreVal then
        return stageCfg.giftStoreVal
    end

    return 0
end

-- 计算当前关卡一共能获得的礼金
local function calcCurrentStageVal(stageType, stageVal)
    local val = 0
    for i=1, stageVal do
        val = val + getStageVal(stageType, i)
    end

    return val
end

-- 处理老号, 把老号之前已经通关的关卡应该获得的礼金算上
local function calcBeforeVal(human)
    local totalVal = 0
    for i=EliteDefine.COPY_ELITE_NORMAL, EliteDefine.COPY_ELITE_DIFFICULTY do
        local stage = BattleLogic.GetBattleIdByType(human, i)
        if stage and stage > 0 then
            totalVal = totalVal + calcCurrentStageVal(i, stage)
        end
    end

    addDailyStoreVal(human, totalVal)
end

-- 计算当前关卡一共增加的"每日可领取上限"
local function calcStageDailyGeteVal(human)
    local val = 0
    local stage = BattleLogic.GetBattleIdByType(human, EliteDefine.COPY_ELITE_NORMAL)

    for k,v in ipairs(BattleExcel.node) do
        if k > stage then
            break
        end

        if v.dailyLimitVal and v.dailyLimitVal > 0 then
            val = val + v.dailyLimitVal
        end
    end

    return val
end

-- 计算每天可领礼金的最大值
local function calclDailyMaxGetVal(human)
    local val = EliteDefine.BATTLEGIFT_FREE_LIMIT

    local battleGiftData = getData(human)
    if battleGiftData and battleGiftData.buyIdList then
        for _, buyId in ipairs(battleGiftData.buyIdList) do
            val = val + (EliteDefine.BATTLEGIFT_BUYID_2_LIMIT[buyId] or 0)
        end
    end

    val = val + calcStageDailyGeteVal(human)
    return val
end

-- 红点刷新
local function updateRedDot(human)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1106)
end



-- 外部调用,跨天处理
function updateDaily(human)
    local battleGiftData = getData(human)
    if not battleGiftData then
        calcBeforeVal(human)
        updateRedDot(human)
        return
    end

    resetData(human)

    updateRedDot(human)
end

-- 外部调用, 登录处理
function onLogin(human)
    local battleGiftData = getData(human)
    if not battleGiftData then
        calcBeforeVal(human)
        updateRedDot(human)
    end
end

-- 外部调用, 通关新的关卡
function StageClearHandle(human, currentStage, stageType)
    local val = getStageVal(stageType, currentStage)
    if val > 0 then
        addDailyStoreVal(human, val)
        updateRedDot(human)
    end
end

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

    local dailyStoreVal = battleGiftData.dailyStoreVal or 0
    if dailyStoreVal <= 0 then
        return false
    end

    -- local dailyGetVal = battleGiftData.dailyGetVal or 0
    -- local dailyMaxVal = calclDailyMaxGetVal(human)
    -- if dailyGetVal >= dailyMaxVal then
    --     return false
    -- end

    return true
end

-- 外部调用, 充值处理接口
function charge(human, nAllPrice, buyID)
    if not table.find(EliteDefine.BATTLEGIFT_BUYID_LIST, buyID) then
        return
    end

    local battleGiftData = getData(human)
    if battleGiftData and battleGiftData.buyIdList and table.find(battleGiftData.buyIdList, buyID) then
        return
    end

    insertBuyIdList(human, buyID)

    updateRedDot(human)

    BattleGift_Query(human)
end




-- 查询
function BattleGift_Query(human)
    local msgRet = Msg.gc.GC_BATTLEGIFT_QUERY
    msgRet.leftTime = 0
    msgRet.dailyStoreVal = 0
    msgRet.dailyGetVal = 0
    msgRet.dailyMaxVal = 0

    local now = os.time()
    local toDayStartTime = Util.getDayStartTime(now)
    msgRet.leftTime = toDayStartTime + 86400 - now

    local battleGiftData = getData(human)
    msgRet.dailyStoreVal = battleGiftData and battleGiftData.dailyStoreVal or 0
    msgRet.dailyGetVal = battleGiftData and battleGiftData.dailyGetVal or 0
    msgRet.dailyMaxVal = calclDailyMaxGetVal(human)
    msgRet.dailyLimitVal = 0

    local targetBuyId = 0
    for _, buyId in ipairs(EliteDefine.BATTLEGIFT_BUYID_LIST) do
        if not battleGiftData or not battleGiftData.buyIdList or not table.find(battleGiftData.buyIdList, buyId) then
            targetBuyId = buyId
            break
        end
    end

    msgRet.dailyLimitVal = EliteDefine.BATTLEGIFT_BUYID_2_LIMIT[targetBuyId] or 0
    BuyLogic.fontBuyItem(human, msgRet.buyItem, targetBuyId)

    Msg.send(msgRet, human.fd)
end

-- 领取
function BattleGift_Get(human)
    local battleGiftData = getData(human)
    if not battleGiftData then
        return Broadcast.sendErr(human, Lang.BATTLEGIFT_NOT_ENOUGH)
    end

    local dailyStoreVal = battleGiftData.dailyStoreVal or 0
    if dailyStoreVal <= 0 then
        return Broadcast.sendErr(human, Lang.BATTLEGIFT_NOT_ENOUGH)
    end

    local dailyGetVal = battleGiftData.dailyGetVal or 0
    local dailyMaxVal = calclDailyMaxGetVal(human)
    if dailyGetVal >= dailyMaxVal then
        return Broadcast.sendErr(human, Lang.BATTLEGIFT_DAILY_CAN_GET_MAX)
    end

    local getVal = math.min(dailyStoreVal, dailyMaxVal - dailyGetVal)
    if getVal < 100 then
        return Broadcast.sendErr(human, Lang.BATTLEGIFT_DAILY_CAN_GET_MAX)
    end

    addDailyStoreVal(human, -getVal)

    addDailyGetVal(human, getVal)

    local itemId = EliteDefine.BATTLEGIFT_2_ITEMID
    local itemCnt = math.floor(getVal / 100)
    BagLogic.addItemList(human, { {itemId, itemCnt} }, LOGTYPE)

    BattleGift_Query(human)

    updateRedDot(human)
end