using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using MrWu.Debug;
using Server.Core;

namespace ActorCore
{
    public enum SchedulerType
    {
        Main = 0,
        Thread,
        ThreadPool,
        Max
    }

    /// <summary>
    /// Actor 管理器
    /// </summary>
    public class ActorManager : Singleton<ActorManager>
    {
        
        /// <summary>
        /// 当前进程信息
        /// </summary>
        public ProcessInfo ProcessInfo
        {
            get;
            private set;
        }

        /// <summary>
        /// 当前的Actor
        /// </summary>
        [ThreadStatic]
        public static IActor Current;

        private IScheduler[] _schedulers = new IScheduler[(int)SchedulerType.Max];

        private ConcurrentDictionary<int, IActor> _actors = new ConcurrentDictionary<int, IActor>();

        private MainThreadScheduler _mainThreadScheduler;

        private List<int> actorIds = new List<int>();
        
        protected override void Awake()
        {
            _mainThreadScheduler = new MainThreadScheduler(this);
            _schedulers[(int)SchedulerType.Main] = _mainThreadScheduler;
            _schedulers[(int)SchedulerType.Thread] = new ThreadScheduler(this);
            _schedulers[(int)SchedulerType.ThreadPool] = new ThreadPoolScheduler(this);
        }

        public void Init(ProcessInfo processInfo)
        {
            this.ProcessInfo = processInfo;
        }

        public void Update()
        {
            this._mainThreadScheduler.Update();
        }

        public void LateUpdate()
        {
            this._mainThreadScheduler.LateUpdate();
        }

        protected override void Destroy()
        {
            foreach (var scheduler in _schedulers)
            {
                scheduler.Dispose();
            }

            foreach (var kv in _actors)
            {
                kv.Value.Dispose();
            }
            
            _actors.Clear();
        }

        public void RegisterActor(IActor actor)
        {
            if (!_actors.TryAdd(actor.Id,actor))
            {
                Debug.Error($"actor already existed, actorTypeId:{actor.Id}");
                return;
            }
            
            ActorMessageQueue.Instance.AddQueue(actor.Id);
            actor.IsRegister = true;
            _schedulers[(int)actor.SchedulerType].Add(actor.Id);
            actor.RegisterHandle();
            _actorIds[actor.ActorId.ActorTypeId] = actor.ActorId;
            actorIds.Add(actor.Id);
        }

        public IActor Get(int actorTypeId)
        {
            this._actors.TryGetValue(actorTypeId, out IActor actor);
            return actor;
        }

        public int Count()
        {
            return this._actors.Count;
        }

        public IEnumerable<int> GetActorIds()
        {
            return actorIds;
        }

        private Dictionary<int,ActorId> _actorIds = new Dictionary<int, ActorId>();

        public ActorId GetActorId(int actorTypeId)
        {
            if (_actorIds.TryGetValue(actorTypeId,out ActorId actorId))
            {
                return actorId;
            }

            return default(ActorId);
        }
    }
}