-----------------------------------------------------------------
-- 文件名       ：  OneKillGift.lua
-- 文件说明     ：  一元礼包
-- 创建时间     :   2025/07/22
-- 创建人       :   FC
-----------------------------------------------------------

local Msg = require("core.Msg")
local Util = require("common.Util")
local Grid = require("bag.Grid")
local OnekillConf = require("excel.OneKillGift")
local BuyLogic = require("topup.BuyLogic")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local ObjHuman = require("core.ObjHuman")


local ONEKILLGIFT_MAXBUYNUM = 10        -- 最大购买次数
local ONEKILLGIFT_NOJINGBI = 1          -- 金币不足
local ONEKILLGIFT_SHOW = 2              -- 展示
local ONEKILLGIFT_ONLOGIN = 3           -- 登录

local ONEKILLGIFT_NOSEND = 1            -- 不发送

local nDurationTime = 4 * 60 * 60       -- 4小时

----------------------------------------- 内部处理开始 -------------------------------------
local function OneKillGift_CreateDB(human)
    human.db.OneKillGift = {
        nBuyNum = 0,
        nEndTime = 0,       -- 每轮结束时间
        nNoShow = 0,
        nBeginTime = os.time()
    }
end

local function OneKillGift_CheckCreate(human)
    if not human.db.OneKillGift then
        OneKillGift_CreateDB(human)
    end
end

local function OneKillGift_ResetDB(human)
    OneKillGift_CreateDB(human)
end

-- 购买后操作
local function OneKillGift_BuyGift(human)
    human.db.OneKillGift.nBuyNum = human.db.OneKillGift.nBuyNum + 1
    human.db.OneKillGift.nEndTime = 0
    if human.db.OneKillGift.nBuyNum >= ONEKILLGIFT_MAXBUYNUM then
        human.db.OneKillGift.nNoShow = 1
    end
end

-- 获取购买次数
local function OneKillGift_GetBuyNum(human)
    return human.db.OneKillGift.nBuyNum
end

-- 获取结束时间
local function OneKillGift_GetEndTime(human)
    return human.db.OneKillGift.nEndTime
end

-- 获取是否显示
local function OneKillGift_GetShow(human)
    return human.db.OneKillGift.nNoShow
end

-- 获取开始时间
local function OneKillGift_GetBeginTime(human)
    return human.db.OneKillGift.nBeginTime
end

-- 设置结束时间
local function OneKillGift_SetEndTime(human, nValue)
    human.db.OneKillGift.nEndTime = nValue
end

-- 设置是否显示
local function OneKillGift_SetShow(human, nValue)
    human.db.OneKillGift.nNoShow = nValue
end

-- 发送数据
function OneKillGift_SendMsg(human, bIsOpen)
    local nNowTime = os.time()
    local nBuyNum = OneKillGift_GetBuyNum(human)
    local nNextBuyNum = nBuyNum + 1
    local tConf = OnekillConf.Sheet1[nNextBuyNum]
    if not tConf then
        print("[OneKillGift_SendMsg] 不存在对应的配置 nBuyNum = "..nBuyNum.." nNextBuyNum = "..nNextBuyNum)
        return
    end

    local tMsgData = Msg.gc.GC_ONEKILLGIFT_QUERY
    tMsgData.nBuyNum = nBuyNum
    tMsgData.bIsOpen = bIsOpen
    tMsgData.nLeftTime = OneKillGift_GetEndTime(human) - nNowTime

    print("[OneKillGift_SendMsg] nBuyNum = "..tMsgData.nBuyNum.." bIsOpen = "..tMsgData.bIsOpen.." nLeftTime = "..tMsgData.nLeftTime)
    BuyLogic.fontBuyItem(human, tMsgData.buyItem, tConf.nBuyID)

    --table.print_lua_table(tMsgData.buyItem)
    tMsgData.tList[0] = #tConf.tPrize
    for i, v in ipairs(tConf.tPrize) do
        print("[OneKillGift_SendMsg] i = "..i.." nGoodsID = "..v[1].." nGoodsNum = "..v[2])
        Grid.makeItem(tMsgData.tList[i], v[1], v[2])
       -- table.print_lua_table(tMsgData.tList[i])
    end

    Msg.send(tMsgData, human.fd)
    print("[OneKillGift_SendMsg] 发送数据完成")
end

----------------------------------------- 客户端请求 -------------------------------------
function OneKillGiftLogic_Query(human, bOperate)
    print("[OneKillGiftLogic_Query] 收到消息")
    local bIsOpen = ONEKILLGIFT_ONLOGIN == bOperate and 0 or 1

    OneKillGift_CheckCreate(human)
    local bShow = OneKillGift_GetShow(human)
    if ONEKILLGIFT_NOSEND == bShow then
        print("[OneKillGiftLogic_Query] 当前已经设置了不再展示，直接返回")
        return
    end

    local nNowTime = os.time()
    local nOldEndTime = OneKillGift_GetEndTime(human)
    if nNowTime >= nOldEndTime and 0 ~= nOldEndTime then
        print("[OneKillGiftLogic_Query] 当前结束时间不为0, 且当前时间大于结束时间，设置为不展示直接返回")
        OneKillGift_SetShow(human, ONEKILLGIFT_NOSEND)
        return
    end

    if ONEKILLGIFT_NOJINGBI ==  bOperate then
        if nOldEndTime ~= 0 and nOldEndTime > nNowTime then
            print("[OneKillGiftLogic_Query] 当前结束时间不为0, 且结束时间大于当前时间，")
            return
        end

        local nEndTime = nNowTime + nDurationTime
        OneKillGift_SetEndTime(human, nEndTime)
    end

    OneKillGift_SendMsg(human, bIsOpen)
    print("[OneKillGiftLogic_Query] 发送客户端协议完成")
end

function onLogin(human)
    print("[OneKillGift_onLogin] 玩家登录")
    OneKillGift_CheckCreate(human)
    
    local nOldEndTime = OneKillGift_GetEndTime(human)
    local nNowTime = os.time()
    local nBeginTime = OneKillGift_GetBeginTime(human)

    if true ~= Util.isSameDayByTimes(nBeginTime, nNowTime) then
        print("[OneKillGift_onLogin] 不是同一天进行重置")

        OneKillGift_ResetDB(human)
    else
        if nOldEndTime > nNowTime  then
            print("[OneKillGift_onLogin] 当前结束时间大于当前时间发送数据")
            OneKillGiftLogic_Query(human, ONEKILLGIFT_ONLOGIN)
        else
            if 0 < nOldEndTime then
                OneKillGift_SetShow(human, ONEKILLGIFT_NOSEND)
                print("[OneKillGift_onLogin] 玩家未购买且结束时间不为0直接不弹")
            end
        end
    end
end


function onCharge(human, nBuyID)
    local nBuyNum = OneKillGift_GetBuyNum(human)
    local tConf = OnekillConf.Sheet1[nBuyNum + 1]
    if tConf and tConf.nBuyID == nBuyID then
        BagLogic.addItemList(human, tConf.tPrize, "onekillgift")
        OneKillGift_BuyGift(human)
        local tMsgData = Msg.gc.GC_ONEKILLGIFT_CLOSE
        Msg.send(tMsgData, human.fd)
    end
end

function onZero()
     -- 遍历在线玩家
     for uuid, human in pairs(ObjHuman.onlineUuid) do
        local tMsgData = Msg.gc.GC_ONEKILLGIFT_CLOSE
        Msg.send(tMsgData, human.fd)
        OneKillGift_ResetDB(human)
    end
end

-- function GetRemainNum(human, nBuyID)
--     local nBuyNum = OneKillGift_GetBuyNum(human)
--     local tConf = OnekillConf.Sheet1[nBuyNum + 1]
--     if tConf and tConf.nBuyID == nBuyID then
--         return 1
--     else
--         return 0
--     end
-- end