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

namespace TD.SandDock
{
	internal class j : a
	{
		public new enum c
		{
			a,
			b,
			c,
			d,
			e,
			f,
			g
		}

		public new delegate void a(b A_0);

		public new class b
		{
			public 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;

			public b(c A_0)
			{
				a = A_0;
			}
		}

		private new const int m_a = 30;

		private new SandDockManager m_b = null;

		private new DockContainer m_c = null;

		private LayoutSystemBase m_d = null;

		private DockControl m_e = null;

		private new Size m_f = Size.Empty;

		private bool m_g = true;

		private Point m_h = Point.Empty;

		private b m_i = null;

		private Cursor m_j;

		private Cursor m_k;

		private new int l = 0;

		private ControlLayoutSystem[] m = null;

		private a n;

		private EventHandler o;

		[MethodImpl(MethodImplOptions.Synchronized)]
		[SpecialName]
		public void b(a A_0)
		{
			n = (a)Delegate.Combine(n, A_0);
		}

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

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

		[MethodImpl(MethodImplOptions.Synchronized)]
		[SpecialName]
		public void b(EventHandler A_0)
		{
			o = (EventHandler)Delegate.Remove(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, A_2: true, A_1.WorkingRenderer.TabStripMetrics.Height)
		{
			this.m_b = A_0;
			this.m_c = A_1;
			this.m_d = A_2;
			this.m_e = A_3;
			this.m_g = A_6;
			if (A_1 is DocumentContainer)
			{
				this.m_j = new Cursor(GetType().Assembly.GetManifestResourceStream("TD.SandDock.Resources.splitting.cur"));
				this.m_k = new Cursor(GetType().Assembly.GetManifestResourceStream("TD.SandDock.Resources.splittingno.cur"));
			}
			if (A_2 is SplitLayoutSystem)
			{
				this.m_f = ((m)A_1).g();
			}
			else if (A_3 != null)
			{
				this.m_f = A_3.FloatingSize;
			}
			else if (A_2 is ControlLayoutSystem && ((ControlLayoutSystem)A_2).SelectedControl != null)
			{
				this.m_f = ((ControlLayoutSystem)A_2).SelectedControl.FloatingSize;
			}
			else
			{
				this.m_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.m_f.Width);
			if (A_3 != null)
			{
				ref Point reference = ref this.m_h;
				reference = new Point(A_4.X, this.m_f.Height - (bounds.Bottom - A_4.Y));
			}
			else
			{
				ref Point reference2 = ref this.m_h;
				reference2 = new Point(A_4.X, A_4.Y - bounds.Top);
			}
			if (this.m_h.Y < 0)
			{
				this.m_h.Y = 0;
			}
			if (this.m_h.Y > this.m_f.Height)
			{
				this.m_h.Y = this.m_f.Height;
			}
			m = d();
			Screen[] allScreens = Screen.AllScreens;
			foreach (Screen screen in allScreens)
			{
				if (screen.Bounds.Y < l)
				{
					l = screen.Bounds.Y;
				}
			}
			this.m_c.OnDockingStarted(EventArgs.Empty);
		}

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

		[SpecialName]
		public SandDockManager k()
		{
			return this.m_b;
		}

		[SpecialName]
		public DockContainer i()
		{
			return this.m_c;
		}

		[SpecialName]
		public new LayoutSystemBase f()
		{
			return this.m_d;
		}

		[SpecialName]
		public DockControl h()
		{
			return this.m_e;
		}

		[SpecialName]
		private Point e()
		{
			return new Point(Cursor.Position.X - this.m_h.X, Cursor.Position.Y - this.m_h.Y);
		}

		public override void a(Point A_0)
		{
			b b2 = null;
			if ((Control.ModifierKeys & Keys.Control) != Keys.Control)
			{
				b2 = b(A_0);
			}
			if (b2 == null || (b2.a == TD.SandDock.j.c.a && this.m_b != null && this.m_g))
			{
				b2 = ((this.m_b == null || !this.m_g) ? new b(TD.SandDock.j.c.b) : new b(TD.SandDock.j.c.c));
			}
			if (b2.a == TD.SandDock.j.c.a)
			{
				b2.a = TD.SandDock.j.c.b;
			}
			if (b2.a == TD.SandDock.j.c.c)
			{
				ref Rectangle reference = ref b2.e;
				reference = new Rectangle(e(), this.m_f);
				if (b2.e.Y < l)
				{
					b2.e.Y = l;
				}
			}
			if (b2.c == this.m_d && this.m_e != null && b2.d == DockSide.None)
			{
				l();
				ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)this.m_d;
				if (b2.f != controlLayoutSystem.Controls.IndexOf(this.m_e) && b2.f != controlLayoutSystem.Controls.IndexOf(this.m_e) + 1)
				{
					controlLayoutSystem.Controls.SetChildIndex(this.m_e, b2.f);
				}
				b2.a = TD.SandDock.j.c.g;
			}
			else if (b2.a == TD.SandDock.j.c.b)
			{
				l();
			}
			else
			{
				a(b2.e, b2.a == TD.SandDock.j.c.e);
			}
			if (this.m_c is DocumentContainer)
			{
				if (b2.a == TD.SandDock.j.c.g)
				{
					Cursor.Current = Cursors.Default;
				}
				else if (b2.a == TD.SandDock.j.c.b)
				{
					Cursor.Current = this.m_k;
				}
				else
				{
					Cursor.Current = this.m_j;
				}
			}
			this.m_i = b2;
		}

		[SpecialName]
		public b j()
		{
			return this.m_i;
		}

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

		protected Rectangle a(DockContainer A_0, LayoutSystemBase A_1)
		{
			int num = 0;
			int num2 = 0;
			num = ((A_1.f.Width < A_1.f.Height) ? ((int)A_1.f.Width) : ((int)A_1.f.Height));
			num2 = ((!A_0.Empty) ? (A_0.ContentSize + num) : ((A_0.ContentSize <= 0) ? num : A_0.ContentSize));
			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.Left:
					result.Width += num2;
					break;
				case DockStyle.Right:
					result.Offset(-num2, 0);
					result.Width += num2;
					break;
				case DockStyle.Top:
					result.Height += num2;
					break;
				case DockStyle.Bottom:
					result.Offset(0, -num2);
					result.Height += num2;
					break;
				}
			}
			else
			{
				switch (A_0.Dock)
				{
				case DockStyle.Left:
					result.X = result.Right;
					result.Width = num;
					break;
				case DockStyle.Right:
					result.X -= num;
					result.Width = num;
					break;
				case DockStyle.Top:
					result.Y = result.Bottom;
					result.Height = num;
					break;
				case DockStyle.Bottom:
					result.Y -= num;
					result.Height = num;
					break;
				}
			}
			return result;
		}

		private ControlLayoutSystem[] d()
		{
			ArrayList arrayList = new ArrayList();
			if (this.m_b == null)
			{
				a(i(), arrayList);
			}
			else
			{
				foreach (DockContainer item in this.m_b.a)
				{
					if (item.IsFloating && ((m)item).l().Visible && (!(this.m_d is SplitLayoutSystem) || this.m_d.DockContainer != item))
					{
						a(item, arrayList);
					}
				}
				foreach (DockContainer item2 in this.m_b.a)
				{
					if (!item2.IsFloating && !item2.Empty)
					{
						a(item2, 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)
			{
				a(A_0, A_0.LayoutSystem, A_1);
			}
		}

		private void a(DockContainer A_0, SplitLayoutSystem A_1, ArrayList A_2)
		{
			foreach (LayoutSystemBase layoutSystem in A_1.LayoutSystems)
			{
				if (layoutSystem is SplitLayoutSystem)
				{
					a(A_0, (SplitLayoutSystem)layoutSystem, A_2);
				}
				else if (layoutSystem is ControlLayoutSystem && (this.m_e == null || layoutSystem != this.m_d || this.m_e.b.Controls.Count != 1) && !((ControlLayoutSystem)layoutSystem).Collapsed)
				{
					A_2.Add(layoutSystem);
				}
			}
		}

		protected virtual b b(Point A_0)
		{
			b b2 = null;
			Rectangle rectangle;
			if (this.m_b != null)
			{
				foreach (DockContainer item in this.m_b.a)
				{
					if (!item.IsFloating || !((m)item).l().Visible || !((m)item).HasSingleControlLayoutSystem || item.LayoutSystem == this.m_d)
					{
						continue;
					}
					rectangle = ((m)item).a();
					if (rectangle.Contains(A_0))
					{
						rectangle = new Rectangle(item.PointToScreen(item.LayoutSystem.LayoutSystems[0].Bounds.Location), item.LayoutSystem.LayoutSystems[0].Bounds.Size);
						if (!rectangle.Contains(A_0))
						{
							b2 = new b(TD.SandDock.j.c.e);
							b2.b = item;
							b2.c = (ControlLayoutSystem)item.LayoutSystem.LayoutSystems[0];
							b2.e = ((m)item).a();
							return b2;
						}
					}
				}
			}
			ControlLayoutSystem[] array = m;
			foreach (ControlLayoutSystem controlLayoutSystem in array)
			{
				rectangle = new Rectangle(controlLayoutSystem.DockContainer.PointToScreen(controlLayoutSystem.Bounds.Location), controlLayoutSystem.Bounds.Size);
				if (rectangle.Contains(A_0))
				{
					b2 = a(controlLayoutSystem.DockContainer, controlLayoutSystem, A_0, A_3: true);
					if (b2 != null)
					{
						return b2;
					}
				}
			}
			if (k() != null)
			{
				foreach (DockContainer item2 in this.m_b.a)
				{
					if (!item2.IsFloating && item2.Enabled && item2.Visible && (item2.Empty || this.m_e != null || item2 != this.m_c || !item2.HasSingleControlLayoutSystem) && a(item2).Contains(A_0))
					{
						b2 = new b(TD.SandDock.j.c.f);
						b2.b = item2;
						b2.e = a(item2, this.m_d);
						return b2;
					}
				}
			}
			return null;
		}

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

		private b a(DockContainer A_0, ControlLayoutSystem A_1, Point A_2)
		{
			b b2 = 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))
			{
				b2 = a(A_0, A_1);
				if (point.X < a_.Left + 30)
				{
					a(A_0, A_1, b2, a_, point);
				}
				else if (point.X > a_.Right - 30)
				{
					b(A_0, A_1, b2, a_, point);
				}
				else
				{
					a(A_0, A_1, b2, DockSide.Top);
				}
			}
			else if (new Rectangle(a_.Left, a_.Top, 30, a_.Height).Contains(point))
			{
				b2 = a(A_0, A_1);
				if (point.Y < a_.Top + 30)
				{
					a(A_0, A_1, b2, a_, point);
				}
				else if (point.Y > a_.Bottom - 30)
				{
					c(A_0, A_1, b2, a_, point);
				}
				else
				{
					a(A_0, A_1, b2, DockSide.Left);
				}
			}
			else if (new Rectangle(a_.Right - 30, a_.Top, 30, a_.Height).Contains(point))
			{
				b2 = a(A_0, A_1);
				if (point.Y < a_.Top + 30)
				{
					b(A_0, A_1, b2, a_, point);
				}
				else if (point.Y > a_.Bottom - 30)
				{
					d(A_0, A_1, b2, a_, point);
				}
				else
				{
					a(A_0, A_1, b2, DockSide.Right);
				}
			}
			else if (new Rectangle(a_.Left, a_.Bottom - 30, a_.Width, 30).Contains(point))
			{
				b2 = a(A_0, A_1);
				if (point.X < a_.Left + 30)
				{
					c(A_0, A_1, b2, a_, point);
				}
				else if (point.X > a_.Right - 30)
				{
					d(A_0, A_1, b2, a_, point);
				}
				else
				{
					a(A_0, A_1, b2, DockSide.Bottom);
				}
			}
			return b2;
		}

		private void d(DockContainer A_0, ControlLayoutSystem A_1, 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)))
			{
				a(A_0, A_1, A_2, DockSide.Bottom);
			}
			else
			{
				a(A_0, A_1, A_2, DockSide.Right);
			}
		}

		private void c(DockContainer A_0, ControlLayoutSystem A_1, 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)))
			{
				a(A_0, A_1, A_2, DockSide.Bottom);
			}
			else
			{
				a(A_0, A_1, A_2, DockSide.Left);
			}
		}

		private void b(DockContainer A_0, ControlLayoutSystem A_1, 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)))
			{
				a(A_0, A_1, A_2, DockSide.Right);
			}
			else
			{
				a(A_0, A_1, A_2, DockSide.Top);
			}
		}

		private void a(DockContainer A_0, ControlLayoutSystem A_1, 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)))
			{
				a(A_0, A_1, A_2, DockSide.Left);
			}
			else
			{
				a(A_0, A_1, A_2, DockSide.Top);
			}
		}

		private void a(DockContainer A_0, ControlLayoutSystem A_1, b A_2, DockSide A_3)
		{
			A_2.e = 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.Left:
				result.Width /= 2;
				break;
			case DockSide.Right:
				result.Offset(result.Width / 2, 0);
				result.Width /= 2;
				break;
			case DockSide.Bottom:
				result.Offset(0, result.Height / 2);
				result.Height /= 2;
				break;
			}
			return result;
		}

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

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

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

		public override void b()
		{
			this.m_c.OnDockingFinished(EventArgs.Empty);
			if (this.m_j != null)
			{
				this.m_j.Dispose();
			}
			if (this.m_k != null)
			{
				this.m_k.Dispose();
			}
			base.b();
		}

		void IDisposable.Dispose()
		{
			//ILSpy generated this explicit interface implementation from .override directive in b
			this.b();
		}
	}
}
