using System;
using System.Collections.Generic;
using System.Linq;
using Plugins.JNGame.Util;

namespace Plugins.JNGame.Sync.Frame.game.Time
{
    
    
    public class JNFrameTimeHandler
    {
        public int ID;
        public Action Handler;
        public int Timeout;
    }
    
    //帧同步定时器
    public class JNFrameTime : Singleton<JNFrameTime>
    {
        
        private int _id = 0;
        private List<JNFrameTimeHandler> _handlers = new();
        
        //设置定时器
        public int SetTimeout(Action handler,int timeout = 0)
        {
            var funHandler = new JNFrameTimeHandler()
            {
                ID = this._id++,
                Handler = handler,
                Timeout = timeout
            };
            this._handlers.Add(funHandler);
            return funHandler.ID;
        }

        public void Update(int dt)
        {
            
            this._handlers.ToList().ForEach(action =>
            {
                action.Timeout -= dt;
                if (action.Timeout <= 0)
                {
                    action.Handler();
                    this._handlers.Remove(action);
                }
            });
            
        }

    }
}