using System;
using System.Collections.Generic;
using XGame.Framework.Interfaces;

namespace XGame.Framework.UI
{
    internal class UIControllerGroup : IUIControllerGroup
    {
        protected UIContext _context;
        private IUIViewModel _viewModel;
        private List<IUIController> _controllers;
        private HashSet<IUpdate> _updates;
        private HashSet<ILateUpdate> _lateUpdates;
        public UIControllerGroup(UIContext context, IUIViewModel viewModel)
        {
            _context = context;
            _viewModel = viewModel;
            _controllers = new List<IUIController>();
            _updates = new HashSet<IUpdate>();
            _lateUpdates = new HashSet<ILateUpdate>();
        }

        void IUIControllerGroup.Add(IUIController controller, bool isStatic)
        {
            if (_controllers.Contains(controller))
            { // 保存失败
                Log.Warn($"IUIControllerGroup.Add repeat.");
                return;
            }
            else
            {
                _controllers.Add(controller);
            }
            if (isStatic)
            {
                if (controller is IUIContextSetter contextSetter)
                {
                    contextSetter.Context = _context;
                }
                if (controller is IUIViewModelSetter vmSetter)
                {
                    vmSetter.ViewModel = _viewModel;
                }
            }
            if (controller is IUpdate update)
            {
                _updates.Add(update);
            }
            if (controller is ILateUpdate lateUpdate)
            {
                _lateUpdates.Add(lateUpdate);
            }
        }
        void IUIControllerGroup.Remove(IUIController controller)
        {
            if (_controllers.Remove(controller))
            {
                if (controller is IUpdate update)
                {
                    _updates.Remove(update);
                }
                if (controller is ILateUpdate lateUpdate)
                {
                    _lateUpdates.Remove(lateUpdate);
                }
            }
        }
        #region public
        public void Enable(object intent)
        {
            var count = _controllers.Count;
            for (var i = 0; i < count && i < _controllers.Count; i++)
            { // enable的时候可能子节点会新增Ctrl
                _controllers[i].Enable(intent);
            }
            //Log.Info($"UIControllerGroup Enable Start:{count} End:{_controllers.Count}");
        }
        public void Disable()
        {
            var count = _controllers.Count;
            for (var i = 0; i < count && i < _controllers.Count; i++)
            {
                _controllers[i].Disable();
            }
            //Log.Info($"UIControllerGroup Disable Start:{count} End:{_controllers.Count}");
        }
        public void Update(int millisecond)
        {
            foreach (var update in _updates)
            {
                update?.Update(millisecond);
            }
        }
        public void LateUpdate(int millisecond)
        {
            foreach (var lateUpdate in _lateUpdates)
            {
                lateUpdate?.LateUpdate(millisecond);
            }
        }
        public void Dispose()
        {
            _updates.Clear();
            _lateUpdates.Clear();
            foreach (var controller in _controllers)
            {
                (controller as IDisposable)?.Dispose();
            }
            _controllers.Clear();
        }
        #endregion
    }
}
