using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace friction
{
	public static class Theme
	{
		public static Color Backcolor = Color.FromArgb(255, 45, 45, 48);
		public static Color BackColorTrans = Color.FromArgb(230, 45, 45, 48);
		public static Color Forecolor = Color.FromArgb(255, 235, 235, 235);

		public static Color Green = Color.LimeGreen;
		public static Color Yellow = Color.FromArgb(255, 255, 165, 0);
		public static Color Red = Color.OrangeRed;
		public static Color Gray = Color.DarkGray;
		public static Color White = Color.Snow;

		public static void Apply(Control ctrl)
		{
			ctrl.BackColor = Backcolor;
			ctrl.ForeColor = Forecolor;
		}

		public static void Apply(DataGridView ctrl)
		{
			ctrl.BackgroundColor = Backcolor;
			ctrl.DefaultCellStyle.BackColor = Backcolor;
			ctrl.DefaultCellStyle.ForeColor = Forecolor;
			ctrl.EnableHeadersVisualStyles = false;
			ctrl.ColumnHeadersDefaultCellStyle.BackColor = Backcolor;
			ctrl.ColumnHeadersDefaultCellStyle.ForeColor = Forecolor;
			ctrl.RowHeadersDefaultCellStyle.BackColor = Backcolor;
			ctrl.RowHeadersDefaultCellStyle.ForeColor = Forecolor;

			typeof(DataGridView).InvokeMember("DoubleBuffered",
				BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty,
				null, ctrl, new object[] { true });
		}

		public static void Apply(ListView ctrl)
		{
			ctrl.BackColor = Backcolor;
			ctrl.ForeColor = Forecolor;
			ctrl.OwnerDraw = true;
			ctrl.DrawColumnHeader +=
				new DrawListViewColumnHeaderEventHandler
				(
					(sender, e) => ListViewHeaderDraw(sender, e, Backcolor, Forecolor)
				);
			ctrl.DrawItem += new DrawListViewItemEventHandler(ListViewBodyDraw);

			typeof(ListView).InvokeMember("DoubleBuffered",
				BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty,
				null, ctrl, new object[] { true });
		}

		private static void ListViewHeaderDraw(object sender, DrawListViewColumnHeaderEventArgs e, Color backColor, Color foreColor)
		{
			e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds);
			e.Graphics.DrawString(e.Header.Text, e.Font, new SolidBrush(foreColor), e.Bounds);
		}
		private static void ListViewBodyDraw(object sender, DrawListViewItemEventArgs e)
		{
			e.DrawDefault = true;
		}

		public static void ResizeFullColumn(ListView view)
		{
			bool bScrollbarPresented = false;
			foreach (var scroll in view.Controls.OfType<VScrollBar>())
			{
				bScrollbarPresented = true;
				break;
			}

			int iWidth = view.Width;
			if (bScrollbarPresented == true)
				iWidth -= SystemInformation.VerticalScrollBarWidth;
			switch (view.BorderStyle)
			{
				case BorderStyle.Fixed3D:
					iWidth -= 4;
					break;
				case BorderStyle.FixedSingle:
					iWidth -= 2;
					break;
			}

			for (int i = 0; i < view.Columns.Count; i++)
			{
				if (view.Columns[i].Width < iWidth)
				{
					iWidth -= view.Columns[i].Width;
				}
				else
				{
					view.Columns[i].Width = iWidth;
					// Hide columns that can't fit 
					for (int jx = i + 1; jx < view.Columns.Count; ++jx)
						view.Columns[jx].Width = 0;
					return;
				}
			}

			view.Columns[view.Columns.Count - 1].Width += iWidth;
		}

		public class MenustripColor : ProfessionalColorTable
		{
			public override Color MenuItemSelected { get { return BackColorTrans; } }
			public override Color MenuItemSelectedGradientBegin { get { return BackColorTrans; } }
			public override Color MenuItemSelectedGradientEnd { get { return BackColorTrans; } }
			public override Color MenuItemPressedGradientBegin { get { return Backcolor; } }
			public override Color MenuItemPressedGradientEnd { get { return Backcolor; } }

			public override Color MenuItemBorder { get { return BackColorTrans; } }
			public override Color MenuBorder { get { return Backcolor; } }
		}

		class MenustripRenderer : ToolStripProfessionalRenderer
		{
			public MenustripRenderer() : base(new MenustripColor())
			{

			}

			protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
			{
				if (e.Item.Enabled)
					base.OnRenderMenuItemBackground(e);
				else
				{
					e.Item.BackColor = Backcolor;
					base.OnRenderMenuItemBackground(e);

					if (e.Item.Selected == true)
					{
						e.Item.ImageTransparentColor = Backcolor;
						base.OnRenderMenuItemBackground(e);
					}
				}
			}

			protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
			{
				if (e.Item.Enabled)
					base.OnRenderMenuItemBackground(e);
			}
		}


		public static void Apply(MenuStrip ctrl)
		{
			ctrl.BackColor = Backcolor;
			ctrl.ForeColor = Forecolor;

			foreach (ToolStripMenuItem item in ctrl.Items)
			{
				foreach (ToolStripMenuItem subitem in item.DropDownItems)
				{
					subitem.BackColor = Backcolor;
					subitem.ForeColor = Forecolor;

					subitem.MouseEnter += Ctrl_MouseEnter;
					subitem.MouseLeave += Ctrl_MouseLeave;
				}

				item.BackColor = Backcolor;
				item.ForeColor = Forecolor;

				item.MouseEnter += Ctrl_MouseEnter;
				item.MouseLeave += Ctrl_MouseLeave;
			}

			ctrl.Renderer = new MenustripRenderer();
		}

		private static void Ctrl_MouseEnter(object sender, EventArgs e)
		{
			if(sender is ToolStripMenuItem)
			{
				((ToolStripMenuItem)sender).ForeColor = Color.Orange;
			}
		}

		private static void Ctrl_MouseLeave(object sender, EventArgs e)
		{
			if (sender is ToolStripMenuItem)
			{
				((ToolStripMenuItem)sender).ForeColor = Forecolor;
			}
		}
	}
}
