using System;
using System.Drawing;
using System.Windows.Forms;

namespace TD.SandDock.Rendering
{
	public class EverettRenderer : RendererBase
	{
		private static StringFormat m_a = null;

		private static StringFormat b = null;

		private Color c = SystemColors.InactiveCaption;

		private Color d = SystemColors.ActiveCaption;

		private Color e = SystemColors.Control;

		private Color f = SystemColors.AppWorkspace;

		private Color g = SystemColors.ControlText;

		private Color h = SystemColors.ControlLightLight;

		private Color i = SystemColors.ControlDarkDark;

		private Color j = SystemColors.ControlDark;

		private Color k = SystemColors.ControlDarkDark;

		private Color l;

		private SolidBrush m = null;

		private Pen n;

		private Pen o;

		private Pen p;

		private Pen q;

		private SolidBrush r;

		private StringFormat s;

		private BoxModel t = null;

		private BoxModel u = null;

		private BoxModel v = null;

		internal static StringFormat StandardStringFormat
		{
			get
			{
				if (EverettRenderer.m_a == null)
				{
					EverettRenderer.m_a = new StringFormat(StringFormat.GenericDefault);
					EverettRenderer.m_a.Alignment = StringAlignment.Near;
					EverettRenderer.m_a.LineAlignment = StringAlignment.Center;
					EverettRenderer.m_a.Trimming = StringTrimming.EllipsisCharacter;
					EverettRenderer.m_a.FormatFlags |= StringFormatFlags.NoWrap;
				}
				return EverettRenderer.m_a;
			}
		}

		internal static StringFormat StandardVerticalStringFormat
		{
			get
			{
				if (b == null)
				{
					b = new StringFormat(StringFormat.GenericDefault);
					b.Alignment = StringAlignment.Near;
					b.LineAlignment = StringAlignment.Center;
					b.Trimming = StringTrimming.EllipsisCharacter;
					b.FormatFlags |= StringFormatFlags.NoWrap;
					b.FormatFlags |= StringFormatFlags.DirectionVertical;
				}
				return b;
			}
		}

		public Color CollapsedTabOutlineColor
		{
			get
			{
				return j;
			}
			set
			{
				j = value;
				base.CustomColors = true;
			}
		}

		public Color BackgroundTabForeColor
		{
			get
			{
				return i;
			}
			set
			{
				i = value;
			}
		}

		public Color HighlightColor
		{
			get
			{
				return h;
			}
			set
			{
				h = value;
				base.CustomColors = true;
			}
		}

		public Color ShadowColor
		{
			get
			{
				return g;
			}
			set
			{
				g = value;
				base.CustomColors = true;
			}
		}

		public Color TabStripBackgroundColor => l;

		public Color DocumentContainerBackgroundColor
		{
			get
			{
				return f;
			}
			set
			{
				f = value;
				base.CustomColors = true;
			}
		}

		public Color BackgroundColor
		{
			get
			{
				return e;
			}
			set
			{
				e = value;
				base.CustomColors = true;
			}
		}

		public Color InactiveTitleBarColor
		{
			get
			{
				return c;
			}
			set
			{
				c = value;
				base.CustomColors = true;
			}
		}

		public Color ActiveTitleBarColor
		{
			get
			{
				return d;
			}
			set
			{
				d = value;
				base.CustomColors = true;
			}
		}

		protected internal override TabTextDisplayMode TabTextDisplay => TabTextDisplayMode.SelectedTab;

		protected internal override Size DocumentClientPadding => new Size(2, 2);

		protected internal override Size ControlClientPadding => new Size(0, 0);

		protected internal override BoxModel TitleBarMetrics
		{
			get
			{
				if (v == null)
				{
					v = new BoxModel(0, SystemInformation.ToolWindowCaptionHeight + 2, 0, 0, 0, 0, 0, 0, 0, 2);
				}
				return v;
			}
		}

		protected internal override BoxModel TabMetrics
		{
			get
			{
				if (u == null)
				{
					u = new BoxModel(0, 0, 0, 0, 0, 0, 0, 0, 1, 0);
				}
				return u;
			}
		}

		protected internal override BoxModel TabStripMetrics
		{
			get
			{
				if (t == null)
				{
					t = new BoxModel(0, Control.DefaultFont.Height + 9, 4, 0, 5, 1, 0, 1, 0, 0);
				}
				return t;
			}
		}

		internal virtual int DocumentTabPadding => 5;

		protected internal override int DocumentTabSize => Control.DefaultFont.Height + 6;

		protected internal override int DocumentTabStripSize => Control.DefaultFont.Height + 8;

		protected internal override int DocumentTabExtra => 0;

		protected override void CalculateBaseColors()
		{
			l = a(BackgroundColor);
		}

		private Color a(Color A_0)
		{
			byte b = A_0.R;
			byte b2 = A_0.G;
			byte b3 = A_0.B;
			byte b4 = Math.Max(Math.Max(b, b2), b3);
			if (b4 == 0)
			{
				return Color.FromArgb(35, 35, 35);
			}
			byte b5 = (byte)((b4 > 220) ? ((byte)(255 - b4)) : 35);
			b = (byte)(b + (byte)((double)((float)(int)b5 * ((float)(int)b / (float)(int)b4)) + 0.5));
			b2 = (byte)(b2 + (byte)((double)((float)(int)b5 * ((float)(int)b2 / (float)(int)b4)) + 0.5));
			b3 = (byte)(b3 + (byte)((double)((float)(int)b5 * ((float)(int)b3 / (float)(int)b4)) + 0.5));
			return Color.FromArgb(b, b2, b3);
		}

		protected internal override void DrawControlClientBackground(Graphics graphics, Rectangle bounds, Color backColor)
		{
		}

		protected internal override void DrawDocumentClientBackground(Graphics graphics, Rectangle bounds, Color backColor)
		{
			using SolidBrush brush = new SolidBrush(backColor);
			graphics.FillRectangle(brush, bounds);
		}

		protected internal override void DrawDocumentStripBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.FillRectangle(m, bounds);
			graphics.DrawLine(o, bounds.X, bounds.Bottom - 1, bounds.Right, bounds.Bottom - 1);
		}

		protected internal override void DrawDocumentContainerBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.Clear(f);
		}

		protected internal override Size MeasureDocumentStripTab(Graphics graphics, Image image, string text, Font font, DrawItemState state)
		{
			int num;
			if ((state & DrawItemState.Focus) == DrawItemState.Focus)
			{
				using Font font2 = new Font(font, FontStyle.Bold);
				num = (int)Math.Ceiling(graphics.MeasureString(text, font2, 999, StandardStringFormat).Width);
			}
			else
			{
				num = (int)Math.Ceiling(graphics.MeasureString(text, font, 999, StandardStringFormat).Width);
			}
			num += 2 + DocumentTabPadding * 2 + 2;
			if (image != null)
			{
				num += 20;
			}
			num += DocumentTabExtra;
			return new Size(num, 0);
		}

		protected internal override void DrawDocumentStripTab(Graphics graphics, Rectangle bounds, Image image, string text, Font font, Color backColor, Color foreColor, DrawItemState state, bool drawSeparator)
		{
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				using (SolidBrush brush = new SolidBrush(backColor))
				{
					graphics.FillRectangle(brush, bounds);
				}
				graphics.DrawLine(o, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 1);
				graphics.DrawLine(o, bounds.Left, bounds.Top, bounds.Right - 1, bounds.Top);
				graphics.DrawLine(n, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom - 1);
			}
			else if (drawSeparator)
			{
				graphics.DrawLine(SystemPens.ControlDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);
			}
			if (image != null)
			{
				graphics.DrawImage(image, bounds.X + 4, bounds.Y + 2, 16, 16);
				bounds.X += 20;
				bounds.Width -= 20;
			}
			if (bounds.Width <= 8)
			{
				return;
			}
			Font font2 = font;
			if ((state & DrawItemState.Focus) == DrawItemState.Focus)
			{
				font2 = new Font(font, FontStyle.Bold);
			}
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				using SolidBrush brush2 = new SolidBrush(foreColor);
				graphics.DrawString(text, font2, brush2, bounds, s);
			}
			else
			{
				graphics.DrawString(text, font2, r, bounds, s);
			}
			if ((state & DrawItemState.Focus) == DrawItemState.Focus)
			{
				font2.Dispose();
			}
		}

		protected internal override void StartRenderSession()
		{
			m = new SolidBrush(l);
			n = new Pen(g);
			p = new Pen(e);
			o = new Pen(h);
			r = new SolidBrush(i);
			q = new Pen(j);
			s = new StringFormat(StringFormat.GenericDefault);
			s.FormatFlags = StringFormatFlags.NoWrap;
			s.Alignment = StringAlignment.Center;
			s.LineAlignment = StringAlignment.Center;
		}

		protected internal override void DrawSplitter(Graphics graphics, Rectangle bounds, Orientation orientation)
		{
			graphics.FillRectangle(SystemBrushes.Control, bounds);
		}

		protected internal override void DrawDocumentStripButton(Graphics graphics, Rectangle bounds, SandDockButtonType buttonType, DrawItemState state)
		{
			DrawButtonHighlight(graphics, bounds, state);
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				bounds.Offset(1, 1);
			}
			switch (buttonType)
			{
			case SandDockButtonType.ScrollLeft:
				TD.SandDock.o.b(graphics, bounds, k, (state & DrawItemState.Disabled) != DrawItemState.Disabled);
				break;
			case SandDockButtonType.ScrollRight:
				TD.SandDock.o.a(graphics, bounds, k, (state & DrawItemState.Disabled) != DrawItemState.Disabled);
				break;
			case SandDockButtonType.Close:
			{
				using Pen a_ = new Pen(k);
				TD.SandDock.o.b(graphics, bounds, a_);
				break;
			}
			case SandDockButtonType.Pin:
				break;
			}
		}

		internal virtual void DrawButtonHighlight(Graphics graphics, Rectangle bounds, DrawItemState state)
		{
			if ((state & DrawItemState.HotLight) == DrawItemState.HotLight)
			{
				Pen pen;
				Pen pen2;
				if ((state & DrawItemState.Selected) == DrawItemState.Selected)
				{
					pen = n;
					pen2 = o;
				}
				else
				{
					pen2 = n;
					pen = o;
				}
				graphics.DrawLine(pen, bounds.Left, bounds.Top, bounds.Right - 1, bounds.Top);
				graphics.DrawLine(pen, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 1);
				graphics.DrawLine(pen2, bounds.Right - 1, bounds.Bottom - 1, bounds.Right - 1, bounds.Top);
				graphics.DrawLine(pen2, bounds.Right - 1, bounds.Bottom - 1, bounds.Left, bounds.Bottom - 1);
			}
		}

		protected internal override void DrawTitleBarButton(Graphics graphics, Rectangle bounds, SandDockButtonType buttonType, DrawItemState state, bool focused, bool toggled)
		{
			bounds.Width--;
			bounds.Height--;
			DrawButtonHighlight(graphics, bounds, state);
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				bounds.Offset(1, 1);
			}
			switch (buttonType)
			{
			case SandDockButtonType.Pin:
				TD.SandDock.o.a(graphics, bounds, focused ? SystemPens.ActiveCaptionText : SystemPens.ControlText, toggled);
				break;
			case SandDockButtonType.Close:
				TD.SandDock.o.a(graphics, bounds, focused ? SystemPens.ActiveCaptionText : SystemPens.ControlText);
				break;
			}
		}

		protected internal override void DrawDockContainerBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.Clear(e);
		}

		protected internal override void DrawTabStripBackground(Graphics graphics, Rectangle bounds, int selectedTabOffset)
		{
			graphics.FillRectangle(m, bounds);
			graphics.DrawLine(n, bounds.X, bounds.Y, bounds.Right, bounds.Y);
		}

		protected internal override void DrawTabStripTab(Graphics graphics, Rectangle bounds, Image image, string text, Font font, Color backColor, Color foreColor, DrawItemState state, bool drawSeparator)
		{
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				using (SolidBrush brush = new SolidBrush(backColor))
				{
					graphics.FillRectangle(brush, bounds);
				}
				graphics.DrawLine(o, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 1);
				graphics.DrawLine(n, bounds.Left, bounds.Bottom - 1, bounds.Right, bounds.Bottom - 1);
				graphics.DrawLine(n, bounds.Right, bounds.Top, bounds.Right, bounds.Bottom - 1);
			}
			else if (drawSeparator)
			{
				graphics.DrawLine(SystemPens.ControlDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);
			}
			if (bounds.Width >= 24)
			{
				graphics.DrawImage(image, new Rectangle(bounds.X + 4, bounds.Y + 2, image.Width, image.Height));
			}
			bounds.X += 23;
			bounds.Width -= 25;
			if (bounds.Width <= 8)
			{
				return;
			}
			if ((state & DrawItemState.Selected) == DrawItemState.Selected)
			{
				using (SolidBrush brush2 = new SolidBrush(foreColor))
				{
					graphics.DrawString(text, font, brush2, bounds, StandardStringFormat);
					return;
				}
			}
			graphics.DrawString(text, font, r, bounds, StandardStringFormat);
		}

		protected internal override void DrawCollapsedBackground(Graphics graphics, Rectangle bounds)
		{
			graphics.FillRectangle(m, bounds);
		}

		protected internal override void DrawCollapsedTab(Graphics graphics, Rectangle bounds, DockSide dockSide, Image image, string text, Font font, Color backColor, Color foreColor, DrawItemState state, bool vertical)
		{
			using (SolidBrush brush = new SolidBrush(backColor))
			{
				graphics.FillRectangle(brush, bounds);
			}
			if (dockSide != 0)
			{
				graphics.DrawLine(q, bounds.Left, bounds.Top, bounds.Right, bounds.Top);
			}
			if (dockSide != DockSide.Right)
			{
				graphics.DrawLine(q, bounds.Right, bounds.Top, bounds.Right, bounds.Bottom);
			}
			if (dockSide != DockSide.Bottom)
			{
				graphics.DrawLine(q, bounds.Left, bounds.Bottom, bounds.Right, bounds.Bottom);
			}
			if (dockSide != DockSide.Left)
			{
				graphics.DrawLine(q, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom);
			}
			bounds.Inflate(-2, -2);
			if (vertical)
			{
				bounds.Offset(0, 1);
			}
			else
			{
				bounds.Offset(1, 0);
			}
			graphics.DrawImage(image, new Rectangle(bounds.Left, bounds.Top, image.Width, image.Height));
			if (text.Length != 0)
			{
				if (vertical)
				{
					bounds.Offset(0, 23);
					graphics.DrawString(text, font, r, bounds, StandardVerticalStringFormat);
				}
				else
				{
					bounds.Offset(23, 0);
					graphics.DrawString(text, font, r, bounds, StandardStringFormat);
				}
			}
		}

		protected internal override void DrawTitleBarBackground(Graphics graphics, Rectangle bounds, bool focused)
		{
			if (focused)
			{
				graphics.FillRectangle(SystemBrushes.ActiveCaption, bounds);
				return;
			}
			graphics.FillRectangle(SystemBrushes.Control, bounds);
			graphics.DrawLine(SystemPens.ControlDark, bounds.X + 1, bounds.Y, bounds.Right - 2, bounds.Y);
			graphics.DrawLine(SystemPens.ControlDark, bounds.X + 1, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);
			graphics.DrawLine(SystemPens.ControlDark, bounds.X, bounds.Y + 1, bounds.X, bounds.Bottom - 2);
			graphics.DrawLine(SystemPens.ControlDark, bounds.Right - 1, bounds.Y + 1, bounds.Right - 1, bounds.Bottom - 2);
		}

		protected internal override void DrawTitleBarText(Graphics graphics, Rectangle bounds, bool focused, string text, Font font)
		{
			Brush brush = (focused ? SystemBrushes.ActiveCaptionText : SystemBrushes.ControlText);
			bounds.Inflate(-3, 0);
			graphics.DrawString(text, font, brush, bounds, StandardStringFormat);
		}

		protected internal override void FinishRenderSession()
		{
			m.Dispose();
			n.Dispose();
			p.Dispose();
			o.Dispose();
			r.Dispose();
			q.Dispose();
			s.Dispose();
		}

		public override string ToString()
		{
			return "Everett";
		}
	}
}
