--------------------------------
-- 文件名       :   CommonActCharge.lua
-- 文件说明     :   通用节日活动-连充豪礼
-- 创建时间     :   2025/09
-- 创建人       :   FC
--------------------------------

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local AbsActLogic = require("absAct.AbsActLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Config = require("excel.commonact").allcharge
local CommonDefine = require("common.CommonDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local AbsActExcel = require("excel.absAct")


local COMMONACTCHARGEABSID = 7503

----------------------------------------- 内部处理开始 -------------------------------------
-- 获取配置
local function CommonActCharge_GetCof()
    return Config
end

-- 初始化DB数据()
function CreatDB(human)
   if not human.db.absAct[COMMONACTCHARGEABSID] then
        human.db.absAct[COMMONACTCHARGEABSID] = {}
    end

    if not human.db.absAct[COMMONACTCHARGEABSID].tPrizeData then
        human.db.absAct[COMMONACTCHARGEABSID].tPrizeData = {}
    end
    
    human.db.absAct[COMMONACTCHARGEABSID].nAllMoney = 0
    for index, v in ipairs(Config) do
        human.db.absAct[COMMONACTCHARGEABSID].tPrizeData[index] = CommonDefine.COMMON_PRIZE_STATE_NOGET
    end

    return true
end

-- 检测并创建DB数据
local function CheckAndCreateDB(human)
    if not human.db.absAct[COMMONACTCHARGEABSID] or not human.db.absAct[COMMONACTCHARGEABSID].tPrizeData then
        CreatDB(human)
    end

    -- 处理新增配置
    local tPrizeData = human.db.absAct[COMMONACTCHARGEABSID].tPrizeData
    if tPrizeData and #tPrizeData < #Config then
        for i= #tPrizeData+1, #Config do
            tPrizeData[i] = CommonDefine.COMMON_PRIZE_STATE_NOGET
        end
    end
end 

-- 获取DB奖励数据
local function CommonActCharge_GetDBPrizeData(human)
    return human.db.absAct[COMMONACTCHARGEABSID].tPrizeData
end

-- 获取DB奖励状态
local function CommonActCharge_GetDBPrizeStatus(human, nIndex)
    local tPrizeData = CommonActCharge_GetDBPrizeData(human)
    return tPrizeData[nIndex]
end

-- 设置DB奖励状态
local function CommonActCharge_SetDBPrizeStatus(human, nIndex, nStatus)
    local tPrizeData = CommonActCharge_GetDBPrizeData(human)
    tPrizeData[nIndex] = nStatus
end

-- 获取DB天数充值金额
local function CommonActCharge_GetDBDayCharge(human)
    return human.db.absAct[COMMONACTCHARGEABSID].nAllMoney
end

-- 设置DB天数充值金额
local function CommonActCharge_SetDBDayCharge(human, nAddMoney)
    human.db.absAct[COMMONACTCHARGEABSID].nAllMoney = human.db.absAct[COMMONACTCHARGEABSID].nAllMoney + nAddMoney
end


----------------------------------------- 客户端请求处理开始 -------------------------------------
-- 请求数据
function CommonActCharge_Query(human)
    CheckAndCreateDB(human)
    
    local tConf = CommonActCharge_GetCof()
    local tMsgData = Msg.gc.GC_ABS_FESTIVAL_RECHARGE_QUERY

    tMsgData.tPrize[0] = 0
    tMsgData.nNowCharge = CommonActCharge_GetDBDayCharge(human)
    local nLen = 0
    for nID, v in ipairs(tConf) do
        nLen = nLen + 1
        local tPrizeData = tMsgData.tPrize[nLen]
        tPrizeData.nID = nID
        tPrizeData.nState = CommonActCharge_GetDBPrizeStatus(human, nID)
        tPrizeData.nNeedCharge = v.nMoney

        tPrizeData.tPrize[0] = #v.tPrize
        for nIndex, tItem in ipairs(v.tPrize) do
            local nGoodsID = tItem[1]
            local nGoodsNum = tItem[2]

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

    tMsgData.tPrize[0] = nLen
    Msg.send(tMsgData, human.fd)
end

-- 请求领取奖励
function CommonActCharge_Get(human, nIndex)
    local tConf = CommonActCharge_GetCof()

    local tItem = {}
    for nID, v in ipairs(tConf) do
        local nState = CommonActCharge_GetDBPrizeStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
            CommonActCharge_SetDBPrizeStatus(human, nID, 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

    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, "CommonActCharge")
        CommonActCharge_Query(human)
        YunYingLogic.sendBanner(human)
        local config = AbsActExcel.absActivity[COMMONACTCHARGEABSID]
        YunYingLogic.sendGroupUpdate(YYInfo[COMMONACTCHARGEABSID], human, config.panelID)
    end
end

----------------------------------------- 外部调用 -------------------------------------
function isRed(human)
    local state, nEndTime, nOpenTime = AbsActLogic.isStarted(human, COMMONACTCHARGEABSID)
    if not state then
        return false
    end

    CheckAndCreateDB(human)

    local tConf = CommonActCharge_GetCof()
    for nID, v in ipairs(tConf) do
        local nState = CommonActCharge_GetDBPrizeStatus(human, nID)
        if nState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
            return true
        end
    end

    return false
end

function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig and funcConfig.funcID or COMMONACTCHARGEABSID)
    if not state then
        print("[CommonActCharge_isOpen] 当前活动未开启")
        return 
    end
    -- print("[CommonActCharge__isOpen] 进入判断 endTime = "..endTime.." startTime = "..startTime)
    return true, endTime, startTime
end

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

function onCharge(human, price, funcID, buyID)
   local state, endTime, startTime = AbsActLogic.isStarted(human, COMMONACTCHARGEABSID)
    if not state then 
        return 
    end

    CheckAndCreateDB(human)

    local tConf = CommonActCharge_GetCof()
    local bQuery = false

    local nNewChareNum = CommonActCharge_GetDBDayCharge(human)
    nNewChareNum = nNewChareNum + price
    
    for nID, v in ipairs(tConf) do
        local nStatus = CommonActCharge_GetDBPrizeStatus(human, nID)
        if nStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET and nNewChareNum >= v.nMoney then
            CommonActCharge_SetDBPrizeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
            bQuery = true
        end
    end

    CommonActCharge_SetDBDayCharge(human, price)

    if true == bQuery then
        CommonActCharge_Query(human)
        YunYingLogic.sendBanner(human)
        local config = AbsActExcel.absActivity[COMMONACTCHARGEABSID]
        YunYingLogic.sendGroupUpdate(YYInfo[COMMONACTCHARGEABSID], human, config.panelID)
    end
end

function onLogin(human)
   local state, endTime, startTime = AbsActLogic.isStarted(human, COMMONACTCHARGEABSID)
    if not state then 
        return 
    end

    CheckAndCreateDB(human)
    if not human.db.absAct[COMMONACTCHARGEABSID].IsAddCharge then
        human.db.absAct[COMMONACTCHARGEABSID].IsAddCharge = true
        if human.db.topupAcountDaily and human.db.topupAcountDaily > 0 then
            onCharge(human, human.db.topupAcountDaily)
        end
    end
end