using System;
using System.Drawing;
using System.Windows.Forms;
using TD.SandDock.Rendering;

namespace TD.SandDock
{
	internal class k : IDisposable
	{
		public k(DockContainer A_0)
		{
			this.a = A_0;
			this.b = new Timer();
			this.b.Interval = 500;
			this.b.Tick += this.b;
			this.c = new Timer();
			this.c.Interval = 800;
			this.c.Tick += this.c;
			this.d = new r(A_0, this);
		}

		internal static int a()
		{
			return Control.DefaultFont.Height + 9;
		}

		public void b()
		{
			this.a(true);
		}

		private void a(Rectangle A_0, Rectangle A_1)
		{
			float num = (float)(A_1.X - A_0.X);
			float num2 = (float)(A_1.Y - A_0.Y);
			float num3 = (float)(A_1.Width - A_0.Width);
			float num4 = (float)(A_1.Height - A_0.Height);
			int tickCount = Environment.TickCount;
			while (Environment.TickCount < tickCount + 100)
			{
				float num5 = (float)(Environment.TickCount - tickCount) / 100f;
				float num6 = (float)A_0.X + num * num5;
				float num7 = (float)A_0.Y + num2 * num5;
				float num8 = (float)A_0.Width + num3 * num5;
				float num9 = (float)A_0.Height + num4 * num5;
				Rectangle rectangle = new Rectangle((int)num6, (int)num7, (int)num8, (int)num9);
				this.d.SetBounds(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, BoundsSpecified.All);
				Application.DoEvents();
				if (this.d == null)
				{
					return;
				}
			}
		}

		internal void a(DockControl A_0, bool A_1, bool A_2)
		{
			try
			{
				if (this.e != A_0.b || A_0.b.SelectedControl != A_0)
				{
					A_0.b.SelectedControl = A_0;
					this.a(true);
					Rectangle rectangle;
					this.f = this.a(A_0.b.PopupSize, out rectangle);
					this.d.a(A_0.b);
					this.d.Visible = false;
					this.a.Parent.Controls.Add(this.d);
					this.d.Bounds = rectangle;
					this.d.Visible = true;
					this.d.BringToFront();
					if (!A_1)
					{
						this.a(rectangle, this.f);
					}
					if (this.d != null)
					{
						this.d.Bounds = this.f;
						this.e = A_0.b;
						this.c.Enabled = true;
						this.a.Parent.Resize += this.a;
					}
				}
			}
			finally
			{
				if (A_2)
				{
					A_0.Activate();
				}
			}
		}

		internal void a(bool A_0)
		{
			if (this.e == null)
			{
				return;
			}
			this.c.Enabled = false;
			if (!A_0)
			{
				Rectangle a_;
				this.a(this.e.PopupSize, out a_);
				this.a(this.d.Bounds, a_);
			}
			this.d.a(null);
			if (this.d.Parent != null)
			{
				this.a.Parent.Resize -= this.a;
				this.d.Parent.Controls.Remove(this.d);
			}
			this.e = null;
		}

		private Rectangle a(int A_0, out Rectangle A_1)
		{
			Rectangle bounds = this.a.Bounds;
			if (this.a.AutoHideVisible)
			{
				switch (this.a.Dock)
				{
				case DockStyle.Top:
					bounds = new Rectangle(bounds.X, bounds.Y + k.a(), bounds.Width, 0);
					break;
				case DockStyle.Bottom:
					bounds = new Rectangle(bounds.X, bounds.Bottom - k.a(), bounds.Width, 0);
					break;
				case DockStyle.Left:
					bounds = new Rectangle(bounds.X + k.a(), bounds.Y, 0, bounds.Height);
					break;
				case DockStyle.Right:
					bounds = new Rectangle(bounds.Right - k.a(), bounds.Y, 0, bounds.Height);
					break;
				}
			}
			A_1 = bounds;
			int num = A_0 + 4;
			switch (this.a.Dock)
			{
			case DockStyle.Top:
				bounds.Height = num;
				break;
			case DockStyle.Bottom:
				bounds.Offset(0, -num);
				bounds.Height = num;
				break;
			case DockStyle.Left:
				bounds.Width = num;
				break;
			case DockStyle.Right:
				bounds.Offset(-num, 0);
				bounds.Width = num;
				break;
			}
			return bounds;
		}

		private void c(object A_0, EventArgs A_1)
		{
			bool flag = this.d.ClientRectangle.Contains(this.d.PointToClient(Cursor.Position));
			bool flag2 = this.a.AutoHideBounds.Contains(this.a.PointToClient(Cursor.Position));
			if (!flag && !flag2 && !this.d.c() && !this.d.ContainsFocus)
			{
				this.a(false);
			}
		}

		private void b(object A_0, EventArgs A_1)
		{
			this.b.Enabled = false;
			DockControl dockControl = this.a(this.a.PointToClient(Cursor.Position));
			if (dockControl != null)
			{
				this.a(dockControl, false, false);
			}
		}

		public void d(MouseEventArgs A_0)
		{
			this.b.Enabled = false;
			this.b.Enabled = true;
		}

		public void b(Point A_0)
		{
			DockControl dockControl = this.a(A_0);
			if (dockControl != null)
			{
				this.a(dockControl, true, false);
			}
		}

		public void c(MouseEventArgs A_0)
		{
			DockControl dockControl = this.a(new Point(A_0.X, A_0.Y));
			if (dockControl != null)
			{
				this.a(dockControl, false, true);
			}
		}

		public void b(MouseEventArgs A_0)
		{
		}

		public void a(MouseEventArgs A_0)
		{
		}

		public void a(Graphics A_0, Rectangle A_1)
		{
			this.a.WorkingRenderer.DrawCollapsedBackground(A_0, A_1);
			DockSide dockSide = DockSide.Right;
			switch (this.a.Dock)
			{
			case DockStyle.Top:
				dockSide = DockSide.Top;
				break;
			case DockStyle.Bottom:
				dockSide = DockSide.Bottom;
				break;
			case DockStyle.Left:
				dockSide = DockSide.Left;
				break;
			}
			foreach (object obj in this.a.c)
			{
				LayoutSystemBase layoutSystemBase = (LayoutSystemBase)obj;
				if (layoutSystemBase is ControlLayoutSystem && ((ControlLayoutSystem)layoutSystemBase).Collapsed)
				{
					ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)layoutSystemBase;
					foreach (object obj2 in controlLayoutSystem.Controls)
					{
						DockControl dockControl = (DockControl)obj2;
						DrawItemState drawItemState = DrawItemState.Default;
						if (dockControl == controlLayoutSystem.SelectedControl)
						{
							drawItemState |= DrawItemState.Selected;
						}
						string text = dockControl.TabText;
						if (this.a.WorkingRenderer.TabTextDisplay == TabTextDisplayMode.SelectedTab && dockControl != controlLayoutSystem.SelectedControl)
						{
							text = "";
						}
						this.a.WorkingRenderer.DrawCollapsedTab(A_0, dockControl.c, dockSide, dockControl.WorkingTabImage, text, this.a.Font, dockControl.BackColor, dockControl.ForeColor, drawItemState, this.a.Vertical);
					}
				}
			}
		}

		private DockControl a(Point A_0)
		{
			foreach (object obj in this.a.c)
			{
				LayoutSystemBase layoutSystemBase = (LayoutSystemBase)obj;
				if (layoutSystemBase is ControlLayoutSystem)
				{
					ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)layoutSystemBase;
					if (controlLayoutSystem.Collapsed)
					{
						foreach (object obj2 in controlLayoutSystem.Controls)
						{
							DockControl dockControl = (DockControl)obj2;
							Rectangle rectangle = dockControl.c;
							if (rectangle.Contains(A_0))
							{
								return dockControl;
							}
						}
					}
				}
			}
			return null;
		}

		public void a(RendererBase A_0, Graphics A_1, Rectangle A_2)
		{
			int num = 0;
			foreach (object obj in this.a.c)
			{
				LayoutSystemBase layoutSystemBase = (LayoutSystemBase)obj;
				if (layoutSystemBase is ControlLayoutSystem && ((ControlLayoutSystem)layoutSystemBase).Collapsed)
				{
					ControlLayoutSystem a_ = (ControlLayoutSystem)layoutSystemBase;
					this.a(A_0, A_1, a_, A_2, ref num);
				}
			}
		}

		private void a(RendererBase A_0, Graphics A_1, ControlLayoutSystem A_2, Rectangle A_3, ref int A_4)
		{
			A_4 += 3;
			int num = 0;
			if (A_0.TabTextDisplay == TabTextDisplayMode.SelectedTab)
			{
				foreach (object obj in A_2.Controls)
				{
					DockControl dockControl = (DockControl)obj;
					int num2;
					if (this.a.Vertical)
					{
						num2 = (int)Math.Ceiling((double)A_1.MeasureString(dockControl.TabText, this.a.Font, 999, EverettRenderer.StandardVerticalStringFormat).Height);
					}
					else
					{
						num2 = (int)Math.Ceiling((double)A_1.MeasureString(dockControl.TabText, this.a.Font, 999, EverettRenderer.StandardStringFormat).Width);
					}
					if (num2 > num)
					{
						num = num2;
					}
				}
			}
			foreach (object obj2 in A_2.Controls)
			{
				DockControl dockControl2 = (DockControl)obj2;
				Rectangle rectangle = new Rectangle(A_3.Left - 1, A_3.Top - 1, k.a() - 2, k.a() - 2);
				switch (this.a.Dock)
				{
				case DockStyle.Bottom:
					rectangle.Offset(0, 3);
					break;
				case DockStyle.Right:
					rectangle.Offset(3, 0);
					break;
				}
				int num3 = 23;
				if (A_0.TabTextDisplay == TabTextDisplayMode.AllTabs)
				{
					if (this.a.Vertical)
					{
						num3 += (int)Math.Ceiling((double)A_1.MeasureString(dockControl2.TabText, this.a.Font, 999, EverettRenderer.StandardVerticalStringFormat).Height);
					}
					else
					{
						num3 += (int)Math.Ceiling((double)A_1.MeasureString(dockControl2.TabText, this.a.Font, 999, EverettRenderer.StandardStringFormat).Width);
					}
					num3 += 2;
				}
				else if (A_2.SelectedControl == dockControl2)
				{
					num3 += num + 16;
				}
				if (this.a.Vertical)
				{
					rectangle.Offset(0, A_4);
					rectangle.Height = num3;
					A_4 += num3;
				}
				else
				{
					rectangle.Offset(A_4, 0);
					rectangle.Width = num3;
					A_4 += num3;
				}
				dockControl2.c = rectangle;
			}
			A_4 += 10;
		}

		public void c()
		{
			this.a(true);
			this.b.Dispose();
			this.b = null;
			this.d.Dispose();
			this.d = null;
		}

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

		private DockContainer a = null;

		private Timer b = null;

		private Timer c = null;

		private r d = null;

		private ControlLayoutSystem e = null;

		private Rectangle f = Rectangle.Empty;
	}
}
