--------------------------------
-- 文件名       :   MergeServerCharge.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 YunYingLogic = require("yunying.YunYingLogic")
local MergeServerLogin = require("present.MergeServerLogin")

-- 金额映射红点下标
local tChargeMoney2Index = 
{
    [6] = 1,
    [98] = 2,
    [198] = 3,
}

local tIndex2Money = {
    [1] = 6,
    [2] = 98,
    [3] = 198,
}

local MERGESERVERCHARGE_MAXDAY = 10

----------------------------------------- 内部处理开始 -------------------------------------
local function MergeServerCharge_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 MergeServerCharge_GetCof()
    return MergeConfig.Charge
end

-- 初始化DB数据
local function MergeServerCharge_CreatDB(human)
    human.db.MergeCharge =
    {
        nBeginTime = CommonDB.getMergeServerTime(),
        bDispose = false,
        tChargeData = {},
        tPrizeData = {}
    }

    local tPrizeData = human.db.MergeCharge.tPrizeData
    local tConf = MergeServerCharge_GetCof()
    for _, v in ipairs(tConf) do
        if not tPrizeData[v.nDay] then
            tPrizeData[v.nDay] = {
                tMoney = {}
            }
        end

        if not human.db.MergeCharge.tChargeData[v.nMoney] then
            human.db.MergeCharge.tChargeData[v.nMoney] = {
                nBuyTime = 0,
                nBuyDay = 0
            }
        end
       
        local tNodeData = tPrizeData[v.nDay].tMoney
        if not tNodeData[v.nMoney] then
            tNodeData[v.nMoney] =
            {
                nStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET,
                nMoney = 0
            }
        else
            print("[MergeServerCharge_CreateDB] 配置了重复的数据")
        end
    end

    print("[MergeServerCharge_CreatDB] 初始化数据完成 name = "..human.db.name)
    return true
end

local function MergeServerCharge_CheckDB(human)
    if not human.db.MergeCharge then
        MergeServerCharge_CreatDB(human)
    else
        if human.db.MergeCharge.nBeginTime then
            local nNowBeginTime =  CommonDB.getMergeServerTime()
            if nNowBeginTime ~= human.db.MergeCharge.nBeginTime then
                MergeServerCharge_CreatDB(human)
            end
        end
    end
end

-- 获取是否处理合服前的充值
local function MergeServerCharge_GetDispose(human)
    return human.db.MergeCharge.bDispose
end

-- 设置处理合服前的充值
local function MergeServerCharge_SetDispose(human, nValue)
    human.db.MergeCharge.bDispose = nValue
end

-- 获取第一次充值的时间
local function MergeServerCharge_GetChargeTime(human, nMoney)
    return human.db.MergeCharge.tChargeData[nMoney].nBuyTime
end

-- 设置充值时间
local function MergeServerCharge_SetChargeTime(human, nMoney, nValue)
    human.db.MergeCharge.tChargeData[nMoney].nBuyTime = nValue
end

local function MergeServerCharge_GetBuyDay(human, nMoney)
    return human.db.MergeCharge.tChargeData[nMoney].nBuyDay
end

local function MergeServerCharge_SetBuyDay(human, nMoney,nDay)
    human.db.MergeCharge.tChargeData[nMoney].nBuyDay = nDay
end

-- 获取DB奖励数据
local function MergeServerCharge_GetDBPrizeData(human)
    return human.db.MergeCharge.tPrizeData
end

-- 获取DB奖励状态
local function MergeServerCharge_GetDBPrizeStatus(human, nDay, nMoney)
    local tPrizeData = MergeServerCharge_GetDBPrizeData(human)

    if not tPrizeData[nDay] or not tPrizeData[nDay].tMoney[nMoney] then
        return CommonDefine.COMMON_PRIZE_STATE_NOGET
    end

    return tPrizeData[nDay].tMoney[nMoney].nStatus
end

-- 设置DB奖励状态
local function MergeServerCharge_SetDBPrizeStatus(human, nDay, nMoney, nStatus)
    print("[MergeServerCharge_SetDBPrizeStatus] 设置DB奖励状态 nDay = "..nDay.." nMoney = "..nMoney.." nStatus = "..nStatus)
    local tPrizeData = MergeServerCharge_GetDBPrizeData(human)
    tPrizeData[nDay].tMoney[nMoney].nStatus = nStatus
end

-- 获取DB天数充值金额
local function MergeServerCharge_GetDBDayCharge(human, nDay, nMoney)
    local tPrizeData = MergeServerCharge_GetDBPrizeData(human)

    if not tPrizeData[nDay] then
        return 0
    end

    return tPrizeData[nDay].tMoney[nMoney].nMoney
end

-- 设置DB天数充值金额
local function MergeServerCharge_SetDBDayCharge(human, nDay, nMoney, nAddMoney)
    local tPrizeData = MergeServerCharge_GetDBPrizeData(human)
    tPrizeData[nDay].tMoney[nMoney].nMoney = tPrizeData[nDay].tMoney[nMoney].nMoney + nAddMoney
    if tPrizeData[nDay].tMoney[nMoney].nMoney < 0 then
        tPrizeData[nDay].tMoney[nMoney].nMoney  = 0
    end
end


local function MergeServerCharge_GetDayAllMoney(human, nDay)
    local nAllMoney = 0
    for _, v in ipairs(tIndex2Money) do
        local nMoney = MergeServerCharge_GetDBDayCharge(human, nDay, v)
        nAllMoney = nAllMoney + nMoney
    end

    return nAllMoney
end

-----------------------------------客户端请求-----------------------------
-- 请求数据
function MergeServerCharge_Query(human, nIndex)
    if false == MergeServerCharge_IsOpen() then
        return
    end

    MergeServerCharge_CheckDB(human)
    local nMoney = tIndex2Money[nIndex]
    if not nMoney then
        return
    end

    local tConf = MergeServerCharge_GetCof()
    
    local tMsgData = Msg.gc.GC_MERGESERVER_CHARGE_QUERY
    tMsgData.tList[0] = 0
    
    local nLen = 0
    for nID, v in ipairs(tConf) do
        if v.nMoney == nMoney then
            nLen = nLen + 1
            tMsgData.tList[0] = nLen
            local tPrizeData = tMsgData.tList[nLen]
            tPrizeData.nID = nID
            tPrizeData.nState = MergeServerCharge_GetDBPrizeStatus(human, v.nDay, nMoney)
            tPrizeData.nDay = v.nDay
            tPrizeData.nAllMoney = MergeServerCharge_GetDBDayCharge(human, v.nDay, nMoney)
            tPrizeData.nMoney = v.nMoney
            tPrizeData.item[0] = #v.tPrize


            for i, tItem in ipairs(v.tPrize) do
                local nGoodsID = tItem[1]
                local nGoodsNum = tItem[2]

                Grid.makeItem(tPrizeData.item[i], nGoodsID, nGoodsNum)
            end
        end
    end

    Msg.send(tMsgData, human.fd)
end

-- 请求领取奖励
function MergeServerCharge_Get(human, nIndex)
    if false == MergeServerCharge_IsOpen() then
        return
    end

    local nMoney = tIndex2Money[nIndex]
    if not nMoney then
        return
    end

    local tConf = MergeServerCharge_GetCof()

    local tItem = {}
    for nID, v in ipairs(tConf) do
        if v.nMoney == nMoney then
            local nState = MergeServerCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
            if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
                MergeServerCharge_SetDBPrizeStatus(human, v.nDay, nMoney, CommonDefine.COMMON_PRIZE_STATE_GET)
                for _, tPrize in ipairs(v.tPrize) do
                    if not tItem[tPrize[1]]  then
                        tItem[tPrize[1]] = 0
                    end

                    tItem[tPrize[1]] = tItem[tPrize[1]] + tPrize[2]
                end
            end
        end
    end

    if nil ~= _G.next(tItem) then
        local tPrize = {}
        for nID, nNum in pairs(tItem) do
            table.insert(tPrize, {nID, nNum})
        end
    
        BagLogic.addItemList(human, tPrize, "MergeCharge")
        MergeServerCharge_Query(human, nIndex)

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

----------------------------------------- 外部调用 -------------------------------------
function isRed(human)
    if false == MergeServerCharge_IsOpen() then
        return false
    end

    MergeServerCharge_CheckDB(human)

    if not human.db.MergeCharge then
        return false
    end

    local tConf = MergeServerCharge_GetCof()
    for _, v in ipairs(tConf) do
        local nState = MergeServerCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
        if nState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
            return true
        end
    end

    return false
end

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

    local tConf = MergeServerCharge_GetCof()
    local bQuery = false

    for _, nMoney in ipairs(tIndex2Money) do
        local nBuyTime = MergeServerCharge_GetChargeTime(human, nMoney)
        local nBuyDay = MergeServerCharge_GetBuyDay(human, nMoney)
        if 0 == nBuyTime then
            nBuyTime = os.time()
            MergeServerCharge_SetChargeTime(human, nMoney, nBuyTime)
        end

        if 0 == nBuyDay then
            nBuyDay = nBuyDay + 1
            MergeServerCharge_SetBuyDay(human, nMoney, nBuyDay)
        else
            -- 跨天了
            if nil == Util.isSameDay(nBuyTime) then
                nBuyDay = nBuyDay + 1
                if nBuyDay <= MERGESERVERCHARGE_MAXDAY then
                    MergeServerCharge_SetBuyDay(human, nMoney, nBuyDay)
                end

                nBuyTime = os.time()
                MergeServerCharge_SetChargeTime(human, nMoney, nBuyTime)
            end
        end

        nBuyDay = (nBuyDay > MERGESERVERCHARGE_MAXDAY) and MERGESERVERCHARGE_MAXDAY or nBuyDay
        print("[MergeServerCharge_onCharge] 111 玩家充值增加对应的总充值金额 天数 nBuyDay = "..nBuyDay
            .." 对应金额 nMoney = "..nMoney.." 增加的钱 price = "..price)
        
        MergeServerCharge_SetDBDayCharge(human, nBuyDay, nMoney, price)
        local nNewChareNum = MergeServerCharge_GetDBDayCharge(human, nBuyDay, nMoney)
        print("[MergeServerCharge_onCharge] 玩家充值增加对应的总充值金额 天数 nBuyDay = "..nBuyDay
                .." 对应金额 nMoney = "..nMoney.." 增加的钱 price = "..price.." 总金额 nNewChareNum = "..nNewChareNum)
        for _, v in ipairs(tConf) do
            if v.nDay == nBuyDay and v.nMoney == nMoney then
                local nStatus = MergeServerCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
                if nStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET and nNewChareNum >= v.nMoney then
                    MergeServerCharge_SetDBPrizeStatus(human, v.nDay, v.nMoney, CommonDefine.COMMON_PRIZE_STATE_CANGET)
                    bQuery = true
                end
            end
        end
    end

    if true == bQuery then
        MergeServerLogin.MergeServerLogin_SendRed(human)
        -- MergeServerCharge_Query(human, price)
    end
end

function isOpen(human, YYInfo, funcConfig)
   return MergeServerCharge_IsOpen()
end

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

function onLogin(human)
    if false == MergeServerCharge_IsOpen() then
        return
    end

    MergeServerCharge_CheckDB(human)

    local bDispose = MergeServerCharge_GetDispose(human)
    if false == bDispose then
        MergeServerCharge_SetDispose(human, true)
        local nNowCharge = human.db.topupAcountDaily
        if nNowCharge and 0 < nNowCharge then
            onCharge(human, nNowCharge)
        end
    end

    local nNowTime = os.time()
    for _, nMoney in ipairs(tIndex2Money) do
        local nBuyTime = MergeServerCharge_GetChargeTime(human, nMoney)
        local nBuyDay = MergeServerCharge_GetBuyDay(human, nMoney)
        if true ~= Util.isSameDayByTimes(nNowTime, nBuyTime) and nBuyDay ~= 0 then
            local nAllMoney = MergeServerCharge_GetDBDayCharge(human, nBuyDay, nMoney)
            if nAllMoney < nMoney and 0 < nAllMoney then
                MergeServerCharge_SetDBDayCharge(human, nBuyDay, nMoney, -nAllMoney)
                MergeServerCharge_SetChargeTime(human, nMoney, 0)
                print("[MergeServerCharge_onLogin] 玩家登录 重置对应不达标数据 nBuyDay = "..nBuyDay.." 对应金额 nMoney = "..nMoney.." 充值数 nAllMoney = "..nAllMoney)
            end
        end
     end
end

function onZero(human, funcID)
    if false == MergeServerCharge_IsOpen() then
        return
    end

    MergeServerCharge_CheckDB(human)

    for _, nMoney in ipairs(tIndex2Money) do
       -- local nBuyTime = MergeServerCharge_GetChargeTime(human, nMoney)
        local nBuyDay = MergeServerCharge_GetBuyDay(human, nMoney)

        local nAllMoney = MergeServerCharge_GetDBDayCharge(human, nBuyDay, nMoney)
        if nAllMoney < nMoney and 0 < nAllMoney then
            MergeServerCharge_SetDBDayCharge(human, nBuyDay, nMoney, -nAllMoney)
            MergeServerCharge_SetChargeTime(human, nMoney, 0)
            print("[MergeServerCharge_onZero] 零点刷新 重置对应不达标数据 nBuyDay = "..nBuyDay.." 对应金额 nMoney = "..nMoney.." 充值数 nAllMoney = "..nAllMoney)
        end
    end
end

function MergeServerCharge_GMReset(human, nDay)
    for _, v in ipairs(tIndex2Money) do
        human.db.MergeCharge.tPrizeData[nDay].tMoney[v]=
        {
            nStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET,
            nMoney = 0,
        }
    end
end