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

namespace TD.SandDock
{
	public class DocumentLayoutSystem : ControlLayoutSystem
	{
		public DocumentLayoutSystem()
		{
			this.g = new n();
			this.h = new n();
			this.i = new n();
			this.e = new Timer();
			this.e.Interval = 20;
			this.e.Tick += this.a;
		}

		public DocumentLayoutSystem(int desiredWidth, int desiredHeight) : this()
		{
			this.f = new SizeF((float)desiredWidth, (float)desiredHeight);
		}

		public DocumentLayoutSystem(int desiredWidth, int desiredHeight, DockControl[] controls, DockControl selectedControl) : this(desiredWidth, desiredHeight)
		{
			base.Controls.AddRange(controls);
			if (selectedControl != null)
			{
				this.SelectedControl = selectedControl;
			}
		}

		internal override string GetToolTipText(Point position)
		{
			n buttonAt = this.GetButtonAt(position.X, position.Y);
			if (buttonAt == this.g)
			{
				return SandDockLanguage.ScrollLeftText;
			}
			if (buttonAt == this.h)
			{
				return SandDockLanguage.ScrollRightText;
			}
			if (buttonAt == this.i)
			{
				return SandDockLanguage.CloseText;
			}
			return base.GetToolTipText(position);
		}

		internal override void OnDockingManagerCommitted(j.b target)
		{
			base.OnDockingManagerCommitted(target);
			if ((target == null || target.a == TD.SandDock.j.c.b) && this.SelectedControl != null && base.IsInContainer)
			{
				Point a_ = this.SelectedControl.PointToClient(Cursor.Position);
				base.DockContainer.OnShowControlContextMenu(new ShowControlContextMenuEventArgs(this.SelectedControl, a_));
				return;
			}
		}

		internal override void OnButtonPress(n button)
		{
			if (button == this.g || button == this.h)
			{
				this.a();
			}
		}

		internal override void OnButtonPressed(n button)
		{
			if (button == this.i)
			{
				this.OnCloseButtonClick(new CancelEventArgs());
				return;
			}
			if (button == this.g || button == this.h)
			{
				this.b();
			}
		}

		internal override n GetButtonAt(int x, int y)
		{
			if (this.i.b && this.i.c && this.i.a.Contains(x, y))
			{
				return this.i;
			}
			if (this.g.b && this.g.c && this.g.a.Contains(x, y))
			{
				return this.g;
			}
			if (this.h.b && this.h.c && this.h.a.Contains(x, y))
			{
				return this.h;
			}
			return null;
		}

		internal override void InvalidateTitleBar()
		{
			if (base.DockContainer != null)
			{
				base.DockContainer.Invalidate(this.f);
			}
		}

		internal override void Paint(RendererBase renderer, Graphics graphics, Font font)
		{
			if (this.SelectedControl != null && !this.SelectedControl.p)
			{
				this.SelectedControl.OnBeforeFirstShown(EventArgs.Empty);
				this.SelectedControl.p = true;
			}
			renderer.DrawDocumentStripBackground(graphics, this.f);
			if (this.SelectedControl != null)
			{
				renderer.DrawDocumentClientBackground(graphics, this.g, this.SelectedControl.BackColor);
			}
			else
			{
				renderer.DrawDocumentClientBackground(graphics, this.g, SystemColors.Control);
			}
			Region clip = graphics.Clip;
			Rectangle clip2 = this.f;
			clip2.X += this.LeftPadding;
			clip2.Width -= this.LeftPadding;
			clip2.Width -= this.RightPadding;
			graphics.SetClip(clip2);
			for (int i = base.Controls.Count - 1; i >= 0; i--)
			{
				DockControl dockControl = base.Controls[i];
				this.a(renderer, graphics, dockControl.Font, dockControl);
			}
			if (this.SelectedControl != null)
			{
				this.a(renderer, graphics, this.SelectedControl.Font, this.SelectedControl);
			}
			graphics.Clip = clip;
			base.a(graphics, renderer, this.i, SandDockButtonType.Close, true);
			base.a(graphics, renderer, this.h, SandDockButtonType.ScrollRight, this.h.c);
			base.a(graphics, renderer, this.g, SandDockButtonType.ScrollLeft, this.g.c);
		}

		private new void a(RendererBase A_0, Graphics A_1, Font A_2, DockControl A_3)
		{
			DrawItemState drawItemState = DrawItemState.Default;
			if (this.SelectedControl == A_3)
			{
				drawItemState |= DrawItemState.Selected;
				if (base.DockContainer is DocumentContainer && ((DocumentContainer)base.DockContainer).ActiveDocument == A_3)
				{
					drawItemState |= DrawItemState.Focus;
				}
			}
			bool drawSeparator = true;
			if (this.SelectedControl != null && base.Controls.IndexOf(A_3) == base.Controls.IndexOf(this.SelectedControl) - 1)
			{
				drawSeparator = false;
			}
			if ((drawItemState & DrawItemState.Focus) == DrawItemState.Focus)
			{
				using (Font font = new Font(A_2, FontStyle.Bold))
				{
					A_0.DrawDocumentStripTab(A_1, A_3.c, A_3.TabImage, A_3.TabText, font, A_3.BackColor, A_3.ForeColor, drawItemState, drawSeparator);
					return;
				}
			}
			A_0.DrawDocumentStripTab(A_1, A_3.c, A_3.TabImage, A_3.TabText, A_2, A_3.BackColor, A_3.ForeColor, drawItemState, drawSeparator);
		}

		protected internal override ControlLayoutSystem CreateNewLayoutSystem(int desiredWidth, int desiredHeight, DockControl[] controls, DockControl selectedControl)
		{
			return new DocumentLayoutSystem(desiredWidth, desiredHeight, controls, selectedControl);
		}

		protected internal override ControlLayoutSystem CreateNewLayoutSystem()
		{
			return new DocumentLayoutSystem();
		}

		public override DockControl GetControlAt(Point position)
		{
			if (this.f.Contains(position) && (position.X < this.f.X + this.LeftPadding || position.X > this.f.Right - this.RightPadding))
			{
				return null;
			}
			return base.GetControlAt(position);
		}

		protected internal override void OnControlEnter(DockControl control)
		{
			base.OnControlEnter(control);
			if (base.DockContainer is DocumentContainer)
			{
				((DocumentContainer)base.DockContainer).c(control);
			}
		}

		public bool ScrollingEnabled
		{
			get
			{
				return this.f;
			}
			set
			{
				this.f = value;
				base.f();
			}
		}

		public override bool Collapsed
		{
			get
			{
				return false;
			}
			set
			{
			}
		}

		protected virtual int LeftPadding
		{
			get
			{
				return 0;
			}
		}

		protected virtual int RightPadding
		{
			get
			{
				if (this.g.b)
				{
					return base.Bounds.Right - this.g.a.Left;
				}
				if (this.i.b)
				{
					return base.Bounds.Right - this.i.a.Left;
				}
				return 0;
			}
		}

		protected override void CalculateLayout(RendererBase renderer, Rectangle bounds, bool floating, out Rectangle titlebarBounds, out Rectangle tabstripBounds, out Rectangle clientBounds, out Rectangle joinCatchmentBounds)
		{
			titlebarBounds = Rectangle.Empty;
			tabstripBounds = bounds;
			tabstripBounds.Height = renderer.DocumentTabStripSize;
			bounds.Offset(0, renderer.DocumentTabStripSize);
			bounds.Height -= renderer.DocumentTabStripSize;
			clientBounds = bounds;
			joinCatchmentBounds = tabstripBounds;
		}

		protected internal override void Layout(RendererBase renderer, Graphics graphics, Rectangle bounds, bool floating)
		{
			base.Layout(renderer, graphics, bounds, floating);
			this.b(renderer, graphics, this.f);
			this.a(renderer, graphics, this.f);
			bounds = this.g;
			Size documentClientPadding = renderer.DocumentClientPadding;
			bounds.Inflate(-documentClientPadding.Width, -documentClientPadding.Height);
			this.s = true;
			try
			{
				foreach (object obj in base.Controls)
				{
					DockControl dockControl = (DockControl)obj;
					if (dockControl.Parent != base.DockContainer)
					{
						if (dockControl.Parent != null)
						{
							TD.SandDock.g.a(dockControl);
						}
						base.DockContainer.Controls.Add(dockControl);
					}
					if (dockControl == this.SelectedControl)
					{
						dockControl.Visible = true;
						dockControl.Bounds = bounds;
					}
				}
				foreach (object obj2 in base.Controls)
				{
					DockControl dockControl2 = (DockControl)obj2;
					if (dockControl2 != this.SelectedControl)
					{
						dockControl2.Visible = false;
					}
				}
			}
			finally
			{
				this.s = false;
			}
		}

		private new void b(RendererBase A_0, Graphics A_1, Rectangle A_2)
		{
			int y = A_2.Top + A_2.Height / 2 - 7;
			int num = A_2.Right - 2;
			if (this.SelectedControl != null && this.SelectedControl.Closable)
			{
				this.i.b = true;
				this.i.a = new Rectangle(num - 14, y, 14, 15);
				num -= 15;
			}
			else
			{
				this.i.b = false;
			}
			if (this.f)
			{
				this.h.b = true;
				this.h.a = new Rectangle(num - 14, y, 14, 15);
				num -= 15;
				this.g.b = true;
				this.g.a = new Rectangle(num - 14, y, 14, 15);
				num -= 15;
				return;
			}
			this.h.b = false;
			this.g.b = false;
		}

		private new void a(RendererBase A_0, Graphics A_1, Rectangle A_2)
		{
			int num = 3;
			foreach (object obj in base.Controls)
			{
				DockControl dockControl = (DockControl)obj;
				DrawItemState drawItemState = DrawItemState.Default;
				if (this.SelectedControl == dockControl)
				{
					drawItemState |= DrawItemState.Selected;
					if (base.DockContainer is DocumentContainer && ((DocumentContainer)base.DockContainer).ActiveDocument == dockControl)
					{
						drawItemState |= DrawItemState.Focus;
					}
				}
				int width = A_0.MeasureDocumentStripTab(A_1, dockControl.TabImage, dockControl.TabText, dockControl.Font, drawItemState).Width;
				dockControl.h = false;
				dockControl.c = new Rectangle(num, A_2.Bottom - A_0.DocumentTabSize, width, A_0.DocumentTabSize);
				num += width - A_0.DocumentTabExtra + 1;
			}
			if (base.Controls.Count != 0)
			{
				num += A_0.DocumentTabExtra;
			}
			num += 3;
			int num2 = A_2.Width - this.LeftPadding - this.RightPadding;
			this.d = num - num2;
			if (this.d < 0)
			{
				this.d = 0;
			}
			if (this.c > this.d)
			{
				this.c = this.d;
			}
			this.g.c = (this.c > 0);
			this.h.c = (this.c < this.d);
			foreach (object obj2 in base.Controls)
			{
				DockControl dockControl2 = (DockControl)obj2;
				Rectangle rectangle = dockControl2.c;
				rectangle.Offset(A_2.Left + this.LeftPadding - this.c, 0);
				dockControl2.c = rectangle;
			}
		}

		public override DockControl SelectedControl
		{
			get
			{
				return base.SelectedControl;
			}
			set
			{
				base.SelectedControl = value;
				if (value != null)
				{
					this.a(value);
				}
			}
		}

		private new void a(DockControl A_0)
		{
			if (this.d > 0)
			{
				Rectangle rectangle = A_0.c;
				int num = this.f.Right - this.RightPadding;
				int num2 = this.f.Left + this.LeftPadding;
				int num3 = num - num2;
				int num4 = 0;
				if (rectangle.Right > num)
				{
					num4 = rectangle.Right - num3 + 30;
				}
				if (rectangle.Left < num2)
				{
					num4 = rectangle.Left - num2 - 30;
				}
				if (num4 != 0)
				{
					this.a(num4);
				}
			}
		}

		private new void b()
		{
			this.e.Enabled = false;
			this.o = null;
			this.p = false;
			this.InvalidateTitleBar();
		}

		private new void a()
		{
			this.e.Enabled = true;
			this.a(this.e, EventArgs.Empty);
		}

		private new void a(int A_0)
		{
			this.c += A_0;
			if (this.c > this.d)
			{
				this.c = this.d;
				this.b();
			}
			if (this.c < 0)
			{
				this.c = 0;
				this.b();
			}
			base.f();
		}

		private new void a(object A_0, EventArgs A_1)
		{
			if (this.o == this.g)
			{
				this.a(-15);
				return;
			}
			if (this.o == this.h)
			{
				this.a(15);
				return;
			}
			this.b();
		}

		private new const int a = 14;

		private new const int b = 15;

		private new int c = 0;

		private int d = 0;

		private new Timer e = null;

		private new bool f = true;

		internal new n g = null;

		internal new n h = null;

		private new n i = null;

		private Point j = Point.Empty;
	}
}
