------------------------------------------------
--作者: 杨全福
--日期: 2021-02-24
--文件: MainCustomBtnSystem.lua
--模块: MainCustomBtnSystem
--描述: 自定义按钮系统
------------------------------------------------

local L_MainCustomBtn = require "Logic.MainCustomBtnSystem.MainCustomBtn"

local MainCustomBtnSystem = {
    BtnList = List:New(),
}

function MainCustomBtnSystem:Initialize()
    self.BtnList:Clear()
end

function MainCustomBtnSystem:UnInitialize()
    self.BtnList:Clear()
end

local function L_BtnShort(left, right)
    local _lTime = left:GetRemainTime()
    local _rTime = right:GetRemainTime()
    if not left.UseRemainTime then
        _lTime = -999999999
    end
    if not right.UseRemainTime then
        _rTime = -999999999
    end

    if _lTime == _rTime then
        return left.ID < right.ID
    end
    return _lTime < _rTime
end

--添加限时按钮
function MainCustomBtnSystem:AddLimitBtn(iconID, showText, remainTime, customData, clickBack, showEffect, showRedPoint, remainTimeSuf, useServerTime, tweenRot, vfxId)
    local _newBtn = L_MainCustomBtn:New()
    _newBtn.IconID = iconID
    _newBtn.ShowText = showText
    _newBtn.UseServerTime = useServerTime
    _newBtn:SetRemainTime(remainTime)
    _newBtn.RemainTimeSuf = remainTimeSuf
    _newBtn.CustomData = customData
    _newBtn.ClickCallBack = clickBack
    _newBtn.UseRemainTime = true
    _newBtn.ShowEffect = showEffect
    _newBtn.ShowRedPoint = showRedPoint
    _newBtn.TweenRot = tweenRot
    _newBtn.VfxId = vfxId
    self.BtnList:Add(_newBtn)
    self.BtnList:Sort(L_BtnShort)
    GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_UPDATECUSTONBTNS)
    return _newBtn.ID
end

--添加不限时按钮
function MainCustomBtnSystem:AddBtn(iconID, showText, customData, clickBack, showEffect, showRedPoint, tweenRot, vfxId)
    local _newBtn = L_MainCustomBtn:New()
    _newBtn.IconID = iconID
    _newBtn.ShowText = showText
    _newBtn.RemainTime = 0
    _newBtn.CustomData = customData
    _newBtn.ClickCallBack = clickBack
    _newBtn.UseRemainTime = false
    _newBtn.ShowEffect = showEffect
    _newBtn.ShowRedPoint = showRedPoint
    _newBtn.TweenRot = tweenRot
    _newBtn.VfxId = vfxId
    self.BtnList:Add(_newBtn)
    self.BtnList:Sort(L_BtnShort)
    GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_UPDATECUSTONBTNS)
    return _newBtn.ID
end

--设置显示红点
function MainCustomBtnSystem:SetShowRedPoint(id, show)
    local _succ = false
    for i = 1, #self.BtnList do
        local _btn = self.BtnList[i]
        if _btn.ID == id then
            if _btn.ShowRedPoint ~= show then
                _btn.ShowRedPoint = show
                _succ = true
            end
            break
        end
    end
    if _succ then
        GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_UPDATECUSTONBTNS)
    end
    return _succ
end
    
--设置显示特效
function MainCustomBtnSystem:SetShowEffect(id, show)
    local _succ = false
    for i = 1, #self.BtnList do
        local _btn = self.BtnList[i]
        if _btn.ID == id then
            if _btn.ShowEffect ~= show then
                _btn.ShowEffect = show
                _succ = true
            end
            break
        end
    end
 
    if _succ then
        GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_UPDATECUSTONBTNS)
    end
    return _succ
end
--删除按钮
function MainCustomBtnSystem:RemoveBtn(id)
    local _succ = false
    for i = 1, #self.BtnList do
        if self.BtnList[i].ID == id then
            self.BtnList:RemoveAt(i)
            _succ = true
            break
        end
    end
    if _succ then
        GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_UPDATECUSTONBTNS)
    end
end
--更新
function MainCustomBtnSystem:Update()
    local _remove = false
    for i = #self.BtnList, 1, -1 do
        local _btn = self.BtnList[i]
        if _btn.UseRemainTime and _btn:GetRemainTime() <= 0 then
            self.BtnList:RemoveAt(i)
            _remove = true
        end
    end

    if _remove then
        GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_UPDATECUSTONBTNS)
    end
end

return MainCustomBtnSystem