--------------------------------
-- 文件名       :   BusOneActivityBuy.lua
-- 文件说明     :   新商业活动-购买
-- 创建时间     :   2025/08/05
-- 创建人       :   FC
--------------------------------

local tBuyConf = require("excel.BusoneActivity").Buy
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 ObjHuman = require("core.ObjHuman")
local BusOneActivityTask = require("absAct.BusOneActivityTask")
local YunYingLogic = require("yunying.YunYingLogic")

local BUSONEBUYABSID = 7202        -- 对应ABS活动ID
local BUSONEBUY_TYPE_MONEY = 2      -- 钱购买

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

    human.db.absAct[BUSONEBUYABSID].tBuyInfo = {}
    for nID, v in ipairs(tBuyConf) do
        human.db.absAct[BUSONEBUYABSID].tBuyInfo[nID] = {
            nBuyNum = 0,
            nStatus = CommonDefine.COMMON_PRIZE_STATE_NOGET
        }
    end
end

-- 重置DB
local function BusOneBuy_CheckAndResetDB(human)
    if not human.db.absAct[BUSONEBUYABSID] or nil == _G.next(human.db.absAct[BUSONEBUYABSID]) 
        or not human.db.absAct[BUSONEBUYABSID].tBuyInfo then
        BusOneBuy_CreatDB(human)
    end
end

-- 获取购买次数
local function BusOneBuy_GetBuyNum(human, nID)
    return human.db.absAct[BUSONEBUYABSID].tBuyInfo[nID].nBuyNum
end

-- 设置购买次数
local function BusOneBuy_SetBuyNum(human, nID, nAddNum)
    human.db.absAct[BUSONEBUYABSID].tBuyInfo[nID].nBuyNum = human.db.absAct[BUSONEBUYABSID].tBuyInfo[nID].nBuyNum  + nAddNum
end

-- 获取购买状态
local function BusOneBuy_GetBuyStatus(human, nID)
    return human.db.absAct[BUSONEBUYABSID].tBuyInfo[nID].nStatus
end

-- 设置购买状态
local function BusOneBuy_SetBuyStatus(human, nID, nValue)
    human.db.absAct[BUSONEBUYABSID].tBuyInfo[nID].nStatus = nValue
end

-----------------------------------客户端请求-----------------------------
function BusOneBuy_Query(human)
    BusOneBuy_CheckAndResetDB(human)
    
    local tMsgData = Msg.gc.GC_NEW_BUSONEACT_BUY_QUERY
    tMsgData.tList[0] = 0
    for nID, v in ipairs(tBuyConf) do
        tMsgData.tList[0] = tMsgData.tList[0] + 1
        local tData = tMsgData.tList[tMsgData.tList[0]]
        tData.nID = nID
        tData.nStatus = BusOneBuy_GetBuyStatus(human, nID)
        tData.nNowBuyNum = BusOneBuy_GetBuyNum(human, nID)
        tData.nBuyNum = v.nBuyNum
        tData.nType = v.nType
        
        BuyLogic.fontBuyItem(human, tData.tBuyItem, v.nBuyID)
        if v.nBuyID == 0 then
            tData.tBuyItem.cost = v.nPrize
        end

        tData.tPrize[0] = #v.tPrize
        for i, tItem in ipairs(v.tPrize) do
            Grid.makeItem(tData.tPrize[i], tItem[1], tItem[2])
        end
    end

    Msg.send(tMsgData, human.fd)
end

function BusOneBuy_Buy(human, nID)
    local tConf = tBuyConf[nID]
    if not tConf then
        -- print("[BusOneBuy_Buy] 不存在对应的配置 返回 nID = "..nID)
        return
    end

    if tConf.nType == BUSONEBUY_TYPE_MONEY then
        -- print("[BusOneBuy_Buy] 配置对应的类型不正确 返回 nID = "..nID.." nType = "..tConf.nType)
        return
    end

    local nNowBuyNum = BusOneBuy_GetBuyNum(human, nID)
    if nNowBuyNum >= tConf.nBuyNum then
        -- print("[BusOneBuy_Buy] 当前购买的次数大于可购买的次数 返回 nID = "..nID.." nType = "
        --         ..tConf.nType.." nNowBuyNum = "..nNowBuyNum)
        return
    end

    if not ObjHuman.checkRMB(human, tConf.nPrize) then
        -- print("[BusOneBuy_Buy] 当前购买的金币不足 返回 nID = "..nID.." nType = "
        --     ..tConf.nType.." nNowBuyNum = "..nNowBuyNum)
		return
	end
	
	-- 扣消耗
	ObjHuman.decZuanshi(human, -tConf.nPrize, "BusOneBuy")
    BusOneBuy_SetBuyNum(human, nID, 1)

    -- BusOneBuy_SetBuyStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
    BagLogic.addItemList(human, tConf.tPrize, "BusOneBuy")

    BusOneBuy_Query(human)
end

function BusOneBuy_Get(human, itemMul)
    itemMul = itemMul or 1
    local tItem = {}
    for nID, v in pairs(tBuyConf) do
        local nStatus = BusOneBuy_GetBuyStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nStatus then
            for i, tData in ipairs(v.tPrize) do
                if not tItem[tData[1]] then
                    tItem[tData[1]] = 0
                end

                tItem[tData[1]] = tItem[tData[1]] + tData[2] * itemMul
            end

            local nNowBuyNum = BusOneBuy_GetBuyNum(human, nID)
            if nNowBuyNum >= v.nBuyNum then
                BusOneBuy_SetBuyStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
            else
                BusOneBuy_SetBuyStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_NOGET)
            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, "BusOneBuy")
        BusOneBuy_Query(human)
        BusOneActivityTask.BusOneTask_SendRed(human)
    end
end

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

    -- print("[BusOneActivityBuy_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)
    BusOneBuy_CheckAndResetDB(human)
   
    for nID, v in pairs(tBuyConf) do
        local nStatus = BusOneBuy_GetBuyStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nStatus then
            return true
        end
    end

    return false
end

function onCharge(human, price, funcID, buyID, buyNum)
    -- print("[BusOneActivityBuy_onCharge] 进入购买 buyID = "..buyID)
    local bRed = false
    for nID, v in pairs(tBuyConf) do
        if buyID == v.nBuyID then
            local nStatus = BusOneBuy_GetBuyStatus(human, nID)
            if nStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET then
                BusOneBuy_SetBuyStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
                -- BusOneBuy_SetBuyNum(human, nID, 1)
                BusOneBuy_SetBuyNum(human, nID, buyNum)
                bRed = true
            end
        end
    end

    if true == bRed then
        -- print("[BusOneActivityBuy_onCharge] 进入发送数据 buyID = "..buyID)
        BusOneBuy_Get(human, buyNum)
        YunYingLogic.sendBanner(human)
        BusOneActivityTask.BusOneTask_SendRed(human)
    end
end

function GetRemainNum(human, nBuyID)
    for nID, v in pairs(tBuyConf) do
        if nBuyID == v.nBuyID then
            local nBuyNum = BusOneBuy_GetBuyNum(human, nID)
            if v.nBuyNum > nBuyNum then
                -- return 1
                return v.nBuyNum - nBuyNum
            else
                return 0
            end
        end
    end

    return 0
end