using FLY.Thick.Base.IService;
using FLY.Thick.Base.UI.OnInit;
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 System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Unity;

namespace FLY.Thick.Base.UI.CustomSection
{
    /// <summary>
    /// UcControllerState.xaml 的交互逻辑
    /// </summary>
    public partial class UcSectionControllerState : UserControl
    {
        ParamDictionary paramDictionary;
        IUnityContainer container;
        FlyLayoutManager manager;

        public UcSectionControllerState()
        {
            InitializeComponent();
        }

        [Unity.InjectionMethod]
        public void Init(
            IUnityContainer container,
            ParamDictionary paramDictionary,
            FlyLayoutManager manager)
        {
            this.container = container;
            this.paramDictionary = paramDictionary;
            this.manager = manager;

            InitializeTabControl();
        }

        void InitializeTabControl() 
        {
            //找出全部 xxx.service 服务容器 且包含 ITDGageService服务
            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<ITDGageService>()
                select regist.Name;

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

            tabControl.Visibility = Visibility.Visible;

            foreach (var rontainerName in rontainerNames)
            {
                var serviceContainer = container.Resolve<IUnityContainer>(rontainerName);

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

                //找到状态切换条件
                var controllerStateTabItems = paramDictionary.GetValue<List<ControllerStateTabItem>>($"{rontainerName}.stateChanged");
                if (controllerStateTabItems == null)
                {
                    controllerStateTabItems = new List<ControllerStateTabItem>();
                    paramDictionary.SetValue($"{rontainerName}.stateChanged", controllerStateTabItems);
                }
                var tabItem = new TabItem();
                tabItem.Header = gageTabItem.Title;

                var singleTab = new UcSingleStateTab();
                singleTab.Init(controllerStateTabItems, gageTabItem);

                singleTab.Margin = new Thickness(5);
                tabItem.Content = singleTab;

                tabControl.Items.Add(tabItem);
            }
        }
    }
}
