--------------------------------
-- 文件名       :   ServerCommerceActCharge.lua
-- 文件说明     :   跨服商业-连充豪礼
-- 创建时间     :   2025/03/31
-- 创建人       :   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 ServerCommerceConf = require("excel.ServerCommerce")
local ServerCommerceManager = require("serverCommerce.ServerCommerceManager")

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

----------------------------------------- 内部处理开始 -------------------------------------
-- 获取配置
local function CommerceActCharge_GetCof()
    return ServerCommerceConf.CommerceCharge
end

-- 初始化DB数据()
function CreatDB(human)
    if not human.db.ServerCommerce or not human.db.ServerCommerce.Charge then
        print("[CommerceActCharge_CreatDB] 初始化数据失败 name = "..human.db.name)
        return false
    end

    human.db.ServerCommerce.Charge = 
    {
        nChargeTime = 0,
        tPrizeData = {}
    }

    local tPrizeData = human.db.ServerCommerce.Charge.tPrizeData
    local tConf = CommerceActCharge_GetCof()
    for _, v in ipairs(tConf) do
        if not tPrizeData[v.nDay] then
            tPrizeData[v.nDay] = {
                nChargeMoney = 0,
                tMoney = {}
            }
        end
       
        local tNodeData = tPrizeData[v.nDay].tMoney
        if not tNodeData[v.nMoney] then
            tNodeData[v.nMoney] = CommonDefine.COMMON_PRIZE_STATE_NOGET
        else
            print("[CommerceActCharge_CreateDB] 配置了重复的数据")
        end
    end

    print("[CommerceActTask_CreatDB] 连充豪礼 初始化数据完成 name = "..human.db.name)
    return true
end

-- 获取第一次充值的时间
local function CommerceActCharge_GetChargeTime(human)
    return human.db.ServerCommerce.Charge.nChargeTime
end

-- 设置充值时间
local function CommerceActCharge_SetChargeTime(human, nTime)
    human.db.ServerCommerce.Charge.nChargeTime = nTime
end

-- 获取DB奖励数据
local function CommerceActCharge_GetDBPrizeData(human)
    return human.db.ServerCommerce.Charge.tPrizeData
end

-- 获取DB奖励状态
local function CommerceActCharge_GetDBPrizeStatus(human, nDay, nMoney)
    local tPrizeData = CommerceActCharge_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]
end

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

-- 获取DB天数充值金额
local function CommerceActCharge_GetDBDayCharge(human, nDay)
    local tPrizeData = CommerceActCharge_GetDBPrizeData(human)

    if not tPrizeData[nDay] then
        return 0
    end

    return tPrizeData[nDay].nChargeMoney
end

-- 设置DB天数充值金额
local function CommerceActCharge_SetDBDayCharge(human, nDay, nMoney)
    local tPrizeData = CommerceActCharge_GetDBPrizeData(human)
    tPrizeData[nDay].nChargeMoney = tPrizeData[nDay].nChargeMoney + nMoney
end


----------------------------------------- 客户端请求处理开始 -------------------------------------
-- 请求数据
function CommerceActCharge_Query(human, nMoney)
    local tConf = CommerceActCharge_GetCof()
    
    local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_CHARGEQUERY
    local tMoney = {}
    local tRed = tMsgData.tRed
    tRed[0] = 0
    tMsgData.list[0] = 0
    
    tMsgData.nMoney = nMoney
    local nChargeDay, nLen = 0, 0
    for nID, v in ipairs(tConf) do
        if v.nMoney == nMoney then
                nLen = nLen + 1
                tMsgData.list[0] = nLen
                local tPrizeData = tMsgData.list[nLen]
                tPrizeData.nID = nID
                tPrizeData.nState = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, nMoney)
                if tPrizeData.nState ~= CommonDefine.COMMON_PRIZE_STATE_NOGET then
                    nChargeDay = nChargeDay + 1
                end
                tPrizeData.nDay = v.nDay
                tPrizeData.nAllMoney = CommerceActCharge_GetDBDayCharge(human, v.nDay)
                tPrizeData.item[0] = #v.Prize

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

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

        if not tMoney[v.nMoney] then
            tMoney[v.nMoney] = 1
            tRed[0] = tRed[0] + 1
            tRed[tChargeMoney2Index[v.nMoney]] = 0
        end

        if tRed[tChargeMoney2Index[v.nMoney]] == 0 then
            local nState = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
            if nState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
                tRed[tChargeMoney2Index[v.nMoney]] = 1
            end
        end
    end

    tMsgData.nChargeDay = nChargeDay
    Msg.send(tMsgData, human.fd)
end

-- 请求领取奖励
function CommerceActCharge_GetPrize(human, nMoney)
    local tConf = CommerceActCharge_GetCof()

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

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

    local tPrize = {}
    for nID, nNum in pairs(tItem) do
        table.insert(tPrize, {nID, nNum})
    end

    BagLogic.addItemList(human, tPrize, "servercommerce")
    CommerceActCharge_Query(human, nMoney)
    ServerCommerceManager.CommerceAct_SendActInfo(human)
end

----------------------------------------- 外部调用 -------------------------------------
function isRed(human)
    if not human.db.ServerCommerce or not human.db.ServerCommerce.Charge then
        return false
    end

    local tConf = CommerceActCharge_GetCof()
    for _, v in ipairs(tConf) do
        local nState = CommerceActCharge_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)
    local nBuyTime = CommerceActCharge_GetChargeTime(human)
    if 0 == nBuyTime then
        nBuyTime = os.time()
        CommerceActCharge_SetChargeTime(human, nBuyTime)
    end

    local nDiffDay = Util.diffDay(nBuyTime) + 1

    local tConf = CommerceActCharge_GetCof()
    local bQuery = false

    local nNewChareNum = CommerceActCharge_GetDBDayCharge(human, nDiffDay)
    nNewChareNum = nNewChareNum + price
    
    for _, v in ipairs(tConf) do
        if v.nDay == nDiffDay then
            local nStatus = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
            if nStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET and nNewChareNum >= v.nMoney then
                CommerceActCharge_SetDBPrizeStatus(human, v.nDay, v.nMoney, CommonDefine.COMMON_PRIZE_STATE_CANGET)
                bQuery = true
            end
        end
    end

    CommerceActCharge_SetDBDayCharge(human, nDiffDay, price)

    if true == bQuery then
        CommerceActCharge_Query(human, price)
        ServerCommerceManager.CommerceAct_SendActInfo(human)
    end
end