--------------------------------
-- 文件名       :   MergeServerHuoYue.lua
-- 文件说明     :   合服活动-活跃
-- 创建时间     :   2025/07/30
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local CommonDefine = require("common.CommonDefine")
local MergeConfig = require("excel.MergeServerActivity")
local CommonDB = require("common.CommonDB")
local BuyLogic = require("topup.BuyLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local MergeServerLogin = require("present.MergeServerLogin")


local MERGEHUOYUE_PAY_NOGET = 0     -- 不可领取
local MERGEHUOYUE_PAY_BUY = 1       -- 已购买
local MERGEHUOYUE_PAY_HUOYUE = 2    -- 活跃度到达要求
local MERGEHUOYUE_PAY_CANGET = 3    -- 可以领取
local MERGEHUOYUE_PAY_GET = 4       -- 已领取

----------------------------------------- 内部处理开始 -------------------------------------
local function MergeServerHuoYue_IsOpen()
    local nBeginTime = CommonDB.getMergeServerTime()
    if 0 >= nBeginTime then
        return false
    end

    local nEndTime = nBeginTime + (CommonDefine.MERGERSERVER_ACTIVITY_DAY - 1) * 24 * 60 * 60
    local tEndDate = os.date("*t",nEndTime)
    tEndDate.hour = 23
    tEndDate.min = 59
    tEndDate.sec = 59

    nEndTime = os.time(tEndDate)

    local nNowTime = os.time()
    
    return nEndTime > nNowTime
end

local function MergeServerHuoYue_GetConf()
    return MergeConfig.HuoYuePrize
end

local function MergeServerHuoYue_CreateDB(human)
    human.db.MergeHuoYue = {
        nBeginTime = CommonDB.getMergeServerTime(),
        nAllHuoYue = 0,
        tPrizeData = {}
    }

    local tData = human.db.MergeHuoYue.tPrizeData
    local tConf = MergeServerHuoYue_GetConf()
    for nID, v in ipairs(tConf) do
        tData[nID] = {
            nFreeStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET,
            nPayStatus = MERGEHUOYUE_PAY_NOGET,
        }
    end
end

local function MergeServerHuoYue_CheckDB(human)
    if not human.db.MergeHuoYue then
        MergeServerHuoYue_CreateDB(human)
    else
        if human.db.MergeHuoYue.nBeginTime then
            local nNowBeginTime =  CommonDB.getMergeServerTime()
            if nNowBeginTime ~= human.db.MergeHuoYue.nBeginTime then
                MergeServerHuoYue_CreateDB(human)
            end
        end
    end
end

local function MergeServerHuoYue_GetFreeStatus(human, nID)
    return human.db.MergeHuoYue.tPrizeData[nID].nFreeStatus
end

local function MergeServerHuoYue_SetFreeStatus(human, nID, nValue)
    human.db.MergeHuoYue.tPrizeData[nID].nFreeStatus = nValue
end

local function MergeServerHuoYue_GetPayStatus(human, nID)
    return human.db.MergeHuoYue.tPrizeData[nID].nPayStatus
end

local function MergeServerHuoYue_SetPayStatus(human, nID, nValue)
    human.db.MergeHuoYue.tPrizeData[nID].nPayStatus = nValue
end

local function MergeServerHuoYue_GetAllHuoYue(human)
    return human.db.MergeHuoYue.nAllHuoYue
end

local function MergeServerHuoYue_AddHuoYue(human, nNum)
    human.db.MergeHuoYue.nAllHuoYue = human.db.MergeHuoYue.nAllHuoYue + nNum
end

-- 检测付费奖励状态
local function MergeServerHuoYue_CheckStatus(human)
    local tConfig = MergeServerHuoYue_GetConf()
    local nAllHuoYue = MergeServerHuoYue_GetAllHuoYue(human)
    for nID, v in ipairs(tConfig) do
        local nPayState = MergeServerHuoYue_GetPayStatus(human, nID)
        if nAllHuoYue >= v.nHuoYue then
            if nPayState == MERGEHUOYUE_PAY_BUY then
                MergeServerHuoYue_SetPayStatus(human, nID, MERGEHUOYUE_PAY_CANGET)
            elseif MERGEHUOYUE_PAY_NOGET == nPayState then
                MergeServerHuoYue_SetPayStatus(human, nID, MERGEHUOYUE_PAY_HUOYUE)
            end
        end

        local nFreeStatus = MergeServerHuoYue_GetFreeStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_NOGET == nFreeStatus and nAllHuoYue >= v.nHuoYue then
            MergeServerHuoYue_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
        end
    end
end

-----------------------------------客户端请求-----------------------------
-- 请求活跃奖励信息
function MergeServerHuoYue_Query(human)
    if false == MergeServerHuoYue_IsOpen() then
        return
    end

    MergeServerHuoYue_CheckDB(human)

    local tConfig = MergeServerHuoYue_GetConf()
    local tMsgData = Msg.gc.GC_MERGESERVER_QUERY_HUOYUE
    tMsgData.nAllHuoYue = MergeServerHuoYue_GetAllHuoYue(human)
    tMsgData.tList[0] = #tConfig

    for nID, v in ipairs(tConfig) do
        local tData = tMsgData.tList[nID]
        tData.nID = nID
        tData.nNeedHuoYue = v.nHuoYue
        tData.nFreeState = MergeServerHuoYue_GetFreeStatus(human, nID)
        tData.bBuy = 0

        local nPayState = MergeServerHuoYue_GetPayStatus(human, nID)
        if MERGEHUOYUE_PAY_CANGET == nPayState then
            tData.nPayState = CommonDefine.COMMON_PRIZE_STATE_CANGET
            tData.bBuy = 1
        elseif MERGEHUOYUE_PAY_GET == nPayState then
            tData.nPayState = CommonDefine.COMMON_PRIZE_STATE_GET
            tData.bBuy = 1
        else
            tData.nPayState =  CommonDefine.COMMON_PRIZE_STATE_NOGET
        end

        if MERGEHUOYUE_PAY_BUY == nPayState then
            tData.bBuy = 1
        end

        -- 购买项
        BuyLogic.fontBuyItem(human, tData.buyItem, v.nBuyID)

        tData.tFreeItem[0] = #v.tFreePrize
        for i, tItem in ipairs(v.tFreePrize) do
            local nGoodsID = tItem[1]
            local nGoodsNum = tItem[2]

            Grid.makeItem(tData.tFreeItem[i], nGoodsID, nGoodsNum)
        end

        tData.tPayItem[0] = #v.tPayPrize
        for i, tItem in ipairs(v.tPayPrize) do
            local nGoodsID = tItem[1]
            local nGoodsNum = tItem[2]

            Grid.makeItem(tData.tPayItem[i], nGoodsID, nGoodsNum)
        end
    end

    Msg.send(tMsgData, human.fd)
end

-- 请求领取活跃奖励
function MergeServerHuoYue_Get(human)
    if false == MergeServerHuoYue_IsOpen() then
        return
    end

    local tConfig = MergeServerHuoYue_GetConf()
    local tGoods = {}
    for nID, v in ipairs(tConfig) do
        local nFreeStatus = MergeServerHuoYue_GetFreeStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus then
            MergeServerHuoYue_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
            for _, tItem in ipairs(v.tFreePrize) do
                local nGoodsID = tItem[1]
                local nGoodsNum = tItem[2]

                if not tGoods[nGoodsID] then
                    tGoods[nGoodsID] = 0
                end

                tGoods[nGoodsID] = tGoods[nGoodsID] + nGoodsNum
            end
        end

        local nPayStatus = MergeServerHuoYue_GetPayStatus(human, nID)
        if MERGEHUOYUE_PAY_CANGET == nPayStatus then
            MergeServerHuoYue_SetPayStatus(human, nID, MERGEHUOYUE_PAY_GET)
            for _, tItem in ipairs(v.tPayPrize) do
                local nGoodsID = tItem[1]
                local nGoodsNum = tItem[2]

                if not tGoods[nGoodsID] then
                    tGoods[nGoodsID] = 0
                end

                tGoods[nGoodsID] = tGoods[nGoodsID] + nGoodsNum
            end
        end
    end

    if nil ~= _G.next(tGoods) then
        local tItem = {}
        for nGoodID, nGoodNum in pairs(tGoods) do
            table.insert(tItem, {nGoodID, nGoodNum})
        end

        BagLogic.addItemList(human, tItem, "MergeHuoYue")
        MergeServerHuoYue_Query(human)

        YunYingLogic.sendBanner(human)
        MergeServerLogin.MergeServerLogin_SendRed(human)
    end
end


------------------------------------- 外部调用 -------------------------------
-- 是否开启
function isOpen(human, YYInfo, funcConfig)
   return MergeServerHuoYue_IsOpen()
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isRed(human, YYInfo, funcConfig)
    if false == MergeServerHuoYue_IsOpen() then
        return false
    end

    MergeServerHuoYue_CheckDB(human)

    local tConfig = MergeServerHuoYue_GetConf()
    for nID, v in ipairs(tConfig) do
        local nFreeStatus = MergeServerHuoYue_GetFreeStatus(human, nID)
        local nPayStatus = MergeServerHuoYue_GetPayStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus or
            MERGEHUOYUE_PAY_CANGET == nPayStatus then
            return true
        end
    end

    return false
end

function onDailyTask(human, funcID, nSum, parameter2)
    if false == MergeServerHuoYue_IsOpen() then
        return
    end

    MergeServerHuoYue_CheckDB(human)

    MergeServerHuoYue_AddHuoYue(human, nSum)
    MergeServerHuoYue_CheckStatus(human)
    MergeServerLogin.MergeServerLogin_SendRed(human)
end

function onCharge(human, price, funcID, buyID)
    if false == MergeServerHuoYue_IsOpen() then
        return
    end

    MergeServerHuoYue_CheckDB(human)

    
    local tConfig = MergeServerHuoYue_GetConf()
    for nID, v in ipairs(tConfig) do
        if v.nBuyID == buyID then
            local nPayStatus = MergeServerHuoYue_GetPayStatus(human, nID)
            if MERGEHUOYUE_PAY_NOGET == nPayStatus then
                MergeServerHuoYue_SetPayStatus(human, nID, MERGEHUOYUE_PAY_BUY)
            elseif MERGEHUOYUE_PAY_HUOYUE == nPayStatus then
                MergeServerHuoYue_SetPayStatus(human, nID, MERGEHUOYUE_PAY_CANGET)
            end
        end
    end

    MergeServerHuoYue_Query(human)
    MergeServerLogin.MergeServerLogin_SendRed(human)
end

-- function GetRemainNum(human, nBuyID)
--     local tConfig = MergeServerHuoYue_GetConf()
--     for nID, v in ipairs(tConfig) do
--         if v.nBuyID == nBuyID then
--             local nPayStatus = MergeServerHuoYue_GetPayStatus(human, nID)
--             if MERGEHUOYUE_PAY_HUOYUE == nPayStatus or MERGEHUOYUE_PAY_HUOYUE == nPayStatus then
--                 return 1
--             else
--                 return 0
--             end
--         end
--     end

--     return 0
-- end