#if !COMBAT_SERVER
using System;
using System.Collections;
using Fort23.Core;
using Fort23.UTool;
using UnityEngine;


public class StaticUpdater : Entity
{
    public static StaticUpdater Instance;

    public delegate void UpdateCallBack();

    private BetterList<UpdateCallBack> m_renderCallBack = new BetterList<UpdateCallBack>();
    private BetterList<UpdateCallBack> m_lateCallBack = new BetterList<UpdateCallBack>();

    private GameObject g;

    [CustomMethod(CustomMethodType.Awake)]
    public void Awake()
    {
        if (Instance != null)
        {
            LogTool.Error("不能添加重复的单例");
            return;
        }

        g = new GameObject();
        GameObject.DontDestroyOnLoad(g);
        Instance = this;
    }

    [CustomMethod(CustomMethodType.Update)]
    public void Update()
    {
        try
        {
            if (m_renderCallBack.size > 0)
            {
                UpdateCallBack[] callBack = m_renderCallBack.ToArray();
                for (int i = 0; i < callBack.Length; i++)
                {
                    try
                    {
                        callBack[i]?.Invoke();
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }

    [CustomMethod(CustomMethodType.LateUpdate)]
    public void LateUpdate()
    {
        try
        {
            if (m_lateCallBack.size > 0)
            {
                UpdateCallBack[] callBack = m_lateCallBack.ToArray();
                for (int i = 0; i < callBack.Length; i++)
                {
                    callBack[i]();
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }

    public override void Dispose()
    {
        m_renderCallBack.Clear();
        m_lateCallBack.Clear();
    }

    public void AddRenderUpdateCallBack(UpdateCallBack callBack)
    {
        if (!m_renderCallBack.Contains(callBack))
        {
            m_renderCallBack.Add(callBack);
        }
    }

    public void RemoveRenderUpdateCallBack(UpdateCallBack callBack)
    {
        m_renderCallBack.Remove(callBack);
    }

    public void AddLateUpdateCallBack(UpdateCallBack callBack)
    {
        m_lateCallBack.Add(callBack);
    }

    public void RemoveLateUpdateCallBack(UpdateCallBack callBack)
    {
        m_lateCallBack.Remove(callBack);
    }

    public void StartCoroutine(IEnumerator enumerator)
    {
    }

    public void RemoveFixedUpdateCallBack(Action updateRequestState)
    {
    }
}
#endif