using MultiLayout.UiModule;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace MultiLayout.MainEdit
{
    public class GraphConverter
    {
        public static ObservableCollection<MeComponentType> GetToolboxItems(FlyLayoutManager manager, ComponentType componentType)
        {
            ObservableCollection<MeComponentType> toolboxitems = new ObservableCollection<MeComponentType>();

            foreach (var uiModule in manager.UIModules)
            {
                if (!uiModule.Type.HasFlag(componentType))
                    continue;

                MeComponentType ctype = new MeComponentType();

                ctype.Module = uiModule;

                ctype.IsUnique = uiModule.IsUnique;
                ctype.Header = uiModule.Title;
                if (ctype.Header == null)
                    ctype.Header = uiModule.GetType().Name;

                ctype.Background = new SolidColorBrush(Colors.Gray);
                toolboxitems.Add(ctype);
            }


            return toolboxitems;
        }
        public static ObservableCollection<MeGraphTabItem> GetMeGraphTabItems(
            GageTabItem gageTabItem, IEnumerable<MeComponentType> meComponentTypes)
        {
            IEnumerable<GraphTabItem> graphTabItems = gageTabItem.Items;

            var meGraphTabItems = new ObservableCollection<MeGraphTabItem>();

            foreach (var graphTabItem in graphTabItems)
            {
                MeGraphTabItem meGraphTabItem = new MeGraphTabItem();
                meGraphTabItem.Title = graphTabItem.Header;
                meGraphTabItem.RefreshRC(graphTabItem.RowCount, graphTabItem.ColumnCount);
                meGraphTabItem.ID = graphTabItem.GetHashCode();
                for (int i = 0; i < meGraphTabItem.ColCnt; i++)
                {
                    if (graphTabItem.ColumnDefinitions == null || graphTabItem.ColumnDefinitions.Count() <= i)
                        break;
                    meGraphTabItem.ColumnDefinitions[i] = graphTabItem.ColumnDefinitions[i];
                }
                for (int i = 0; i < meGraphTabItem.RowCnt; i++)
                {
                    if (graphTabItem.RowDefinitions == null || graphTabItem.RowDefinitions.Count() <= i)
                        break;
                    meGraphTabItem.RowDefinitions[i] = graphTabItem.RowDefinitions[i];

                }

                foreach (var graphComponent in graphTabItem.Graphs)
                {
                    var flyComponent = gageTabItem.Components.Find(c => c.ID == graphComponent.ID);

                    var meComponentType = meComponentTypes.First(ct => ct.Module.GetType().FullName == flyComponent.Module);


                    var meGraphComponent = new MeGraphComponent();
                    meGraphComponent.ID = graphComponent.ID;
                    meGraphComponent.mItem = meGraphTabItem;
                    meGraphComponent.Rect = graphComponent.Rect;
                    meGraphComponent.Type = meComponentType;
                    meGraphComponent.ServiceContainerName = flyComponent.ServiceContainerName;
                    meGraphTabItem.Components.Add(meGraphComponent);
                }
                meGraphTabItems.Add(meGraphTabItem);
            }
            return meGraphTabItems;
        }

        public static void ToGraphTabItems(IEnumerable<MeGraphTabItem> meGraphTabItems, GageTabItem gageTabItem)
        {
            //删除存在的
            var graphTabItems = gageTabItem.Items;
            if (gageTabItem.Components.Count() > 0)
            {
                foreach(var graphTabItem in graphTabItems) 
                {
                    gageTabItem.Components.RemoveAll(c => graphTabItem.Graphs.Any(g => g.ID == c.ID));
                }
            }
            graphTabItems.Clear();
            //添加
            foreach (var meGraphTabItem in meGraphTabItems)
            {
                var graphTabItem = new GraphTabItem();
                graphTabItems.Add(graphTabItem);
                
                graphTabItem.Header = meGraphTabItem.Title;
                graphTabItem.RowCount = meGraphTabItem.RowCnt;
                graphTabItem.ColumnCount = meGraphTabItem.ColCnt;
                graphTabItem.ColumnDefinitions.AddRange(meGraphTabItem.ColumnDefinitions);
                graphTabItem.RowDefinitions.AddRange(meGraphTabItem.RowDefinitions);
                
                foreach (var meFlyComponent in meGraphTabItem.Components)
                {
                    if (meFlyComponent.ID == 0)
                        meFlyComponent.ID = meFlyComponent.GetHashCode();

                    var graphGraphItem = new GraphComponent
                    {
                        ID = meFlyComponent.ID,
                        Rect = meFlyComponent.Rect
                    };
                    graphTabItem.Graphs.Add(graphGraphItem);
                    gageTabItem.Components.Add(
                        new FlyComponent()
                        {
                            ID = graphGraphItem.ID,
                            Module = meFlyComponent.Type.Module.GetType().FullName,
                            ServiceContainerName = meFlyComponent.ServiceContainerName
                        });
                }
            }
        }

        public static ObservableCollection<MeFlyComponent> GetMeFlyComponents(
            GageTabItem gageTabItem, IEnumerable<int> ids,
            IEnumerable<MeComponentType> meComponentTypes)
        {
            var meFlyComponents = new ObservableCollection<MeFlyComponent>();
            foreach (var id in ids)
            {
                var flyComponent = gageTabItem.Components.Find(c => c.ID == id);

                var meComponentType = meComponentTypes.First(ct => ct.Module.GetType().FullName == flyComponent.Module);

                var meFlyComponent = new MeFlyComponent();
                meFlyComponent.ID = id;
                meFlyComponent.Type = meComponentType;
                meFlyComponent.ServiceContainerName = flyComponent.ServiceContainerName;
                meFlyComponents.Add(meFlyComponent);
            }
            return meFlyComponents;
        }

        public static void ToFlyComponents(IEnumerable<MeFlyComponent> meFlyComponents, GageTabItem gageTabItem, List<int> ids)
        {
            //删除存在的
            if (gageTabItem.Components.Count() > 0)
            {
                gageTabItem.Components.RemoveAll(c => ids.Any(id => id == c.ID));
            }
            ids.Clear();

            foreach (var meFlyComponent in meFlyComponents)
            {
                if (meFlyComponent.ID == 0)
                    meFlyComponent.ID = meFlyComponent.GetHashCode();

                ids.Add(meFlyComponent.ID);

                gageTabItem.Components.Add(
                    new FlyComponent()
                    {
                        ID = meFlyComponent.ID,
                        Module = meFlyComponent.Type.Module.GetType().FullName,
                        ServiceContainerName = meFlyComponent.ServiceContainerName
                    });
            }
        }
    }
}
