--------------------------------
-- 文件名       :   BusThreeActGrandGift.lua
-- 文件说明     :   新商业活动3-天选豪礼
-- 创建时间     :   2025/09/01
-- 创建人       :   FC
--------------------------------

local tGrandGiftConf = require("excel.BusThreeActivity").GrandGift
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 AbsActLogic = require("absAct.AbsActLogic")
local BuyLogic = require("topup.BuyLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local AbsActExcel = require("excel.absAct")


local BUSTHREEGRANDGIFTABSID = 7402        -- 对应ABS活动ID

local BUSTHREEGRANDGIFT_PAY_NOGET = 0     -- 不可领取
local BUSTHREEGRANDGIFT_PAY_BUY = 1       -- 已购买
local BUSTHREEGRANDGIFT_PAY_HUOYUE = 2    -- 活跃度到达要求
local BUSTHREEGRANDGIFT_PAY_CANGET = 3    -- 可以领取
local BUSTHREEGRANDGIFT_PAY_GET = 4       -- 已领取

----------------------------------------- 内部处理开始 -------------------------------------
-- 创建DB
local function BusThreeGrandGift_CreatDB(human)
    if not human.db.absAct[BUSTHREEGRANDGIFTABSID] then
        human.db.absAct[BUSTHREEGRANDGIFTABSID] = {}
    end

    human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo = {
        nNum = 0,
        tStatus = {}
    }

    local tStatus = human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.tStatus
    for nID, v in ipairs(tGrandGiftConf) do
        tStatus[nID] = {
            nFreeStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET,
            nPayStatus = BUSTHREEGRANDGIFT_PAY_NOGET
        }
    end
end

-- 重置DB
local function BusThreeGrandGift_CheckAndResetDB(human)
    if not human.db.absAct[BUSTHREEGRANDGIFTABSID] or nil == _G.next(human.db.absAct[BUSTHREEGRANDGIFTABSID]) 
        or not human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo then
        BusThreeGrandGift_CreatDB(human)
    end

    local tStatus = human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.tStatus
    if tStatus and #tStatus < #tGrandGiftConf then
        for i=#tStatus+1, #tGrandGiftConf do
            tStatus[i] = {
            nFreeStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET,
            nPayStatus = BUSTHREEGRANDGIFT_PAY_NOGET
        }
        end
    end
end

-- 获取当前远征次数
local function BusThreeGrandGift_GetBuyNum(human)
    return human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.nNum
end

-- 设置购买次数
local function BusThreeGrandGift_SetBuyNum(human, nAddNum)
    human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.nNum = human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.nNum + nAddNum
end

-- 获取当前免费奖励状态
local function BusThreeGrandGift_GetFreeStatus(human, nID)
    return human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.tStatus[nID].nFreeStatus
end

-- 设置免费奖励状态
local function BusThreeGrandGift_SetFreeStatus(human, nID, nValue)
    human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.tStatus[nID].nFreeStatus = nValue
end

-- 获取当前付费奖励状态
local function BusThreeGrandGift_GetPayStatus(human, nID)
    return human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.tStatus[nID].nPayStatus
end

-- 设置付费奖励状态
local function BusThreeGrandGift_SetPayStatus(human, nID, nValue)
    human.db.absAct[BUSTHREEGRANDGIFTABSID].tGiftInfo.tStatus[nID].nPayStatus = nValue
end

-----------------------------------客户端请求-----------------------------
function BusThreeGrandGift_Query(human)
    BusThreeGrandGift_CheckAndResetDB(human)

    local tMsgData = Msg.gc.GC_NEW_BUSTHREEACT_GRANDGIFT_QUERY
    tMsgData.tList[0] = 0
    for nID, v in ipairs(tGrandGiftConf) do
        tMsgData.tList[0] = tMsgData.tList[0] + 1
        local tData = tMsgData.tList[tMsgData.tList[0]]
        tData.nID = nID
        tData.nFreeStatus = BusThreeGrandGift_GetFreeStatus(human, nID)
        local nPayStatus = BusThreeGrandGift_GetPayStatus(human, nID)
        tData.nPayStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET
        tData.bBuy = 0
        if nPayStatus == BUSTHREEGRANDGIFT_PAY_CANGET then
            tData.nPayStatus = CommonDefine.COMMON_PRIZE_STATE_CANGET
        elseif BUSTHREEGRANDGIFT_PAY_GET == nPayStatus then
            tData.nPayStatus = CommonDefine.COMMON_PRIZE_STATE_GET
        end

        if nPayStatus ~= BUSTHREEGRANDGIFT_PAY_NOGET and BUSTHREEGRANDGIFT_PAY_HUOYUE ~= nPayStatus then
            tData.bBuy = 1
        end

        tData.nFinishNum = BusThreeGrandGift_GetBuyNum(human)
        tData.nNeedNum = v.nNeedNum

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

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

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

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

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

    Msg.send(tMsgData, human.fd)
end

function BusThreeGrandGift_Get(human)
    local tGoods = {}
    for nID, v in ipairs(tGrandGiftConf) do
        local nFreeStatus = BusThreeGrandGift_GetFreeStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus then
            BusThreeGrandGift_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
            for _, tItem in ipairs(v.FreePrize) 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 = BusThreeGrandGift_GetPayStatus(human, nID)
        if BUSTHREEGRANDGIFT_PAY_CANGET == nPayStatus then
            BusThreeGrandGift_SetPayStatus(human, nID, BUSTHREEGRANDGIFT_PAY_GET)
            for _, tItem in ipairs(v.PayPrize) 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, "BusThreeGrandGift")
        BusThreeGrandGift_Query(human)

        YunYingLogic.sendBanner(human)
        local config = AbsActExcel.absActivity[BUSTHREEGRANDGIFTABSID]
        YunYingLogic.sendGroupUpdate(YYInfo[BUSTHREEGRANDGIFTABSID], human, config.panelID)
    end
end

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

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

function isRed(human, YYInfo, absActConfig)
    BusThreeGrandGift_CheckAndResetDB(human)

    for nID, v in ipairs(tGrandGiftConf) do
        local nFreeStatus = BusThreeGrandGift_GetFreeStatus(human, nID)
        local nPayStatus = BusThreeGrandGift_GetPayStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus or
            BUSTHREEGRANDGIFT_PAY_CANGET == nPayStatus then
            return true
        end
    end

    return false
end

function onCharge(human, price, funcID, buyID)
    -- print("[BusThreeGrandGift_onCharge] 进入购买 buyID = "..buyID)

    BusThreeGrandGift_CheckAndResetDB(human)

    for nID, v in ipairs(tGrandGiftConf) do
        if v.nBuyID == buyID then
            local nPayStatus = BusThreeGrandGift_GetPayStatus(human, nID)
            if BUSTHREEGRANDGIFT_PAY_NOGET == nPayStatus then
                BusThreeGrandGift_SetPayStatus(human, nID, BUSTHREEGRANDGIFT_PAY_BUY)
            elseif BUSTHREEGRANDGIFT_PAY_HUOYUE == nPayStatus then
                BusThreeGrandGift_SetPayStatus(human, nID, BUSTHREEGRANDGIFT_PAY_CANGET)
            end
        end
    end

    -- print("[BusThreeGrandGift_onCharge] 发送协议完成")

    BusThreeGrandGift_Query(human)
    -- BusOneActivityTask.BusOneTask_SendRed(human)
end

function BusThreeGrandGift_NumChange(human, nNum)
    if 0 >= nNum then
        return
    end

    BusThreeGrandGift_CheckAndResetDB(human)
    local nOldNum = BusThreeGrandGift_GetBuyNum(human)
    BusThreeGrandGift_SetBuyNum(human, nNum)

    local nNewNum = nOldNum + nNum
    local bSend = false
    for nID, v in ipairs(tGrandGiftConf) do
        if nNewNum >= v.nNeedNum then
            local nFreeStatus = BusThreeGrandGift_GetFreeStatus(human, nID)
            local nPayStatus = BusThreeGrandGift_GetPayStatus(human, nID)
            if nFreeStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                BusThreeGrandGift_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
                bSend = true
            end

            if BUSTHREEGRANDGIFT_PAY_NOGET == nPayStatus then
                BusThreeGrandGift_SetPayStatus(human, nID, BUSTHREEGRANDGIFT_PAY_HUOYUE)
                bSend = true
            elseif BUSTHREEGRANDGIFT_PAY_BUY == nPayStatus then
                BusThreeGrandGift_SetPayStatus(human, nID, BUSTHREEGRANDGIFT_PAY_CANGET)
                bSend = true
            end
        end
    end

    if true == bSend then
        YunYingLogic.sendBanner(human)
        -- BusThreeActivityTask.BusOneTask_SendRed(human)
    end
end

-- function GetRemainNum(human, nBuyID)
--     for nID, v in ipairs(tGrandGiftConf) do
--         if v.nBuyID == nBuyID then
--             local nPayStatus = BusThreeGrandGift_GetPayStatus(human, nID)
--             if nPayStatus == BUSTHREEGRANDGIFT_PAY_NOGET or BUSTHREEGRANDGIFT_PAY_HUOYUE == nPayStatus then
--                 return 1
--             else
--                 return 0
--             end
--         end
--     end
-- end