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
	{
		private DesignerVerbCollection m_a = null;

		private DockControl m_b = null;

		public override DesignerVerbCollection Verbs => this.m_a;

		public override SelectionRules SelectionRules => SelectionRules.Visible;

		public DockControlDesigner()
		{
			this.m_a = new DesignerVerbCollection();
			this.m_a.Add(new DesignerVerb("Add Control", a));
			this.m_a.Add(new DesignerVerb("Insert Control (Top)", b));
			this.m_a.Add(new DesignerVerb("Insert Control (Bottom)", b));
			this.m_a.Add(new DesignerVerb("Insert Control (Left)", b));
			this.m_a.Add(new DesignerVerb("Insert Control (Right)", b));
		}

		private void b(object A_0, EventArgs A_1)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
			_ = (ISelectionService)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.m_b.b.CreateNewLayoutSystem((int)((LayoutSystemBase)this.m_b.b).f.Width, (int)((LayoutSystemBase)this.m_b.b).f.Height, new DockControl[1] { dockControl }, dockControl);
			DockSide side = DockSide.Top;
			switch (designerVerb.Text)
			{
			case "Insert Control (Bottom)":
				side = DockSide.Bottom;
				break;
			case "Insert Control (Left)":
				side = DockSide.Left;
				break;
			case "Insert Control (Right)":
				side = DockSide.Right;
				break;
			}
			componentChangeService.OnComponentChanging(this.m_b.b.DockContainer, TypeDescriptor.GetProperties(this.m_b.b.DockContainer)["LayoutSystem"]);
			this.m_b.b.SplitForLayoutSystem(layoutSystem, side);
			componentChangeService.OnComponentChanged(this.m_b.b.DockContainer, TypeDescriptor.GetProperties(this.m_b.b.DockContainer)["LayoutSystem"], null, null);
			designerTransaction.Commit();
		}

		private void a(object A_0, EventArgs A_1)
		{
			IComponentChangeService componentChangeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
			IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
			ISelectionService selectionService = (ISelectionService)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.m_b.b.DockContainer, TypeDescriptor.GetProperties(this.m_b.b.DockContainer)["LayoutSystem"]);
			this.m_b.b.Controls.Add(dockControl);
			this.m_b.b.SelectedControl = dockControl;
			componentChangeService.OnComponentChanged(this.m_b.b.DockContainer, TypeDescriptor.GetProperties(this.m_b.b.DockContainer)["LayoutSystem"], null, null);
			designerTransaction.Commit();
			selectionService.SetSelectedComponents(new object[1] { dockControl });
		}

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

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

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

		protected override void a(PaintEventArgs A_0)
		{
			OnPaintAdornments(A_0);
			if (this.m_b.BorderStyle == TD.SandDock.Rendering.BorderStyle.None)
			{
				using Pen pen = new Pen(SystemColors.ControlDark);
				pen.DashStyle = DashStyle.Dash;
				Rectangle clientRectangle = this.m_b.ClientRectangle;
				clientRectangle.Width--;
				clientRectangle.Height--;
				A_0.Graphics.DrawRectangle(pen, clientRectangle);
			}
			if (this.m_b.Controls.get_Count() == 0)
			{
				Font font = new Font("Tahoma", 6.75f);
				Rectangle clientRectangle2 = this.m_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.m_b.Invalidate();
		}
	}
}
