--------------------------------
-- 文件名       ：  CycleActivity.lua
-- 文件说明     ：  循环活动管理器
-- 创建时间     :   2025/06/13
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local YunYingConf = require("excel.yunying")
local Timer = require("core.Timer")
local AbsActExcel = require("excel.absAct")
local AbsActLogic = require("absAct.AbsActLogic")
local Objhuman = require("core.ObjHuman")
local YunYingLogic = require("yunying.YunYingLogic")


local CYCLEACTIVITYDAY = 7     -- 持续天数

-- 计算结束时间
local function CycleActivity_CalEndTime()
    local nNowTime = os.time()
    local nEndTime = Util.getDayStartTime(nNowTime) + (CYCLEACTIVITYDAY * 24 * 60 *60) - 1

    -- local tEndDate = os.date("*t",nEndTime)
    -- tEndDate.hour = 0
    -- tEndDate.min = 0
    -- tEndDate.sec = 0
    -- nEndTime = os.time(tEndDate)

    return nEndTime
end

-- 创建数据
local function CycleActivity_CreateDB(human)
    human.db.cycleactivity = 
    {
        nActivityID = 0,
        nEndTime = 0,
        nBeginTime = 0,
        bReset = false,
    }

    local tConfig = YunYingConf.cycleactivity[1]
    if not tConfig or not tConfig.activityid then
        print("[CycleActivity_CreateDB] 不存在对应的配置数据!!!!")
        return
    end

    local nBeginID = tConfig.activityid[1]
    local nEndTime = CycleActivity_CalEndTime()

    human.db.cycleactivity.nBeginTime = os.time()
    human.db.cycleactivity.nActivityID = nBeginID
    human.db.cycleactivity.nEndTime = nEndTime
end

-- 检查是否需要创建DB
local function CycleActivity_CheckCreate(human)
    if not human.db.cycleactivity then
        CycleActivity_CreateDB(human)
    end
end

-- 获取当前记录的活动ID
local function CycleActivity_GetActivityID(human)
    return human.db.cycleactivity.nActivityID
end

-- 设置当前记录的活动ID
local function CycleActivity_SetActivityID(human, nActivityID)
    human.db.cycleactivity.nActivityID = nActivityID
end

-- 获取当前记录的结束时间
local function CycleActivity_GetEndTime(human)
    return human.db.cycleactivity.nEndTime
end

-- 设置当前记录的结束时间
local function CycleActivity_SetEndTime(human, nEndTime)
    human.db.cycleactivity.nEndTime = nEndTime
end

-- 获取当前记录的开始时间
local function CycleActivity_GetBeginTime(human)
    return human.db.cycleactivity.nBeginTime
end

-- 设置当前记录的开始时间
local function CycleActivity_SetBeginTime(human, nBeginTime)
    human.db.cycleactivity.nBeginTime = nBeginTime
end

-- 获取当前是否重置数据
local function CycleActivity_GetReset(human)
    return human.db.cycleactivity.bReset
end

-- 设置当前是否重置数据
local function CycleActivity_SetReset(human, nValue)
    human.db.cycleactivity.bReset = nValue
end

-- 获取下一轮活动ID
local function CycleActivity_GetNextActivityID(human)
    local nNowActivityID = CycleActivity_GetActivityID(human)
    local tConfig = YunYingConf.cycleactivity[1]
    if not tConfig then
        print("[CycleActivity_GetNextActivityID] 没有对应的配置")
        return -1
    end

    local nChoseID
    local nLen = #tConfig.activityid
    for nID, v in ipairs(tConfig.activityid) do
        if v == nNowActivityID then
            if nID == nLen then
                nChoseID = 1
            else
                nChoseID = nID + 1
            end
            break
        end
    end

    local nChoseActivityID = tConfig.activityid[nChoseID]
    return nChoseActivityID
end

-- 判断是否还在活动中
local function CycleActivity_IsRunByYunYingID(human, nYunYingID)
    local nNowID = CycleActivity_GetActivityID(human)
    --print("[CycleActivity_IsRunByYunYingID] 开始判断是否在活动时间内 name = "..human.db.name.." nYunYingID = "..nYunYingID.." nNowID = "..nNowID)
    if nNowID ~= nYunYingID then
        -- print("[CycleActivity_IsRunByYunYingID] 当前ID不相同直接返回")
        return false
    end

    local nEndTime = CycleActivity_GetEndTime(human)
    local nNowTime = os.time()

    --print("[CycleActivity_IsRunByYunYingID] 当前时间判断 nEndTime = "..nEndTime.." nNowTime = "..nNowTime)

    return nEndTime > nNowTime
end

-- 判断是否在活动中
local function CycleActivity_IsRunByAbsID(huamn, nAbsID)
    local tConfig = YunYingConf.cycleactivity[1]
    for _, v in ipairs(tConfig.activityid) do
        local tYunYingConf = YunYingConf.config[v]
        if tYunYingConf then
            for _, nCofAbsID in ipairs(tYunYingConf.funcs) do
                if nCofAbsID == nAbsID then
                    --print("[CycleActivity_IsRunByAbsID] 找到对应的运营活动ID v = "..v.." nCofAbsID = "..nCofAbsID)
                    return CycleActivity_IsRunByYunYingID(huamn, v)
                end
            end
        end
    end

    return false
end

-- 进入下一轮活动
local function CycleActivity_EnterNext(human)
    local nNowID = CycleActivity_GetActivityID(human)
    local bRun = CycleActivity_IsRunByYunYingID(human, nNowID)
    if true == bRun then
        print("[CycleActivity_EnterNext] 当前活动还在进行中无法进入下一轮")
        return false
    end

    local nNextID = CycleActivity_GetNextActivityID(human)
    if -1 >= nNextID then
        print("[CycleActivity_EnterNext] 获取不到下一轮的活动ID")
        return false
    end

    local nEndTime = CycleActivity_CalEndTime()
    local nBeginTime = os.time()

    CycleActivity_SetActivityID(human, nNextID)
    CycleActivity_SetEndTime(human, nEndTime)
    CycleActivity_SetBeginTime(human, nBeginTime)
    CycleActivity_SetReset(human, false)

    return true
end

-- 重置ABS活动DB数据
local function CycleActivity_ResetAbsID(human)
    local nNowID = CycleActivity_GetActivityID(human)
    local nEndTime = CycleActivity_GetEndTime(human)
    local tConfig = YunYingConf.config[nNowID]
    if not tConfig then
        print("[CycleActivity_ResetAbsID] 不存在对应的配置 nNowID = "..nNowID)
        return
    end
    -- print("[CycleActivity_ResetAbsID] 进入重置活动流程 nNowID = "..nNowID.." nEndTime = "..nEndTime)

    for _, nAbsID in ipairs(tConfig.funcs) do
        local tAbsConf = AbsActExcel.absActivity[nAbsID]
        if tAbsConf then
            local bClearDB = AbsActLogic.getIsClearDB(nAbsID)
            -- print("[CycleActivity_ResetAbsID] 正式重置 nAbsID = "..nAbsID.." bClearDB = "..(bClearDB == true and 1 or 0))
            if true == bClearDB then
                human.db.absAct[nAbsID] = {}
                human.db.absAct[nAbsID].finish = nEndTime -- 记录活动结束时间
                -- print("[CycleActivity_ResetAbsID] 清理活动数据成功 nAbsID = "..nAbsID)
            end
        end
    end
end


---------------------------------- 外部调用 -------------------------------
-- abs活动是否开始
function isStarted(human, nAbsID)
    CycleActivity_CheckCreate(human)
    local realEndTime = 0
    local realStartTime = 0
    local state = false
    state = CycleActivity_IsRunByAbsID(human, nAbsID)
    if true == state then
        realStartTime = CycleActivity_GetBeginTime(human)
        realEndTime = CycleActivity_GetEndTime(human)
        -- print("[CycleActivity_isStarted] 当前活动开启中 时间打印 realStartTime = "..realStartTime.." realEndTime = "..realEndTime.." nAbsID = "..nAbsID)
    end

    return state, realEndTime, realStartTime 
end

-- 玩家登录
function onLogin(human)
    CycleActivity_CheckCreate(human)
    
    local nEndTime = CycleActivity_GetEndTime(human)
    local nNowTime = os.time()
    -- print("[CycleActivity_onLogin] 当前玩家登录  时间打印 nEndTime = "..nEndTime.." name = "..human.db.name)
    if nNowTime >= nEndTime then
        local bRet = CycleActivity_EnterNext(human)
        if false == bRet then
            print("[CycleActivity_onLogin] 玩家登录 进入下一轮活动失败 name = "..human.db._id)
            return
        end
        -- print("[CycleActivity_onLogin] 玩家进入下一个活动成功")
    end

    local bReset = CycleActivity_GetReset(human)
    if false == bReset then
        -- print("[CycleActivity_onLogin] 开始重置数据 name = "..human.db.name)
        CycleActivity_ResetAbsID(human)
        CycleActivity_SetReset(human, true)
    end

    -- print("[CycleActivity_onLogin] 打印数据")
    --table.print_lua_table(human.db.cycleactivity)
end

-- 定时
function onZero()
    print("[CycleActivity_onZero] 0点定时刷新开始")
    for _, human in pairs(Objhuman.onlineUuid) do
        if human then
            -- print("[CycleActivity_onZero] 0点开始重置玩家数据 name = "..human.db.name)

            onLogin(human)
            YunYingLogic.sendIcons(human)
            YunYingLogic.sendBanner(human)

            -- print("[CycleActivity_onZero] 0点重置玩家数据结束 name = "..human.db.name)
        end
    end
end

-- GM函数, 进入下一个循环活动
function GM_EnterNext(human)
    -- local nNowID = CycleActivity_GetActivityID(human)
    -- local bRun = CycleActivity_IsRunByYunYingID(human, nNowID)
    -- if true == bRun then
    --     print("[CycleActivity_EnterNext] 当前活动还在进行中无法进入下一轮")
    --     return false
    -- end

    local nNextID = CycleActivity_GetNextActivityID(human)
    if -1 >= nNextID then
        print("[CycleActivity_EnterNext] 获取不到下一轮的活动ID")
        return false
    end

    local nEndTime = CycleActivity_CalEndTime()
    local nBeginTime = os.time()

    CycleActivity_SetActivityID(human, nNextID)
    CycleActivity_SetEndTime(human, nEndTime)
    CycleActivity_SetBeginTime(human, nBeginTime)
    CycleActivity_SetReset(human, false)

    local bReset = CycleActivity_GetReset(human)
    if false == bReset then
        -- print("[CycleActivity_onLogin] 开始重置数据 name = "..human.db.name)
        CycleActivity_ResetAbsID(human)
        CycleActivity_SetReset(human, true)
    end

    YunYingLogic.sendIcons(human)
    YunYingLogic.sendBanner(human)
end
