------------------------------------------------
--作者: yangqf
--日期: 2021-09-27
--文件: TodayFuncSystem.lua
--模块: TodayFuncSystem
--描述: 今日功能系统
------------------------------------------------
local RedPointCustomCondition = CS.Thousandto.Code.Logic.RedPointCustomCondition
local TimeUtils = CS.Thousandto.Core.Base.TimeUtils

local TodayFuncSystem = {
    --开服时间,用于计算开服天数
    ServerOpenTime = nil,
    --展示主任务
    ShowFuncList = List:New(),
    --展示的任务列表
    ShowTaskList = List:New(),
    --任务数据
    TaskData = nil,
    --充值数据
    RechargeData = nil,
    --当前开服天数
    CurOpenServerDay = 0,
}

function TodayFuncSystem:Initialize()
    self.ShowFuncList:Clear()
    --每天凌晨10秒执行
    self.TimerID = GameCenter.TimerEventSystem:AddTimeStampDayEvent(10, 86400,
    true, nil, function(id, remainTime, param)
        self:ReInitTodyTask()
    end)
end

function TodayFuncSystem:UnInitialize()
end

function TodayFuncSystem:SetOpenServerTime(time)
    --检查开启情况
    self.ServerOpenTime = math.floor(math.floor(time / 1000) + GameCenter.HeartSystem.ServerZoneOffset)
    self:ReInitTodyTask()
end

--重新初始化今日活动
function TodayFuncSystem:ReInitTodyTask()
    self.ShowFuncList:Clear()
    local _serverTime = math.floor(GameCenter.HeartSystem.ServerZoneTime)
    --刷新今日任务
    local _openDay = TimeUtils.GetDayOffsetNotZone(self.ServerOpenTime, math.floor(_serverTime)) + 1
    self.CurOpenServerDay = _openDay
    --计算当前周几 1 - 7
    local week = TimeUtils.GetStampTimeWeeklyNotZone(_serverTime)
    if week == 0 then
        week = 7
    end
    local _openCountTable = {}
    DataConfig.DataTodayFunction:Foreach(function(k, cfg)
        local _dayParam = Utils.SplitNumber(cfg.OpenDay, '_')
        if _openDay >= _dayParam[1] and _openDay <= _dayParam[2] then
            if string.len(cfg.WeekDay) > 0 then
                local _weekParam = Utils.SplitNumber(cfg.WeekDay, '_')
                if _weekParam:Contains(week) then
                    local _rOpenDay = _openDay - _dayParam[1]
                    local _startWeek = TimeUtils.GetStampTimeWeeklyNotZone(_serverTime - _rOpenDay * 86400)
                    if _startWeek == 0 then
                        _startWeek = 7
                    end
                    --计算活动开了多少次
                    local _rOpenCount = 0
                    for i = 0, _rOpenDay do
                        if _weekParam:Contains(_startWeek) then
                            _rOpenCount = _rOpenCount + 1
                        end
                        _startWeek = _startWeek + 1
                        if _startWeek > 7 then
                            _startWeek = 1
                        end
                    end
                    --今天开启活动
                    self.ShowFuncList:Add(cfg)
                    _openCountTable[k] = _rOpenCount
                end
            else
                self.ShowFuncList:Add(cfg)
                _openCountTable[k] = _openDay - _dayParam[1] + 1
            end
        end
    end)
    self.ShowTaskList:Clear()
    DataConfig.DataTodayFunctionTask:Foreach(function(k, cfg)
        local _ownerParams = Utils.SplitNumber(cfg.TodayFunctionID, '_')
        local _isShow = false
        for i = 1, #_ownerParams do
            local _openCount = _openCountTable[_ownerParams[i]]
            if _openCount ~= nil and (cfg.ShowCount == nil or cfg.ShowCount == 0 or cfg.ShowCount >= _openCount) then
                _isShow = true
                break
            end
        end
        if _isShow then
            self.ShowTaskList:Add(cfg)
        end
    end)
    if self.ShowTaskList == nil or #self.ShowTaskList <= 0 then
        GameCenter.MainFunctionSystem:SetFunctionVisible(FunctionStartIdCode.ToDayFunc, false)
    else
        GameCenter.MainFunctionSystem:SetFunctionVisible(FunctionStartIdCode.ToDayFunc, true)
        self:CheckAllRedPoint()
    end
    GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_REFRESH_TODAYFUNC_INFO)
end

--检查所有任务红点
function TodayFuncSystem:CheckAllRedPoint()
    GameCenter.RedPointSystem:CleraFuncCondition(FunctionStartIdCode.ToDayFunc)
    --增加第一次红点
    GameCenter.RedPointSystem:AddFuncCondition(FunctionStartIdCode.ToDayFunc, 0, RedPointCustomCondition(true))
    if self.TaskData == nil then
        return
    end
    for i = 1, #self.ShowTaskList do
        self:CheckSingleRedPoint(self.ShowTaskList[i].Id, false)
    end
end

--检查单个任务红点
function TodayFuncSystem:CheckSingleRedPoint(taskId, needClear)
    if needClear then
        GameCenter.RedPointSystem:RemoveFuncCondition(FunctionStartIdCode.ToDayFunc, taskId)
    end
    if self.TaskData == nil then
        return
    end
    local _taskData = self.TaskData[taskId]
    if _taskData == nil then
        return
    end
    if _taskData.IsGet then
        return
    end
    if _taskData.Num < _taskData.MaxNum then
        return
    end
    GameCenter.RedPointSystem:AddFuncCondition(FunctionStartIdCode.ToDayFunc, taskId, RedPointCustomCondition(true))
end

--所有任务列表数据
function TodayFuncSystem:ResAllFunctionTask(msg)
    self.TaskData = {}
    if msg.tasks ~= nil then
        for i = 1, #msg.tasks do
            local _msgTask = msg.tasks[i]
            self.TaskData[_msgTask.id] = {
                Id = _msgTask.id,
                Num = _msgTask.num,--当前数量
                MaxNum = _msgTask.maxNum,--最大数量
                IsGet = _msgTask.get,--是否领取
            }
        end
    end
    self.RechargeData = nil
    if msg.rechargeTask ~= nil then
        self.RechargeData = {
            RechargeId = msg.rechargeTask.rechargeid,--充值ID
            ZheKou = msg.rechargeTask.num,--折扣
            ItemList = msg.rechargeTask.rewards,--物品列表
            IsGet = msg.rechargeTask.get,--是否领取
        }
    end
    self:CheckAllRedPoint()
    GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_REFRESH_TODAYFUNC_INFO)
end

--任务刷新
function TodayFuncSystem:ResFunctionTaskUpdate(msg)
    if self.TaskData == nil then
        self.TaskData = {}
    end
    if msg.tasks ~= nil then
        local _taskData = self.TaskData[msg.tasks.id]
        if _taskData == nil then
            _taskData = {}
            self.TaskData[msg.tasks.id] = _taskData
        end
        _taskData.Id = msg.tasks.id
        _taskData.Num = msg.tasks.num --当前数量
        _taskData.MaxNum = msg.tasks.maxNum--最大数量
        _taskData.IsGet = msg.tasks.get --是否领取
        self:CheckSingleRedPoint(_taskData.Id, true)
    end
    if msg.rechargeTask ~= nil then
        self.RechargeData = {
            RechargeId = msg.rechargeTask.rechargeid,--充值ID
            ZheKou = msg.rechargeTask.num,--折扣
            ItemList = msg.rechargeTask.rewards,--物品列表
            IsGet = msg.rechargeTask.get,--是否领取
        }
    end
    GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_REFRESH_TODAYFUNC_INFO)
end

return TodayFuncSystem