--------------------------------
-- 文件名       :   BusOneActivityYuanZheng.lua
-- 文件说明     :   新商业活动-远征豪礼
-- 创建时间     :   2025/08/06
-- 创建人       :   FC
--------------------------------

local tYuanZhengConf = require("excel.BusoneActivity").YuanZhengGift
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 BusOneActivityTask = require("absAct.BusOneActivityTask")


local BUSONEYUANZHENGABSID = 7203        -- 对应ABS活动ID

local BUSONEYUANZHENG_PAY_NOGET = 0     -- 不可领取
local BUSONEYUANZHENG_PAY_BUY = 1       -- 已购买
local BUSONEYUANZHENG_PAY_HUOYUE = 2    -- 活跃度到达要求
local BUSONEYUANZHENG_PAY_CANGET = 3    -- 可以领取
local BUSONEYUANZHENG_PAY_GET = 4       -- 已领取

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

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

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

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

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

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

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

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

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

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

-----------------------------------客户端请求-----------------------------
function BusOneYuanZheng_Query(human)
    BusOneYuanZheng_CheckAndResetDB(human)

    local tMsgData = Msg.gc.GC_NEW_BUSONEACT_YUANZHENG_QUERY
    tMsgData.tList[0] = 0
    for nID, v in ipairs(tYuanZhengConf) do
        tMsgData.tList[0] = tMsgData.tList[0] + 1
        local tData = tMsgData.tList[tMsgData.tList[0]]
        tData.nID = nID
        tData.nFreeStatus = BusOneYuanZheng_GetFreeStatus(human, nID)
        local nPayStatus = BusOneYuanZheng_GetPayStatus(human, nID)
        tData.nPayStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET
        tData.bBuy = 0
        if nPayStatus == BUSONEYUANZHENG_PAY_CANGET then
            tData.nPayStatus = CommonDefine.COMMON_PRIZE_STATE_CANGET
        elseif BUSONEYUANZHENG_PAY_GET == nPayStatus then
            tData.nPayStatus = CommonDefine.COMMON_PRIZE_STATE_GET
        end

        if nPayStatus ~= BUSONEYUANZHENG_PAY_NOGET and BUSONEYUANZHENG_PAY_HUOYUE ~= nPayStatus then
            tData.bBuy = 1
        end

        tData.nFinishNum = BusOneYuanZheng_GetBuyNum(human)
        tData.nNeedNum = v.nNum

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

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

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

        tData.tPayPrize[0] = #v.tPayPrize
        for i, tItem in ipairs(v.tPayPrize) 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 BusOneYuanZheng_Get(human)
    local tGoods = {}
    for nID, v in ipairs(tYuanZhengConf) do
        local nFreeStatus = BusOneYuanZheng_GetFreeStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus then
            BusOneYuanZheng_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 = BusOneYuanZheng_GetPayStatus(human, nID)
        if BUSONEYUANZHENG_PAY_CANGET == nPayStatus then
            BusOneYuanZheng_SetPayStatus(human, nID, BUSONEYUANZHENG_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, "BusOneYuanZheng")
        BusOneYuanZheng_Query(human)

        YunYingLogic.sendBanner(human)
        BusOneActivityTask.BusOneTask_SendRed(human)
    end
end

-----------------------------------外部调用-----------------------------
function isOpen(human, YYInfo, funcConfig)
    print("[BusOneYuanZheng_isOpen] 进入判断")
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig and funcConfig.funcID or BUSONEYUANZHENGABSID)
    if not state then 
        print("[BusOneYuanZheng_isOpen] 当前活动未开启")
        return 
    end
    print("[BusOneYuanZheng_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)
    BusOneYuanZheng_CheckAndResetDB(human)

    for nID, v in ipairs(tYuanZhengConf) do
        local nFreeStatus = BusOneYuanZheng_GetFreeStatus(human, nID)
        local nPayStatus = BusOneYuanZheng_GetPayStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus or
            BUSONEYUANZHENG_PAY_CANGET == nPayStatus then
            return true
        end
    end

    return false
end

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

    BusOneYuanZheng_CheckAndResetDB(human)

    for nID, v in ipairs(tYuanZhengConf) do
        if v.nBuyID == buyID then
            local nPayStatus = BusOneYuanZheng_GetPayStatus(human, nID)
            if BUSONEYUANZHENG_PAY_NOGET == nPayStatus then
                BusOneYuanZheng_SetPayStatus(human, nID, BUSONEYUANZHENG_PAY_BUY)
            elseif BUSONEYUANZHENG_PAY_HUOYUE == nPayStatus then
                BusOneYuanZheng_SetPayStatus(human, nID, BUSONEYUANZHENG_PAY_CANGET)
            end
        end
    end

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

    BusOneYuanZheng_Query(human)
    BusOneActivityTask.BusOneTask_SendRed(human)
end

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

    BusOneYuanZheng_CheckAndResetDB(human)
    local nOldNum = BusOneYuanZheng_GetBuyNum(human)
    BusOneYuanZheng_SetBuyNum(human, nNum)

    local nNewNum = nOldNum + nNum
    local bSend = false
    for nID, v in ipairs(tYuanZhengConf) do
        if nNewNum >= v.nNum then
            local nFreeStatus = BusOneYuanZheng_GetFreeStatus(human, nID)
            local nPayStatus = BusOneYuanZheng_GetPayStatus(human, nID)
            if nFreeStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                BusOneYuanZheng_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
                bSend = true
            end

            if BUSONEYUANZHENG_PAY_NOGET == nPayStatus then
                BusOneYuanZheng_SetPayStatus(human, nID, BUSONEYUANZHENG_PAY_HUOYUE)
                bSend = true
            elseif BUSONEYUANZHENG_PAY_BUY == nPayStatus then
                BusOneYuanZheng_SetPayStatus(human, nID, BUSONEYUANZHENG_PAY_CANGET)
                bSend = true
            end
        end
    end

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

-- function GetRemainNum(human, nBuyID)
--     for nID, v in ipairs(tYuanZhengConf) do
--         if v.nBuyID == nBuyID then
--             local nPayStatus = BusOneYuanZheng_GetPayStatus(human, nID)
--             if BUSONEYUANZHENG_PAY_NOGET == nPayStatus or BUSONEYUANZHENG_PAY_HUOYUE == nPayStatus then
--                 return 1
--             else
--                 return 0
--             end
--         end
--     end

--     return 0
-- end