using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

namespace TD.SandDock
{
	internal class SandDockManagerDesigner : ComponentDesigner
	{
		private SandDockManager m_a = null;

		private DesignerVerbCollection b = null;

		public override DesignerVerbCollection Verbs => b;

		public override ICollection AssociatedComponents => this.m_a.a;

		public SandDockManagerDesigner()
		{
			b = new DesignerVerbCollection();
			b.Add(new DesignerVerb("Add Control", a));
		}

		private void a(object A_0, EventArgs A_1)
		{
			IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
			DockContainer dockContainer = this.m_a.a(DockStyle.Right);
			if (dockContainer != null)
			{
				DockContainerDesigner dockContainerDesigner = (DockContainerDesigner)designerHost.GetDesigner(dockContainer);
				dockContainerDesigner.e();
			}
		}

		public override void Initialize(IComponent component)
		{
			base.Initialize(component);
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			componentChangeService.ComponentRemoving += a;
			this.m_a = (SandDockManager)component;
		}

		protected override void a(bool A_0)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			componentChangeService.ComponentRemoving -= a;
			base.Dispose(A_0);
		}

		private void a(object A_0, ComponentEventArgs A_1)
		{
			if (A_1.Component != this.m_a)
			{
				return;
			}
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			DockContainer[] array = new DockContainer[checked((uint)this.m_a.a.Count)];
			this.m_a.a.CopyTo(array);
			DockContainer[] array2 = array;
			foreach (DockContainer dockContainer in array2)
			{
				Control parent = dockContainer.Parent;
				if (parent != null)
				{
					componentChangeService.OnComponentChanging(parent, TypeDescriptor.GetProperties(parent)["Controls"]);
					dockContainer.Dispose();
					componentChangeService.OnComponentChanged(parent, TypeDescriptor.GetProperties(parent)["Controls"], null, null);
				}
			}
		}

		private DockContainer a(IDesignerHost A_0, Control A_1, DockStyle A_2, string A_3)
		{
			DockContainer dockContainer = new DockContainer();
			dockContainer.Manager = this.m_a;
			dockContainer.Size = new Size(0, 0);
			dockContainer.Dock = A_2;
			A_0.Container.Add(dockContainer, A_3);
			A_1.Controls.Add(dockContainer);
			dockContainer.SendToBack();
			return dockContainer;
		}

		public override void a()
		{
			base.OnSetComponentDefaults();
			IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			if (designerHost.RootComponent is Control a_)
			{
				componentChangeService.OnComponentChanging(a_, null);
				a(designerHost, a_, DockStyle.Left, "leftSandDock");
				a(designerHost, a_, DockStyle.Right, "rightSandDock");
				a(designerHost, a_, DockStyle.Bottom, "bottomSandDock");
				a(designerHost, a_, DockStyle.Top, "topSandDock");
				componentChangeService.OnComponentChanged(a_, null, null, null);
				a(designerHost, "leftSandBarDock");
				a(designerHost, "rightSandBarDock");
				a(designerHost, "bottomSandBarDock");
				a(designerHost, "topSandBarDock");
				a((object)null, (EventArgs)null);
			}
		}

		private void a(IDesignerHost A_0, string A_1)
		{
			IComponent component = A_0.Container.Components[A_1];
			if (component is Control)
			{
				((Control)component).SendToBack();
			}
		}
	}
}
