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

namespace TD.SandDock
{
	internal class k : IDisposable
	{
		private DockContainer m_a = null;

		private Timer m_b = null;

		private Timer m_c = null;

		private r m_d = null;

		private ControlLayoutSystem e = null;

		private Rectangle f = Rectangle.Empty;

		public k(DockContainer A_0)
		{
			this.m_a = A_0;
			this.m_b = new Timer();
			this.m_b.Interval = 500;
			this.m_b.Tick += b;
			this.m_c = new Timer();
			this.m_c.Interval = 800;
			this.m_c.Tick += c;
			this.m_d = new r(A_0, this);
		}

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

		public void b()
		{
			a(A_0: true);
		}

		private void a(Rectangle A_0, Rectangle A_1)
		{
			float num = A_1.X - A_0.X;
			float num2 = A_1.Y - A_0.Y;
			float num3 = A_1.Width - A_0.Width;
			float num4 = 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.m_d.SetBounds(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, BoundsSpecified.All);
				Application.DoEvents();
				if (this.m_d == null)
				{
					break;
				}
			}
		}

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

		internal void a(bool A_0)
		{
			if (e != null)
			{
				this.m_c.Enabled = false;
				if (!A_0)
				{
					a(e.PopupSize, out var A_);
					a(this.m_d.Bounds, A_);
				}
				this.m_d.a(null);
				if (this.m_d.Parent != null)
				{
					this.m_a.Parent.Resize -= a;
					this.m_d.Parent.Controls.Remove(this.m_d);
				}
				e = null;
			}
		}

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

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

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

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

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

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

		public void b(MouseEventArgs A_0)
		{
		}

		public void a(MouseEventArgs A_0)
		{
		}

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

		private DockControl a(Point A_0)
		{
			foreach (LayoutSystemBase item in this.m_a.c)
			{
				if (!(item is ControlLayoutSystem))
				{
					continue;
				}
				ControlLayoutSystem controlLayoutSystem = (ControlLayoutSystem)item;
				if (!controlLayoutSystem.Collapsed)
				{
					continue;
				}
				foreach (DockControl control in controlLayoutSystem.Controls)
				{
					Rectangle rectangle = control.c;
					if (rectangle.Contains(A_0))
					{
						return control;
					}
				}
			}
			return null;
		}

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

		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 (DockControl control in A_2.Controls)
				{
					int num2 = ((!this.m_a.Vertical) ? ((int)Math.Ceiling(A_1.MeasureString(control.TabText, this.m_a.Font, 999, EverettRenderer.StandardStringFormat).Width)) : ((int)Math.Ceiling(A_1.MeasureString(control.TabText, this.m_a.Font, 999, EverettRenderer.StandardVerticalStringFormat).Height)));
					if (num2 > num)
					{
						num = num2;
					}
				}
			}
			foreach (DockControl control2 in A_2.Controls)
			{
				Rectangle rectangle = new Rectangle(A_3.Left - 1, A_3.Top - 1, a() - 2, a() - 2);
				switch (this.m_a.Dock)
				{
				case DockStyle.Right:
					rectangle.Offset(3, 0);
					break;
				case DockStyle.Bottom:
					rectangle.Offset(0, 3);
					break;
				}
				int num3 = 23;
				if (A_0.TabTextDisplay == TabTextDisplayMode.AllTabs)
				{
					num3 = ((!this.m_a.Vertical) ? (num3 + (int)Math.Ceiling(A_1.MeasureString(control2.TabText, this.m_a.Font, 999, EverettRenderer.StandardStringFormat).Width)) : (num3 + (int)Math.Ceiling(A_1.MeasureString(control2.TabText, this.m_a.Font, 999, EverettRenderer.StandardVerticalStringFormat).Height)));
					num3 += 2;
				}
				else if (A_2.SelectedControl == control2)
				{
					num3 += num + 16;
				}
				if (this.m_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;
				}
				control2.c = rectangle;
			}
			A_4 += 10;
		}

		public void c()
		{
			a(A_0: true);
			this.m_b.Dispose();
			this.m_b = null;
			this.m_d.Dispose();
			this.m_d = null;
		}

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

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