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

namespace TD.SandDock
{
	internal class DockContainerDesigner : ControlDesigner
	{
		public DockContainerDesigner()
		{
			this.a = new DesignerVerbCollection();
			this.a.Add(new DesignerVerb("Add Control", new EventHandler(this.b)));
		}

		private DockControl b(Point A_0)
		{
			LayoutSystemBase layoutSystemAt = this.b.GetLayoutSystemAt(A_0);
			if (layoutSystemAt is ControlLayoutSystem)
			{
				return ((ControlLayoutSystem)layoutSystemAt).GetControlAt(A_0);
			}
			return null;
		}

		protected virtual void a(int A_0, int A_1)
		{
			ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
			Point point = this.b.PointToClient(new Point(A_0, A_1));
			LayoutSystemBase layoutSystemAt = this.b.GetLayoutSystemAt(point);
			if (layoutSystemAt is SplitLayoutSystem)
			{
				SplitLayoutSystem splitLayoutSystem = (SplitLayoutSystem)layoutSystemAt;
				if (splitLayoutSystem.a(point.X, point.Y))
				{
					LayoutSystemBase a_;
					LayoutSystemBase a_2;
					splitLayoutSystem.a(point, out a_, out a_2);
					this.e = new q(this.b, splitLayoutSystem, a_, a_2, point, DockingHints.TranslucentFill);
					this.e.a(new EventHandler(this.c));
					this.e.b(new q.a(this.a));
					this.b.Capture = true;
					return;
				}
			}
			else if (layoutSystemAt is ControlLayoutSystem)
			{
				ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)layoutSystemAt;
				DockControl controlAt = controlLayoutSystem.GetControlAt(point);
				if (controlAt != null && controlAt.b.SelectedControl != controlAt)
				{
					IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
					componentChangeService.OnComponentChanging(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"]);
					controlAt.b.SelectedControl = controlAt;
					componentChangeService.OnComponentChanged(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"], null, null);
				}
				if (controlLayoutSystem.e.Contains(point) || controlAt != null)
				{
					if (controlLayoutSystem.SelectedControl != null)
					{
						selectionService.SetSelectedComponents(new object[]
						{
							controlLayoutSystem.SelectedControl
						}, SelectionTypes.MouseDown | SelectionTypes.Click);
					}
					this.c = new Point(A_0, A_1);
					return;
				}
				selectionService.SetSelectedComponents(new object[]
				{
					this.b
				}, SelectionTypes.MouseDown | SelectionTypes.Click);
				this.b.Capture = true;
				return;
			}
			selectionService.SetSelectedComponents(new object[]
			{
				this.b
			}, SelectionTypes.MouseDown | SelectionTypes.Click);
		}

		protected virtual void a(bool A_0)
		{
			this.c = Point.Empty;
			this.Control.Capture = false;
			if (this.e != null)
			{
				this.e.a();
				this.b.Capture = false;
				return;
			}
			if (this.d != null)
			{
				this.d.a();
				this.b.Capture = false;
				return;
			}
			DockControl dockControl = this.b(this.b.PointToClient(Cursor.Position));
			if (dockControl != null)
			{
				return;
			}
			LayoutSystemBase layoutSystemAt = this.b.GetLayoutSystemAt(this.b.PointToClient(Cursor.Position));
			if (layoutSystemAt is ControlLayoutSystem)
			{
				return;
			}
		}

		protected virtual void b(int A_0, int A_1)
		{
			Point a_ = this.b.PointToClient(new Point(A_0, A_1));
			if (this.e != null)
			{
				this.e.a(a_);
				return;
			}
			if (this.d == null)
			{
				if (this.c != Point.Empty)
				{
					Rectangle rectangle = new Rectangle(this.c, SystemInformation.DragSize);
					rectangle.Offset(-SystemInformation.DragSize.Width / 2, -SystemInformation.DragSize.Height / 2);
					if (!rectangle.Contains(A_0, A_1))
					{
						this.a(this.b.PointToClient(this.c));
						this.c = Point.Empty;
					}
				}
				return;
			}
			this.d.a(Cursor.Position);
			if (this.d.j() == null || this.d.j().a == j.c.b)
			{
				Cursor.Current = Cursors.No;
				return;
			}
			Cursor.Current = Cursors.Default;
		}

		private void a(Point A_0)
		{
			LayoutSystemBase layoutSystemAt = this.b.GetLayoutSystemAt(A_0);
			if (layoutSystemAt is ControlLayoutSystem && this.d == null)
			{
				DockControl controlAt = ((ControlLayoutSystem)layoutSystemAt).GetControlAt(A_0);
				this.d = new j(this.b.Manager, this.b, layoutSystemAt, controlAt, A_0, DockingHints.TranslucentFill, false);
				this.d.b(new j.a(this.a));
				this.d.a(new EventHandler(this.d));
				this.b.Capture = true;
			}
		}

		private void b()
		{
			this.d.a(new j.a(this.a));
			this.d.b(new EventHandler(this.d));
			this.d = null;
		}

		internal virtual void a(j.b A_0)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)this.GetService(typeof(IDesignerHost));
			ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)this.d.f();
			bool flag = this.d.h() == null;
			DockControl selectedControl = controlLayoutSystem.SelectedControl;
			this.b();
			if (A_0 == null || A_0.a == j.c.b || A_0.a == j.c.g)
			{
				return;
			}
			DesignerTransaction designerTransaction = designerHost.CreateTransaction("Move DockControl");
			componentChangeService.OnComponentChanging(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"]);
			if (flag)
			{
				((SplitLayoutSystem)controlLayoutSystem.Parent).LayoutSystems.Remove(controlLayoutSystem);
			}
			else
			{
				g.a(selectedControl);
			}
			componentChangeService.OnComponentChanged(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"], null, null);
			if (A_0.b != null)
			{
				componentChangeService.OnComponentChanging(A_0.b, TypeDescriptor.GetProperties(A_0.b)["LayoutSystem"]);
				controlLayoutSystem.a(selectedControl, flag, A_0);
				componentChangeService.OnComponentChanged(A_0.b, TypeDescriptor.GetProperties(A_0.b)["LayoutSystem"], null, null);
			}
			designerTransaction.Commit();
		}

		internal virtual void d(object A_0, EventArgs A_1)
		{
			this.b();
		}

		private void a()
		{
			this.e.b(new EventHandler(this.c));
			this.e.a(new q.a(this.a));
			this.e = null;
		}

		private void c(object A_0, EventArgs A_1)
		{
			this.a();
		}

		private void a(LayoutSystemBase A_0, LayoutSystemBase A_1, int A_2, int A_3)
		{
			SplitLayoutSystem splitLayoutSystem = this.e.d();
			this.a();
			IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
			componentChangeService.OnComponentChanging(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"]);
			if (splitLayoutSystem.SplitMode == Orientation.Horizontal)
			{
				A_0.f.Height = (float)A_2;
				A_1.f.Height = (float)A_3;
			}
			else
			{
				A_0.f.Width = (float)A_2;
				A_1.f.Width = (float)A_3;
			}
			componentChangeService.OnComponentChanged(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"], null, null);
			splitLayoutSystem.c();
		}

		protected virtual void c()
		{
			Point position = this.b.PointToClient(Cursor.Position);
			LayoutSystemBase layoutSystemAt = this.b.GetLayoutSystemAt(position);
			if (!(layoutSystemAt is SplitLayoutSystem) || !((SplitLayoutSystem)layoutSystemAt).a(position.X, position.Y))
			{
				Cursor.Current = Cursors.Default;
				return;
			}
			if (((SplitLayoutSystem)layoutSystemAt).SplitMode == Orientation.Horizontal)
			{
				Cursor.Current = Cursors.HSplit;
				return;
			}
			Cursor.Current = Cursors.VSplit;
		}

		public void e()
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)this.GetService(typeof(IDesignerHost));
			ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
			DesignerTransaction designerTransaction = designerHost.CreateTransaction("Add Dock Control");
			ControlLayoutSystem controlLayoutSystem = new ControlLayoutSystem();
			DockControl dockControl = (DockControl)designerHost.CreateComponent(typeof(DockControl));
			((ComponentDesigner)designerHost.GetDesigner(dockControl)).OnSetComponentDefaults();
			componentChangeService.OnComponentChanging(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"]);
			this.b.LayoutSystem.LayoutSystems.Add(controlLayoutSystem);
			controlLayoutSystem.Controls.Add(dockControl);
			componentChangeService.OnComponentChanged(this.b, TypeDescriptor.GetProperties(this.b)["LayoutSystem"], null, null);
			designerTransaction.Commit();
			selectionService.SetSelectedComponents(new object[]
			{
				dockControl
			});
		}

		private void b(object A_0, EventArgs A_1)
		{
			this.e();
		}

		public virtual ICollection AssociatedComponents
		{
			get
			{
				return this.b.Controls;
			}
		}

		public virtual void a(IComponent A_0)
		{
			base.Initialize(A_0);
			ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
			selectionService.SelectionChanged += this.a;
			this.b = (DockContainer)A_0;
		}

		protected virtual void b(bool A_0)
		{
			ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
			selectionService.SelectionChanged -= this.a;
			base.Dispose(A_0);
		}

		public override DesignerVerbCollection Verbs
		{
			get
			{
				return this.a;
			}
		}

		private void a(object A_0, EventArgs A_1)
		{
			foreach (object obj in this.b.c)
			{
				LayoutSystemBase layoutSystemBase = (LayoutSystemBase)obj;
				if (layoutSystemBase is ControlLayoutSystem)
				{
					((ControlLayoutSystem)layoutSystemBase).InvalidateTitleBar();
				}
			}
		}

		private DesignerVerbCollection a = null;

		private DockContainer b = null;

		private Point c = Point.Empty;

		private j d = null;

		private q e = null;
	}
}
