using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using MultiLayout.UiModule;
using Misc;
using MultiLayout;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Unity;

namespace FLY.Thick.Base.UI.OnInit
{
    /// <summary>
    /// 根据状态换页
    /// </summary>
    public class OnInitControllerState : IOnInit
    {
        ParamDictionary paramDictionary;
        IUnityContainer container;
        FlyLayoutManager manager;

        List<Tag> tags = new List<Tag>();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        /// <param name="paramDictionary"></param>
        /// <param name="manager"></param>
        /// <param name="lv"></param>
        public OnInitControllerState(
            IUnityContainer container, 
            ParamDictionary paramDictionary,
            FlyLayoutManager manager, 
            int lv=1) 
        {
            Level = lv;
            this.container = container;
            this.paramDictionary = paramDictionary;
            this.manager = manager;
        }

        public int Level { get; }

        class Tag 
        {
            public string rontainerName;
            public DynArea dynArea;
        }
        public void OnInit()
        {
            //找出全部 xxx.service 服务容器 且包含 IDynAreaService服务
            var rontainerNames =
                from regist in container.Registrations
                where
                (regist.RegisteredType == typeof(IUnityContainer))
                && (!string.IsNullOrEmpty(regist.Name))
                && Regex.IsMatch(regist.Name, @"\w+\.service")
                && container.Resolve<IUnityContainer>(regist.Name).IsRegistered<IDynAreaService>()
                select regist.Name;

            if (rontainerNames.Count() == 0)
                return;//一个服务容器都没有

            //注册事件
            foreach (var rontainerName in rontainerNames)
            {
                var serviceContainer = container.Resolve<IUnityContainer>(rontainerName);
                var dynAreaService =serviceContainer.Resolve<IDynAreaService>();
                var tag = new Tag()
                {
                    rontainerName = rontainerName,
                    dynArea = dynAreaService.DynArea
                };
                tags.Add(tag);

                dynAreaService.DynArea.PropertyChanged += (s, e) =>
                {
                    DynArea dynarea = s as DynArea;
                    var t = tags.Find(_t => _t.dynArea == dynarea);

                    if (e.PropertyName == nameof(DynArea.ControllerState))
                    {
                        UpdateControllerState(t);
                    }
                };

                UpdateControllerState(tag);
            }


        }

        void UpdateControllerState(Tag tag)
        {
            string rontainerName = tag.rontainerName;
            var dynArea = tag.dynArea;
            //找到状态切换条件
            var controllerStateTabItems = paramDictionary.GetValue<List<ControllerStateTabItem>>($"{rontainerName}.stateChanged");
            if (controllerStateTabItems == null)
                return;

            //找出设备页面
            if (manager.layout.GageTabItems == null)
                return;
            var gageTabItems = from _gageTabItem in manager.layout.GageTabItems where _gageTabItem.ServiceContainerName == rontainerName select _gageTabItem;
            if (gageTabItems.Count() == 0)
                return;

            var gageTabItem = gageTabItems.First();


            var st = controllerStateTabItems.Find(_st => _st.ControllerState == dynArea.ControllerState.ToString());
            if (st != null)
            {
                if (st.No >= 0 && st.No < gageTabItem.TabControl.Items.Count)
                    gageTabItem.TabControl.SelectedIndex = st.No;
            }
        }
    }
    /// <summary>
    /// 状态换页条件
    /// </summary>
    public class ControllerStateTabItem
    {
        /// <summary>
        /// 状态
        /// </summary>
        public string ControllerState;
        /// <summary>
        /// 页序号
        /// </summary>
        public int No;
    }
}
