using System;
using System.Collections;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace TD.SandDock
{
	internal class j : TD.SandDock.a
	{
		[MethodImpl(MethodImplOptions.Synchronized)]
		public void b(j.a A_0)
		{
			this.n = (j.a)Delegate.Combine(this.n, A_0);
		}

		[MethodImpl(MethodImplOptions.Synchronized)]
		public void a(j.a A_0)
		{
			this.n = (j.a)Delegate.Remove(this.n, A_0);
		}

		[MethodImpl(MethodImplOptions.Synchronized)]
		public void a(EventHandler A_0)
		{
			this.o = (EventHandler)Delegate.Combine(this.o, A_0);
		}

		[MethodImpl(MethodImplOptions.Synchronized)]
		public void b(EventHandler A_0)
		{
			this.o = (EventHandler)Delegate.Remove(this.o, A_0);
		}

		public j(SandDockManager A_0, DockContainer A_1, LayoutSystemBase A_2, DockControl A_3, Point A_4, DockingHints A_5, bool A_6) : base(A_1, A_5, true, A_1.WorkingRenderer.TabStripMetrics.Height)
		{
			this.b = A_0;
			this.c = A_1;
			this.d = A_2;
			this.e = A_3;
			this.g = A_6;
			if (A_1 is DocumentContainer)
			{
				this.j = new Cursor(base.GetType().Assembly.GetManifestResourceStream("TD.SandDock.Resources.splitting.cur"));
				this.k = new Cursor(base.GetType().Assembly.GetManifestResourceStream("TD.SandDock.Resources.splittingno.cur"));
			}
			if (A_2 is SplitLayoutSystem)
			{
				this.f = ((m)A_1).g();
			}
			else if (A_3 != null)
			{
				this.f = A_3.FloatingSize;
			}
			else if (A_2 is ControlLayoutSystem && ((ControlLayoutSystem)A_2).SelectedControl != null)
			{
				this.f = ((ControlLayoutSystem)A_2).SelectedControl.FloatingSize;
			}
			else
			{
				this.f = A_2.Bounds.Size;
			}
			Rectangle bounds = A_2.Bounds;
			A_4.X -= bounds.Left;
			A_4.X = Convert.ToInt32((float)A_4.X / (float)bounds.Width * (float)this.f.Width);
			if (A_3 != null)
			{
				this.h = new Point(A_4.X, this.f.Height - (bounds.Bottom - A_4.Y));
			}
			else
			{
				this.h = new Point(A_4.X, A_4.Y - bounds.Top);
			}
			if (this.h.Y < 0)
			{
				this.h.Y = 0;
			}
			if (this.h.Y > this.f.Height)
			{
				this.h.Y = this.f.Height;
			}
			this.m = this.d();
			foreach (Screen screen in Screen.AllScreens)
			{
				if (screen.Bounds.Y < this.l)
				{
					this.l = screen.Bounds.Y;
				}
			}
			this.c.OnDockingStarted(EventArgs.Empty);
		}

		protected ControlLayoutSystem[] g()
		{
			return this.m;
		}

		public SandDockManager k()
		{
			return this.b;
		}

		public DockContainer i()
		{
			return this.c;
		}

		public LayoutSystemBase f()
		{
			return this.d;
		}

		public DockControl h()
		{
			return this.e;
		}

		private Point e()
		{
			return new Point(Cursor.Position.X - this.h.X, Cursor.Position.Y - this.h.Y);
		}

		public override void a(Point A_0)
		{
			j.b b = null;
			if ((Control.ModifierKeys & Keys.Control) != Keys.Control)
			{
				b = this.b(A_0);
			}
			if (b == null || (b.a == TD.SandDock.j.c.a && this.b != null && this.g))
			{
				if (this.b != null && this.g)
				{
					b = new j.b(TD.SandDock.j.c.c);
				}
				else
				{
					b = new j.b(TD.SandDock.j.c.b);
				}
			}
			if (b.a == TD.SandDock.j.c.a)
			{
				b.a = TD.SandDock.j.c.b;
			}
			if (b.a == TD.SandDock.j.c.c)
			{
				b.e = new Rectangle(this.e(), this.f);
				if (b.e.Y < this.l)
				{
					b.e.Y = this.l;
				}
			}
			if (b.c == this.d && this.e != null && b.d == DockSide.None)
			{
				base.l();
				ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)this.d;
				if (b.f != controlLayoutSystem.Controls.IndexOf(this.e) && b.f != controlLayoutSystem.Controls.IndexOf(this.e) + 1)
				{
					controlLayoutSystem.Controls.SetChildIndex(this.e, b.f);
				}
				b.a = TD.SandDock.j.c.g;
			}
			else if (b.a == TD.SandDock.j.c.b)
			{
				base.l();
			}
			else
			{
				base.a(b.e, b.a == TD.SandDock.j.c.e);
			}
			if (this.c is DocumentContainer)
			{
				if (b.a == TD.SandDock.j.c.g)
				{
					Cursor.Current = Cursors.Default;
				}
				else if (b.a == TD.SandDock.j.c.b)
				{
					Cursor.Current = this.k;
				}
				else
				{
					Cursor.Current = this.j;
				}
			}
			this.i = b;
		}

		public j.b j()
		{
			return this.i;
		}

		private Rectangle a(DockContainer A_0)
		{
			Rectangle empty = new Rectangle(A_0.PointToScreen(new Point(0, 0)), A_0.Size);
			if (A_0.Empty)
			{
				switch (A_0.Dock)
				{
				case DockStyle.Top:
					empty.Height += 30;
					break;
				case DockStyle.Bottom:
					empty.Offset(0, -30);
					empty.Height += 30;
					break;
				case DockStyle.Left:
					empty.Width += 30;
					break;
				case DockStyle.Right:
					empty.Offset(-30, 0);
					empty.Width += 30;
					break;
				}
			}
			else if (!A_0.Collapsed && A_0.Sizable)
			{
				switch (A_0.Dock)
				{
				case DockStyle.Top:
					empty.Y = empty.Bottom - 4;
					empty.Height = 4;
					break;
				case DockStyle.Bottom:
					empty.Height = 4;
					break;
				case DockStyle.Left:
					empty.X = empty.Right - 4;
					empty.Width = 4;
					break;
				case DockStyle.Right:
					empty.Width = 4;
					break;
				default:
					empty = Rectangle.Empty;
					break;
				}
			}
			else if (!A_0.Collapsed)
			{
				empty = Rectangle.Empty;
			}
			return empty;
		}

		protected Rectangle a(DockContainer A_0, LayoutSystemBase A_1)
		{
			int num = (A_1.f.Width < A_1.f.Height) ? ((int)A_1.f.Width) : ((int)A_1.f.Height);
			int num2;
			if (A_0.Empty)
			{
				if (A_0.ContentSize > 0)
				{
					num2 = A_0.ContentSize;
				}
				else
				{
					num2 = num;
				}
			}
			else
			{
				num2 = A_0.ContentSize + num;
			}
			if (A_0.Collapsed && A_0.ContentSize > 0)
			{
				num = A_0.ContentSize;
			}
			Rectangle result = new Rectangle(A_0.PointToScreen(new Point(0, 0)), A_0.Size);
			if (A_0.Empty)
			{
				switch (A_0.Dock)
				{
				case DockStyle.Top:
					result.Height += num2;
					break;
				case DockStyle.Bottom:
					result.Offset(0, -num2);
					result.Height += num2;
					break;
				case DockStyle.Left:
					result.Width += num2;
					break;
				case DockStyle.Right:
					result.Offset(-num2, 0);
					result.Width += num2;
					break;
				}
			}
			else
			{
				switch (A_0.Dock)
				{
				case DockStyle.Top:
					result.Y = result.Bottom;
					result.Height = num;
					break;
				case DockStyle.Bottom:
					result.Y -= num;
					result.Height = num;
					break;
				case DockStyle.Left:
					result.X = result.Right;
					result.Width = num;
					break;
				case DockStyle.Right:
					result.X -= num;
					result.Width = num;
					break;
				}
			}
			return result;
		}

		private ControlLayoutSystem[] d()
		{
			ArrayList arrayList = new ArrayList();
			if (this.b == null)
			{
				this.a(this.i(), arrayList);
			}
			else
			{
				foreach (object obj in this.b.a)
				{
					DockContainer dockContainer = (DockContainer)obj;
					if (dockContainer.IsFloating && ((m)dockContainer).l().Visible && (!(this.d is SplitLayoutSystem) || this.d.DockContainer != dockContainer))
					{
						this.a(dockContainer, arrayList);
					}
				}
				foreach (object obj2 in this.b.a)
				{
					DockContainer dockContainer2 = (DockContainer)obj2;
					if (!dockContainer2.IsFloating && !dockContainer2.Empty)
					{
						this.a(dockContainer2, arrayList);
					}
				}
			}
			ControlLayoutSystem[] array = new ControlLayoutSystem[checked((uint)arrayList.Count)];
			arrayList.CopyTo(array, 0);
			return array;
		}

		private void a(DockContainer A_0, ArrayList A_1)
		{
			if (!A_0.Collapsed && A_0.Enabled && A_0.Visible)
			{
				this.a(A_0, A_0.LayoutSystem, A_1);
			}
		}

		private void a(DockContainer A_0, SplitLayoutSystem A_1, ArrayList A_2)
		{
			foreach (object obj in A_1.LayoutSystems)
			{
				LayoutSystemBase layoutSystemBase = (LayoutSystemBase)obj;
				if (layoutSystemBase is SplitLayoutSystem)
				{
					this.a(A_0, (SplitLayoutSystem)layoutSystemBase, A_2);
				}
				else if (layoutSystemBase is ControlLayoutSystem)
				{
					bool flag = (this.e == null || layoutSystemBase != this.d || this.e.b.Controls.Count != 1) && !((ControlLayoutSystem)layoutSystemBase).Collapsed;
					if (flag)
					{
						A_2.Add(layoutSystemBase);
					}
				}
			}
		}

		protected virtual j.b b(Point A_0)
		{
			if (this.b != null)
			{
				foreach (object obj in this.b.a)
				{
					DockContainer dockContainer = (DockContainer)obj;
					if (dockContainer.IsFloating && ((m)dockContainer).l().Visible && ((m)dockContainer).HasSingleControlLayoutSystem && dockContainer.LayoutSystem != this.d)
					{
						Rectangle rectangle = ((m)dockContainer).a();
						if (rectangle.Contains(A_0))
						{
							rectangle = new Rectangle(dockContainer.PointToScreen(dockContainer.LayoutSystem.LayoutSystems[0].Bounds.Location), dockContainer.LayoutSystem.LayoutSystems[0].Bounds.Size);
							if (!rectangle.Contains(A_0))
							{
								return new j.b(TD.SandDock.j.c.e)
								{
									b = dockContainer,
									c = (ControlLayoutSystem)dockContainer.LayoutSystem.LayoutSystems[0],
									e = ((m)dockContainer).a()
								};
							}
						}
					}
				}
			}
			foreach (ControlLayoutSystem controlLayoutSystem in this.m)
			{
				Rectangle rectangle = new Rectangle(controlLayoutSystem.DockContainer.PointToScreen(controlLayoutSystem.Bounds.Location), controlLayoutSystem.Bounds.Size);
				if (rectangle.Contains(A_0))
				{
					j.b b = this.a(controlLayoutSystem.DockContainer, controlLayoutSystem, A_0, true);
					if (b != null)
					{
						return b;
					}
				}
			}
			if (this.k() != null)
			{
				foreach (object obj2 in this.b.a)
				{
					DockContainer dockContainer2 = (DockContainer)obj2;
					if (!dockContainer2.IsFloating && dockContainer2.Enabled && dockContainer2.Visible && (dockContainer2.Empty || this.e != null || dockContainer2 != this.c || !dockContainer2.HasSingleControlLayoutSystem))
					{
						Rectangle rectangle = this.a(dockContainer2);
						if (rectangle.Contains(A_0))
						{
							return new j.b(TD.SandDock.j.c.f)
							{
								b = dockContainer2,
								e = this.a(dockContainer2, this.d)
							};
						}
					}
				}
			}
			return null;
		}

		protected j.b a(DockContainer A_0, ControlLayoutSystem A_1, Point A_2, bool A_3)
		{
			j.b b = new j.b(TD.SandDock.j.c.a);
			Point point = A_0.PointToClient(A_2);
			if (this.e != null || A_1 != this.d)
			{
				if (A_1.JoinCatchmentBounds.Contains(point) || A_1.f.Contains(point))
				{
					b = new j.b(TD.SandDock.j.c.e);
					b.b = A_0;
					b.c = A_1;
					b.d = DockSide.None;
					b.e = new Rectangle(A_0.PointToScreen(A_1.Bounds.Location), A_1.Bounds.Size);
					if (A_1.f.Contains(point))
					{
						b.f = A_1.a(point);
					}
					else
					{
						b.f = A_1.Controls.Count;
					}
				}
				if (b.a == TD.SandDock.j.c.a && A_3)
				{
					b = this.a(A_0, A_1, A_2);
				}
				return b;
			}
			if (A_1.JoinCatchmentBounds.Contains(point))
			{
				return new j.b(TD.SandDock.j.c.b);
			}
			return new j.b(TD.SandDock.j.c.a);
		}

		private j.b a(DockContainer A_0, ControlLayoutSystem A_1, Point A_2)
		{
			j.b b = null;
			Point point = A_0.PointToClient(A_2);
			Rectangle a_ = A_1.g;
			if (new Rectangle(a_.Left, a_.Top, a_.Width, 30).Contains(point))
			{
				b = this.a(A_0, A_1);
				if (point.X < a_.Left + 30)
				{
					this.a(A_0, A_1, b, a_, point);
				}
				else if (point.X > a_.Right - 30)
				{
					this.b(A_0, A_1, b, a_, point);
				}
				else
				{
					this.a(A_0, A_1, b, DockSide.Top);
				}
			}
			else if (new Rectangle(a_.Left, a_.Top, 30, a_.Height).Contains(point))
			{
				b = this.a(A_0, A_1);
				if (point.Y < a_.Top + 30)
				{
					this.a(A_0, A_1, b, a_, point);
				}
				else if (point.Y > a_.Bottom - 30)
				{
					this.c(A_0, A_1, b, a_, point);
				}
				else
				{
					this.a(A_0, A_1, b, DockSide.Left);
				}
			}
			else if (new Rectangle(a_.Right - 30, a_.Top, 30, a_.Height).Contains(point))
			{
				b = this.a(A_0, A_1);
				if (point.Y < a_.Top + 30)
				{
					this.b(A_0, A_1, b, a_, point);
				}
				else if (point.Y > a_.Bottom - 30)
				{
					this.d(A_0, A_1, b, a_, point);
				}
				else
				{
					this.a(A_0, A_1, b, DockSide.Right);
				}
			}
			else if (new Rectangle(a_.Left, a_.Bottom - 30, a_.Width, 30).Contains(point))
			{
				b = this.a(A_0, A_1);
				if (point.X < a_.Left + 30)
				{
					this.c(A_0, A_1, b, a_, point);
				}
				else if (point.X > a_.Right - 30)
				{
					this.d(A_0, A_1, b, a_, point);
				}
				else
				{
					this.a(A_0, A_1, b, DockSide.Bottom);
				}
			}
			return b;
		}

		private void d(DockContainer A_0, ControlLayoutSystem A_1, j.b A_2, Rectangle A_3, Point A_4)
		{
			A_3.X = A_3.Right - 30;
			A_3.Y = A_3.Bottom - 30;
			A_4.X -= A_3.Left;
			A_4.Y -= A_3.Top;
			A_3 = new Rectangle(0, 0, 30, 30);
			if (A_4.Y > A_3.Top + (int)((float)A_3.Height * ((float)A_4.X / (float)A_3.Width)))
			{
				this.a(A_0, A_1, A_2, DockSide.Bottom);
				return;
			}
			this.a(A_0, A_1, A_2, DockSide.Right);
		}

		private void c(DockContainer A_0, ControlLayoutSystem A_1, j.b A_2, Rectangle A_3, Point A_4)
		{
			A_3.Y = A_3.Bottom - 30;
			A_4.X -= A_3.Left;
			A_4.Y -= A_3.Top;
			A_3 = new Rectangle(0, 0, 30, 30);
			if (A_4.Y > A_3.Bottom - (int)((float)A_3.Height * ((float)A_4.X / (float)A_3.Width)))
			{
				this.a(A_0, A_1, A_2, DockSide.Bottom);
				return;
			}
			this.a(A_0, A_1, A_2, DockSide.Left);
		}

		private void b(DockContainer A_0, ControlLayoutSystem A_1, j.b A_2, Rectangle A_3, Point A_4)
		{
			A_3.X = A_3.Right - 30;
			A_4.X -= A_3.Left;
			A_4.Y -= A_3.Top;
			A_3 = new Rectangle(0, 0, 30, 30);
			if (A_4.Y > A_3.Top + (int)((float)A_3.Height * ((float)(A_3.Right - A_4.X) / (float)A_3.Width)))
			{
				this.a(A_0, A_1, A_2, DockSide.Right);
				return;
			}
			this.a(A_0, A_1, A_2, DockSide.Top);
		}

		private void a(DockContainer A_0, ControlLayoutSystem A_1, j.b A_2, Rectangle A_3, Point A_4)
		{
			A_4.X -= A_3.Left;
			A_4.Y -= A_3.Top;
			A_3 = new Rectangle(0, 0, 30, 30);
			if (A_4.Y > A_3.Top + (int)((float)A_3.Height * ((float)A_4.X / (float)A_3.Width)))
			{
				this.a(A_0, A_1, A_2, DockSide.Left);
				return;
			}
			this.a(A_0, A_1, A_2, DockSide.Top);
		}

		private void a(DockContainer A_0, ControlLayoutSystem A_1, j.b A_2, DockSide A_3)
		{
			A_2.e = this.a(A_0, A_1, A_3);
			A_2.d = A_3;
		}

		internal Rectangle a(DockContainer A_0, ControlLayoutSystem A_1, DockSide A_2)
		{
			Rectangle result = new Rectangle(A_0.PointToScreen(A_1.Bounds.Location), A_1.Bounds.Size);
			switch (A_2)
			{
			case DockSide.Top:
				result.Height /= 2;
				break;
			case DockSide.Bottom:
				result.Offset(0, result.Height / 2);
				result.Height /= 2;
				break;
			case DockSide.Left:
				result.Width /= 2;
				break;
			case DockSide.Right:
				result.Offset(result.Width / 2, 0);
				result.Width /= 2;
				break;
			}
			return result;
		}

		private j.b a(DockContainer A_0, ControlLayoutSystem A_1)
		{
			return new j.b(TD.SandDock.j.c.d)
			{
				b = A_0,
				c = A_1
			};
		}

		public override void a()
		{
			base.a();
			if (this.n != null)
			{
				this.n(this.i);
			}
		}

		public override void c()
		{
			base.c();
			if (this.o != null)
			{
				this.o(this, EventArgs.Empty);
			}
		}

		public override void b()
		{
			this.c.OnDockingFinished(EventArgs.Empty);
			if (this.j != null)
			{
				this.j.Dispose();
			}
			if (this.k != null)
			{
				this.k.Dispose();
			}
			base.b();
		}

		private new const int a = 30;

		private new SandDockManager b = null;

		private new DockContainer c = null;

		private LayoutSystemBase d = null;

		private DockControl e = null;

		private new Size f = Size.Empty;

		private bool g = true;

		private Point h = Point.Empty;

		private j.b i = null;

		private Cursor j;

		private Cursor k;

		private new int l = 0;

		private ControlLayoutSystem[] m = null;

		private j.a n;

		private EventHandler o;

		public new enum c
		{
			a,
			b,
			c,
			d,
			e,
			f,
			g
		}

		public new delegate void a(j.b A_0);

		public new class b
		{
			public b(j.c A_0)
			{
				this.a = A_0;
			}

			public j.c a = TD.SandDock.j.c.a;

			public DockContainer b = null;

			public ControlLayoutSystem c = null;

			public DockSide d = DockSide.None;

			public Rectangle e = Rectangle.Empty;

			public int f = 0;
		}
	}
}
