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

namespace TD.SandDock
{
	internal class DockControlDesigner : ParentControlDesigner
	{
		public DockControlDesigner()
		{
			this.a = new DesignerVerbCollection();
			this.a.Add(new DesignerVerb("Add Control", new EventHandler(this.a)));
			this.a.Add(new DesignerVerb("Insert Control (Top)", new EventHandler(this.b)));
			this.a.Add(new DesignerVerb("Insert Control (Bottom)", new EventHandler(this.b)));
			this.a.Add(new DesignerVerb("Insert Control (Left)", new EventHandler(this.b)));
			this.a.Add(new DesignerVerb("Insert Control (Right)", new EventHandler(this.b)));
		}

		private void b(object A_0, EventArgs A_1)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)this.GetService(typeof(IDesignerHost));
			ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
			DesignerVerb designerVerb = (DesignerVerb)A_0;
			DesignerTransaction designerTransaction = designerHost.CreateTransaction("Insert Dock Control");
			DockControl dockControl = (DockControl)designerHost.CreateComponent(typeof(DockControl));
			((ComponentDesigner)designerHost.GetDesigner(dockControl)).OnSetComponentDefaults();
			ControlLayoutSystem layoutSystem = this.b.b.CreateNewLayoutSystem((int)this.b.b.f.Width, (int)this.b.b.f.Height, new DockControl[]
			{
				dockControl
			}, dockControl);
			DockSide side = DockSide.Top;
			string text;
			if ((text = designerVerb.Text) != null)
			{
				text = string.IsInterned(text);
				if (text != "Insert Control (Bottom)")
				{
					if (text != "Insert Control (Left)")
					{
						if (text == "Insert Control (Right)")
						{
							side = DockSide.Right;
						}
					}
					else
					{
						side = DockSide.Left;
					}
				}
				else
				{
					side = DockSide.Bottom;
				}
			}
			componentChangeService.OnComponentChanging(this.b.b.DockContainer, TypeDescriptor.GetProperties(this.b.b.DockContainer)["LayoutSystem"]);
			this.b.b.SplitForLayoutSystem(layoutSystem, side);
			componentChangeService.OnComponentChanged(this.b.b.DockContainer, TypeDescriptor.GetProperties(this.b.b.DockContainer)["LayoutSystem"], null, null);
			designerTransaction.Commit();
		}

		private void a(object A_0, EventArgs A_1)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)this.GetService(typeof(IDesignerHost));
			ISelectionService selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
			DesignerTransaction designerTransaction = designerHost.CreateTransaction("Add Dock Control");
			DockControl dockControl = (DockControl)designerHost.CreateComponent(typeof(DockControl));
			((ComponentDesigner)designerHost.GetDesigner(dockControl)).OnSetComponentDefaults();
			componentChangeService.OnComponentChanging(this.b.b.DockContainer, TypeDescriptor.GetProperties(this.b.b.DockContainer)["LayoutSystem"]);
			this.b.b.Controls.Add(dockControl);
			this.b.b.SelectedControl = dockControl;
			componentChangeService.OnComponentChanged(this.b.b.DockContainer, TypeDescriptor.GetProperties(this.b.b.DockContainer)["LayoutSystem"], null, null);
			designerTransaction.Commit();
			selectionService.SetSelectedComponents(new object[]
			{
				dockControl
			});
		}

		public virtual void a(IComponent A_0)
		{
			base.Initialize(A_0);
			IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
			this.b = (DockControl)A_0;
			componentChangeService.ComponentRemoving += this.a;
			this.b.ControlAdded += this.a;
			this.b.ControlRemoved += this.a;
		}

		protected virtual void a(bool A_0)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
			this.b.ControlAdded -= this.a;
			this.b.ControlRemoved -= this.a;
			componentChangeService.ComponentRemoving -= this.a;
			base.Dispose(A_0);
		}

		public override DesignerVerbCollection Verbs
		{
			get
			{
				return this.a;
			}
		}

		public override SelectionRules SelectionRules
		{
			get
			{
				return SelectionRules.Visible;
			}
		}

		private void a(object A_0, ComponentEventArgs A_1)
		{
			if (A_1.Component == this.b && this.b.b != null && this.b.b.DockContainer != null)
			{
				IComponentChangeService componentChangeService = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
				ControlLayoutSystem controlLayoutSystem = this.b.b;
				DockContainer dockContainer = controlLayoutSystem.DockContainer;
				componentChangeService.OnComponentChanging(dockContainer, TypeDescriptor.GetProperties(dockContainer)["LayoutSystem"]);
				g.a(this.b);
				componentChangeService.OnComponentChanged(dockContainer, TypeDescriptor.GetProperties(dockContainer)["LayoutSystem"], null, null);
			}
		}

		protected virtual void a(PaintEventArgs A_0)
		{
			base.OnPaintAdornments(A_0);
			if (this.b.BorderStyle == TD.SandDock.Rendering.BorderStyle.None)
			{
				using (Pen pen = new Pen(SystemColors.ControlDark))
				{
					pen.DashStyle = DashStyle.Dash;
					Rectangle clientRectangle = this.b.ClientRectangle;
					clientRectangle.Width--;
					clientRectangle.Height--;
					A_0.Graphics.DrawRectangle(pen, clientRectangle);
				}
			}
			if (this.b.Controls.Count == 0)
			{
				Font font = new Font("Tahoma", 6.75f);
				Rectangle clientRectangle2 = this.b.ClientRectangle;
				clientRectangle2.Inflate(-10, -10);
				StringFormat stringFormat = new StringFormat(StringFormat.GenericDefault);
				stringFormat.Alignment = StringAlignment.Center;
				stringFormat.LineAlignment = StringAlignment.Center;
				A_0.Graphics.DrawString("To redock controls, click and drag their tabs or titlebars to other locations on your form.", font, SystemBrushes.ControlDarkDark, clientRectangle2, stringFormat);
				stringFormat.Dispose();
				font.Dispose();
			}
		}

		private void a(object A_0, ControlEventArgs A_1)
		{
			this.b.Invalidate();
		}

		private DesignerVerbCollection a = null;

		private DockControl b = null;
	}
}
