using System;
using System.IO;
using System.Linq;
using System.Drawing;
using Wisej.Web;

namespace WisejThemeTest.Views
{
	/// <summary>
	/// Control gallery to visually stress-test Wisej themes (Fluent + others).
	/// UI is built entirely in code so the project has no designer/DB dependencies.
	/// The theme list is auto-discovered from the /Themes folder, so any .theme
	/// file dropped in there becomes selectable without code changes.
	/// </summary>
	public class MainPage : Page
	{
		private readonly string[] Themes = DiscoverThemes();

		private Label _currentThemeLabel;

		/// <summary>
		/// Returns the file names (without extension) of every *.theme file in the
		/// physical /Themes folder, excluding mixin partials.
		/// </summary>
		private static string[] DiscoverThemes()
		{
			try
			{
				var dir = System.Web.Hosting.HostingEnvironment.MapPath("~/Themes");
				if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
					return new string[0];

				return Directory.GetFiles(dir, "*.theme")
					.Select(Path.GetFileNameWithoutExtension)
					.Where(n => !n.EndsWith(".mixin", StringComparison.OrdinalIgnoreCase))
					.OrderBy(n => n, StringComparer.OrdinalIgnoreCase)
					.ToArray();
			}
			catch
			{
				return new string[0];
			}
		}

		public MainPage()
		{
			Text = "Wisej Theme Test";
			Padding = new Padding(0);

			BuildToolbar();
			BuildGallery();
		}

		private void BuildToolbar()
		{
			var bar = new Panel
			{
				Dock = DockStyle.Top,
				Height = 52,
				Padding = new Padding(12, 8, 12, 8),
			};

			var label = new Label
			{
				Text = "Theme:",
				AutoSize = true,
				Location = new Point(12, 16),
			};

			var combo = new ComboBox
			{
				DropDownStyle = ComboBoxStyle.DropDownList,
				Location = new Point(70, 12),
				Width = 260,
			};
			combo.Items.AddRange(Themes);
			var current = Application.Theme?.Name;
			if (Array.IndexOf(Themes, current) >= 0)
				combo.SelectedItem = current;
			else if (Themes.Length > 0)
				combo.SelectedIndex = 0;
			combo.SelectedIndexChanged += (s, e) =>
			{
				var name = combo.SelectedItem as string;
				if (string.IsNullOrEmpty(name)) return;
				Application.LoadTheme(name);
				UpdateCurrentThemeLabel();
			};

			_currentThemeLabel = new Label
			{
				AutoSize = true,
				Location = new Point(350, 16),
				ForeColor = Color.Gray,
			};

			bar.Controls.Add(label);
			bar.Controls.Add(combo);
			bar.Controls.Add(_currentThemeLabel);
			Controls.Add(bar);

			UpdateCurrentThemeLabel();
		}

		private void UpdateCurrentThemeLabel()
		{
			_currentThemeLabel.Text = "active: " + (Application.Theme?.Name ?? "(none)");
		}

		private void BuildGallery()
		{
			var scroll = new FlowLayoutPanel
			{
				Dock = DockStyle.Fill,
				AutoScroll = true,
				FlowDirection = FlowDirection.LeftToRight,
				WrapContents = true,
				Padding = new Padding(12),
			};

			scroll.Controls.Add(BuildButtonsGroup());
			scroll.Controls.Add(BuildInputsGroup());
			scroll.Controls.Add(BuildSelectionGroup());
			scroll.Controls.Add(BuildRangeGroup());
			scroll.Controls.Add(BuildTabsGroup());
			scroll.Controls.Add(BuildListsGroup());
			scroll.Controls.Add(BuildGridGroup());
			scroll.Controls.Add(BuildDialogGroup());

			Controls.Add(scroll);
			// Fill must be added before Top in z-order for docking to layout correctly.
			scroll.BringToFront();
		}

		private static GroupBox NewGroup(string title)
		{
			return new GroupBox
			{
				Text = title,
				Width = 320,
				Height = 240,
				Margin = new Padding(8),
			};
		}

		private static FlowLayoutPanel NewStack()
		{
			return new FlowLayoutPanel
			{
				Dock = DockStyle.Fill,
				FlowDirection = FlowDirection.TopDown,
				WrapContents = false,
				AutoScroll = true,
				Padding = new Padding(10),
			};
		}

		private GroupBox BuildButtonsGroup()
		{
			var g = NewGroup("Buttons");
			var s = NewStack();

			var normal = new Button { Text = "Normal Button", Width = 180, Margin = new Padding(3) };
			var primary = new Button { Text = "Default / Primary", Width = 180, Margin = new Padding(3) };
			AcceptButton = primary;
			var disabled = new Button { Text = "Disabled", Width = 180, Enabled = false, Margin = new Padding(3) };
			var toggle = new Button { Text = "Toggle (click me)", Width = 180, Margin = new Padding(3) };
			toggle.Click += (snd, e) => toggle.Text = "Clicked " + DateTime.Now.ToString("HH:mm:ss");

			s.Controls.Add(normal);
			s.Controls.Add(primary);
			s.Controls.Add(disabled);
			s.Controls.Add(toggle);
			g.Controls.Add(s);
			return g;
		}

		private GroupBox BuildInputsGroup()
		{
			var g = NewGroup("Text inputs");
			var s = NewStack();

			s.Controls.Add(new TextBox { Text = "Single line", Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new TextBox { Text = "Read only", ReadOnly = true, Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new MaskedTextBox { Mask = "00/00/0000", Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new NumericUpDown { Value = 42, Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new DateTimePicker { Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new ComboBox { Width = 260, Margin = new Padding(3), DropDownStyle = ComboBoxStyle.DropDown });
			s.Controls.Add(new TextBox { Text = "Multiline\r\nrow 2\r\nrow 3", Multiline = true, Height = 60, Width = 260, Margin = new Padding(3) });

			g.Controls.Add(s);
			return g;
		}

		private GroupBox BuildSelectionGroup()
		{
			var g = NewGroup("Selection");
			var s = NewStack();

			s.Controls.Add(new CheckBox { Text = "Checked", Checked = true, Margin = new Padding(3) });
			s.Controls.Add(new CheckBox { Text = "Unchecked", Margin = new Padding(3) });
			s.Controls.Add(new CheckBox { Text = "Disabled", Enabled = false, Checked = true, Margin = new Padding(3) });
			s.Controls.Add(new RadioButton { Text = "Option A", Checked = true, Margin = new Padding(3) });
			s.Controls.Add(new RadioButton { Text = "Option B", Margin = new Padding(3) });
			s.Controls.Add(new LinkLabel { Text = "A LinkLabel", Margin = new Padding(3) });

			g.Controls.Add(s);
			return g;
		}

		private GroupBox BuildRangeGroup()
		{
			var g = NewGroup("Range / progress");
			var s = NewStack();

			s.Controls.Add(new ProgressBar { Value = 65, Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new ProgressBar { Value = 25, Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new TrackBar { Value = 4, Maximum = 10, Width = 260, Margin = new Padding(3) });
			s.Controls.Add(new TrackBar { Value = 7, Maximum = 10, TickStyle = TickStyle.Both, Width = 260, Margin = new Padding(3) });

			g.Controls.Add(s);
			return g;
		}

		private GroupBox BuildTabsGroup()
		{
			var g = NewGroup("Tabs");
			var tabs = new TabControl { Dock = DockStyle.Fill };

			var t1 = new TabPage { Text = "First" };
			t1.Controls.Add(new Label { Text = "Content of first tab", Location = new Point(12, 16), AutoSize = true });
			var t2 = new TabPage { Text = "Second" };
			t2.Controls.Add(new Button { Text = "Button on tab 2", Location = new Point(12, 16), Width = 160 });
			var t3 = new TabPage { Text = "Third" };

			tabs.TabPages.Add(t1);
			tabs.TabPages.Add(t2);
			tabs.TabPages.Add(t3);
			g.Controls.Add(tabs);
			return g;
		}

		private GroupBox BuildListsGroup()
		{
			var g = NewGroup("Lists / tree");
			var s = NewStack();

			var lb = new ListBox { Width = 130, Height = 90, Margin = new Padding(3) };
			lb.Items.AddRange(new object[] { "Apple", "Banana", "Cherry", "Date" });
			lb.SelectedIndex = 1;

			var tree = new TreeView { Width = 130, Height = 90, Margin = new Padding(3) };
			var root = tree.Nodes.Add("Root");
			root.Nodes.Add("Child 1");
			root.Nodes.Add("Child 2");
			root.Expand();

			var row = new FlowLayoutPanel { AutoSize = true, FlowDirection = FlowDirection.LeftToRight, Margin = new Padding(0) };
			row.Controls.Add(lb);
			row.Controls.Add(tree);
			s.Controls.Add(row);
			g.Controls.Add(s);
			return g;
		}

		private GroupBox BuildGridGroup()
		{
			var g = NewGroup("DataGridView");
			g.Width = 480;
			var grid = new DataGridView
			{
				Dock = DockStyle.Fill,
				AllowUserToAddRows = false,
				ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize,
			};
			grid.Columns.Add("col1", "Name");
			grid.Columns.Add("col2", "Role");
			grid.Columns.Add("col3", "Active");
			grid.Rows.Add("Alice", "Admin", "yes");
			grid.Rows.Add("Bob", "User", "no");
			grid.Rows.Add("Carol", "Manager", "yes");
			grid.Rows.Add("Dave", "Guest", "no");
			g.Controls.Add(grid);
			return g;
		}

		private GroupBox BuildDialogGroup()
		{
			var g = NewGroup("Dialogs / feedback");
			var s = NewStack();

			var msg = new Button { Text = "Show MessageBox", Width = 200, Margin = new Padding(3) };
			msg.Click += (snd, e) =>
				MessageBox.Show("Themed message box.", "Info", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

			var alert = new Button { Text = "Show Alert", Width = 200, Margin = new Padding(3) };
			alert.Click += (snd, e) => AlertBox.Show("Themed alert toast.", MessageBoxIcon.Information);

			s.Controls.Add(msg);
			s.Controls.Add(alert);
			g.Controls.Add(s);
			return g;
		}
	}
}
