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
	{
		private DesignerVerbCollection m_a = null;

		private DockContainer m_b = null;

		private Point m_c = Point.Empty;

		private j m_d = null;

		private q m_e = null;

		public override ICollection AssociatedComponents => this.m_b.Controls;

		public override DesignerVerbCollection Verbs => this.m_a;

		public DockContainerDesigner()
		{
			this.m_a = new DesignerVerbCollection();
			this.m_a.Add(new DesignerVerb("Add Control", b));
		}

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

		protected override void a(int A_0, int A_1)
		{
			ISelectionService selectionService = (ISelectionService)GetService(typeof(ISelectionService));
			Point point = this.m_b.PointToClient(new Point(A_0, A_1));
			LayoutSystemBase layoutSystemAt = this.m_b.GetLayoutSystemAt(point);
			if (layoutSystemAt is SplitLayoutSystem)
			{
				SplitLayoutSystem splitLayoutSystem = (SplitLayoutSystem)layoutSystemAt;
				if (splitLayoutSystem.a(point.X, point.Y))
				{
					splitLayoutSystem.a(point, out var A_2, out var A_3);
					this.m_e = new q(this.m_b, splitLayoutSystem, A_2, A_3, point, DockingHints.TranslucentFill);
					this.m_e.a(c);
					this.m_e.b((q.a)a);
					this.m_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)GetService(typeof(IComponentChangeService));
					componentChangeService.OnComponentChanging(this.m_b, TypeDescriptor.GetProperties(this.m_b)["LayoutSystem"]);
					controlAt.b.SelectedControl = controlAt;
					componentChangeService.OnComponentChanged(this.m_b, TypeDescriptor.GetProperties(this.m_b)["LayoutSystem"], null, null);
				}
				if (controlLayoutSystem.e.Contains(point) || controlAt != null)
				{
					if (controlLayoutSystem.SelectedControl != null)
					{
						selectionService.SetSelectedComponents(new object[1] { controlLayoutSystem.SelectedControl }, SelectionTypes.MouseDown | SelectionTypes.Click);
					}
					ref Point reference = ref this.m_c;
					reference = new Point(A_0, A_1);
				}
				else
				{
					selectionService.SetSelectedComponents(new object[1] { this.m_b }, SelectionTypes.MouseDown | SelectionTypes.Click);
					this.m_b.Capture = true;
				}
				return;
			}
			selectionService.SetSelectedComponents(new object[1] { this.m_b }, SelectionTypes.MouseDown | SelectionTypes.Click);
		}

		protected override void a(bool A_0)
		{
			this.m_c = Point.Empty;
			Control.Capture = false;
			if (this.m_e != null)
			{
				this.m_e.a();
				this.m_b.Capture = false;
				return;
			}
			if (this.m_d != null)
			{
				this.m_d.a();
				this.m_b.Capture = false;
				return;
			}
			DockControl dockControl = b(this.m_b.PointToClient(Cursor.Position));
			if (dockControl == null)
			{
				LayoutSystemBase layoutSystemAt = this.m_b.GetLayoutSystemAt(this.m_b.PointToClient(Cursor.Position));
				if (!(layoutSystemAt is ControlLayoutSystem))
				{
				}
			}
		}

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

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

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

		internal virtual void a(j.b A_0)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
			ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)this.m_d.f();
			bool flag = this.m_d.h() == null;
			DockControl selectedControl = controlLayoutSystem.SelectedControl;
			b();
			if (A_0 != null && A_0.a != j.c.b && A_0.a != j.c.g)
			{
				DesignerTransaction designerTransaction = designerHost.CreateTransaction("Move DockControl");
				componentChangeService.OnComponentChanging(this.m_b, TypeDescriptor.GetProperties(this.m_b)["LayoutSystem"]);
				if (flag)
				{
					((SplitLayoutSystem)controlLayoutSystem.Parent).LayoutSystems.Remove(controlLayoutSystem);
				}
				else
				{
					g.a(selectedControl);
				}
				componentChangeService.OnComponentChanged(this.m_b, TypeDescriptor.GetProperties(this.m_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)
		{
			b();
		}

		private void a()
		{
			this.m_e.b(c);
			this.m_e.a((q.a)a);
			this.m_e = null;
		}

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

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

		protected override void c()
		{
			Point position = this.m_b.PointToClient(Cursor.Position);
			LayoutSystemBase layoutSystemAt = this.m_b.GetLayoutSystemAt(position);
			if (layoutSystemAt is SplitLayoutSystem && ((SplitLayoutSystem)layoutSystemAt).a(position.X, position.Y))
			{
				if (((SplitLayoutSystem)layoutSystemAt).SplitMode == Orientation.Horizontal)
				{
					Cursor.Current = Cursors.HSplit;
				}
				else
				{
					Cursor.Current = Cursors.VSplit;
				}
			}
			else
			{
				Cursor.Current = Cursors.Default;
			}
		}

		public void e()
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
			ISelectionService selectionService = (ISelectionService)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.m_b, TypeDescriptor.GetProperties(this.m_b)["LayoutSystem"]);
			this.m_b.LayoutSystem.LayoutSystems.Add(controlLayoutSystem);
			controlLayoutSystem.Controls.Add(dockControl);
			componentChangeService.OnComponentChanged(this.m_b, TypeDescriptor.GetProperties(this.m_b)["LayoutSystem"], null, null);
			designerTransaction.Commit();
			selectionService.SetSelectedComponents(new object[1] { dockControl });
		}

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

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

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

		private void a(object A_0, EventArgs A_1)
		{
			foreach (LayoutSystemBase item in this.m_b.c)
			{
				if (item is ControlLayoutSystem)
				{
					((ControlLayoutSystem)item).InvalidateTitleBar();
				}
			}
		}
	}
}
