--------------------------------
-- 文件名       ：  MergeServerLogin.lua
-- 文件说明     ：  合服活动-登录豪礼
-- 创建时间     :   2025/07/29
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Msg = require("core.Msg")
local Log = require("common.Log")
local YunYingLogic = require("yunying.YunYingLogic")
local CommonDB = require("common.CommonDB")
local CommonDefine = require("common.CommonDefine")
local MergeConfig = require("excel.MergeServerActivity")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local MergeServerCharge = require("present.MergeServerCharge")
local MergeServerHuioYue = require("present.MergeServerHuoYue")

-----------------------------------内部调用-----------------------------
-- 获取开始时间
local function MergeServerLogin_GetTime()
    local nBeginTime = CommonDB.getMergeServerTime()
    print("[MergeServerLogin_GetTime] nBeginTime = "..nBeginTime)
    if not nBeginTime or 0 >= nBeginTime then
        return 0, 0
    end

    local nAddTime = (CommonDefine.MERGERSERVER_ACTIVITY_DAY - 1) * 24 * 60 *60
    local nEndTime = nBeginTime + nAddTime
    print("[MergeServerLogin_GetTime] nEndTime 1 = "..nEndTime.." nAddTime = "..nAddTime)
    local tEndDate = os.date("*t",nEndTime)
    tEndDate.hour = 23
    tEndDate.min = 59
    tEndDate.sec = 59

    nEndTime = os.time(tEndDate)
    print("[MergeServerLogin_GetTime] nEndTime 2 = "..nEndTime)

    return nBeginTime, nEndTime
end

local function MergeServerLogin_IsOpen()
    local nBeginTime, nEndTime = MergeServerLogin_GetTime()
    if 0 >= nBeginTime or 0 >= nEndTime then
        print("[MergeServerLogin_IsOpen] 获取到的时间为0")
        return false
    end

    local nNowTime = os.time()
    print("[MergeServerLogin_IsOpen] 当前时间 nNowTime = "..nNowTime.." nBeginTime = "..nBeginTime.." nEndTime = "..nEndTime)
    return nEndTime > nNowTime
end

local function MergeServerLogin_GetConf()
    return MergeConfig.LoginPrize
end

-- 初始化DB数据
local function MergeServerLogin_CreateDB(human)
    human.db.MergeLogin = {
        nBeginTime = CommonDB.getMergeServerTime(),
        nLoginTime = 0,
        tPrize = {}
    }

    local tData = human.db.MergeLogin.tPrize
    local tConfig = MergeServerLogin_GetConf()
    for _, v in ipairs(tConfig) do
        tData[v.nDay] = CommonDefine.COMMON_PRIZE_STATE_NOGET
    end
end

local function MergeServerLogin_CheckDB(human)
    if not human.db.MergeLogin then
        MergeServerLogin_CreateDB(human)
    else 
        if human.db.MergeLogin.nBeginTime then
            local nNowBeginTime =  CommonDB.getMergeServerTime()
            if nNowBeginTime ~= human.db.MergeLogin.nBeginTime then
                MergeServerLogin_CreateDB(human)
            end
        end
    end
end

-- 获取某天状态
local function MergeServerLogin_GetStatus(human, nDay)
    return human.db.MergeLogin.tPrize[nDay]
end

-- 设置某天状态
local function MergeServerLogin_SetStatus(human, nDay, nValue)
    human.db.MergeLogin.tPrize[nDay] = nValue
end

-- 获取登录时间
local function MergeServerLogin_GetLoginTime(human)
    return human.db.MergeLogin.nLoginTime
end

-- 设置登录时间
local function MergeServerLogin_SetLoginTime(human, nValue)
    human.db.MergeLogin.nLoginTime = nValue
end


-----------------------------------客户端请求-----------------------------
-- 请求合服登录相关信息
function MergeServerLogin_Query(human)
    if false == MergeServerLogin_IsOpen then
        return
    end

    MergeServerLogin_CheckDB(human)

    local tMsgData = Msg.gc.GC_MERGESERVER_LOGIN_QUERY
    tMsgData.nBeginTime, tMsgData.nEndTime = MergeServerLogin_GetTime()
    local tConfig = MergeServerLogin_GetConf()
    tMsgData.tList[0] = #tConfig
    for nID, v in ipairs(tConfig) do
        local tData = tMsgData.tList[nID]
        tData.nID = nID
        tData.nDay = v.nDay
        tData.nState = MergeServerLogin_GetStatus(human, v.nDay)
        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 MergeServerLogin_Get(human)
    if false == MergeServerLogin_IsOpen then
        return
    end

    local tGoods = {}
    local tConfig = MergeServerLogin_GetConf()
    for nID, v in ipairs(tConfig) do
        local nState = MergeServerLogin_GetStatus(human, v.nDay)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
            MergeServerLogin_SetStatus(human, v.nDay, CommonDefine.COMMON_PRIZE_STATE_GET)
            for i, tItem in ipairs(v.tPrize) do
                if not tGoods[tItem[1]] then
                    tGoods[tItem[1]] = 0
                end

                tGoods[tItem[1]] = tGoods[tItem[1]] + tItem[2]
            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, "MergeLogin")
        MergeServerLogin_Query(human)

        YunYingLogic.sendBanner(human)
        MergeServerLogin_SendRed(human)
    end
end


------------------------------------- 外部调用 -------------------------------
-- 是否开启
function isOpen(human, YYInfo, funcConfig)
   return MergeServerLogin_IsOpen()
end

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

function isRed(human, YYInfo, funcConfig)
    if false == MergeServerLogin_IsOpen() then
        return false
    end

    MergeServerLogin_CheckDB(human)

    local tConfig = MergeServerLogin_GetConf()
    for nID, v in ipairs(tConfig) do
        local nState = MergeServerLogin_GetStatus(human, v.nDay)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
            return true
        end
    end

    return false
end

function onLogin(human)
    if false == MergeServerLogin_IsOpen() then
        return
    end

    MergeServerLogin_CheckDB(human)

    print("[MergeServerLogin_onLogin] 玩家登录")
    local nLastTime = MergeServerLogin_GetLoginTime(human)
    if 0 == nLastTime or nil == Util.isSameDay(nLastTime) then
        MergeServerLogin_SetLoginTime(human, os.time())
        local tConfig = MergeServerLogin_GetConf()
        for _, v in ipairs(tConfig) do
            local nState = MergeServerLogin_GetStatus(human, v.nDay)
            if CommonDefine.COMMON_PRIZE_STATE_NOGET == nState then
                MergeServerLogin_SetStatus(human, v.nDay, CommonDefine.COMMON_PRIZE_STATE_CANGET)
                break
            end
        end
    end

    MergeServerLogin_SendRed(human)
end


function MergeServerLogin_SendRed(human)
    local tMsgData = Msg.gc.GC_MERGESERVER_UPDATE_RED
    tMsgData.tRed[0] = 3
    tMsgData.tRed[1] = isRed(human) == true and 1 or 0
    tMsgData.tRed[2] = MergeServerCharge.isRed(human) == true and 1 or 0
    tMsgData.tRed[3] = MergeServerHuioYue.isRed(human) == true and 1 or 0

    Msg.send(tMsgData, human.fd)
    print("[MergeServerLogin_SendRed] 发送红点结束")
end