using System;
using System.Windows.Forms;

namespace ExtensionMethods
{
	/// <summary>
	/// <para>Add extensions to <c><see cref="Control"/></c></para>
	/// </summary>
	public static class ControlExtensions
	{
		/// <summary>
		/// <para>Reset the value of all <see cref="Control"/>s in the given <paramref name="Control"/></para>
		/// <para>Empty <see cref="TextBox"/>es, set <see cref="ComboBox"/>es to N/A, uncheck <see cref="CheckBox"/>es.</para>
		/// </summary>
		/// <param name="Control">The <see cref="Control"/> whose children <see cref="Control"/>s shall be reset</param>
		public static void clearControls(this Control Control)
		{
			foreach (Control ctrl in Control.Controls)
			{
				ctrl.BackColor = System.Drawing.Color.White;

				switch (ctrl.GetType().Name)
				{
					case "TextBox":
					case "RichTextBox":
						((TextBox)ctrl).Text = String.Empty;
						((TextBox)ctrl).BackColor = System.Drawing.Color.White;
						break;
					case "ComboBox":
						((ComboBox)ctrl).Text = String.Empty;
						break;
					case "CheckBox":
						((CheckBox)ctrl).Checked = false;
						break;
					case "Control":
					case "Panel":
					case "SplitContainer":
					case "SplitterPanel":
					case "TabPage":
						ctrl.clearControls();
						break;
					case "CheckedListBox":
						foreach (object i in ((CheckedListBox)ctrl).CheckedIndices)
							((CheckedListBox)ctrl).SetItemChecked((int)i, false);
						break;
#if DEBUG
					case "ProgressBar":
					case "Button":
					case "Label":
					case "DataGridView":
					case "DataGrid":
					case "PictureBox":
						break;
					default:
						MessageBox.Show(ctrl.Name + ": " + ctrl.GetType());
						break;
#endif
				}
			}
		}

		/// <summary>
		/// <para>Set .readOnly or .Enabled of all <see cref="Control"/>s in the given <paramref name="Control"/> to <paramref name="readOnly"/></para>
		/// </summary>
		/// <param name="Control">The <see cref="Control"/> which children <see cref="Control"/>s behaviour shall be set to <paramref name="readOnly"/></param>
		/// <param name="readOnly">Read only</param>
		public static void setControlsReadOnly(this Control Control, bool readOnly)
		{
			foreach (Control ctrl in Control.Controls)
			{
				switch (ctrl.GetType().Name)
				{
					case "TextBox":
						((TextBox)ctrl).ReadOnly = readOnly;
						break;
					case "RichTextBox":
						((RichTextBox)ctrl).ReadOnly = readOnly;
						break;
					case "ComboBox":
						((ComboBox)ctrl).ReadOnly(readOnly);
						break;
					case "CheckBox":
					case "Button":
					case "CheckedListBox":
						ctrl.Enabled = !readOnly;
						break;
					case "Panel":
					case "TabPage":
					case "SplitContainer":
					case "SplitterPanel":
						ctrl.setControlsReadOnly(readOnly);
						break;
#if DEBUG
					case "ProgressBar":
					case "Label":
					case "DataGridView":
					case "DataGrid":
					case "PictureBox":
						break;
					default:
						MessageBox.Show(ctrl.Name + ": " + ctrl.GetType().Name);
						break;
#endif
				}
			}
		}

		/// <summary>
		/// <para>Set .Enabled of all <see cref="Control"/> s in the given <paramref name="Control"/> to <paramref name="enabled"/></para>
		/// </summary>
		/// <param name="Control">The <see cref="Control"/> which children <see cref="Control"/>s shall be reset</param>
		/// <param name="enabled">Enable</param>
		public static void setControlsEnabled(this Control Control, bool enabled)
		{
			foreach (Control ctrl in Control.Controls)
			{
				switch (ctrl.GetType().Name)
				{
					case "TextBox":
					case "RichTextBox":
					case "ComboBox":
					case "CheckBox":
					case "Button":
					case "CheckedListBox":
						ctrl.Enabled = enabled;
						break;
					case "Panel":
					case "TabPage":
					case "SplitContainer":
					case "SplitterPanel":
						ctrl.setControlsEnabled(enabled);
						break;
#if DEBUG
					case "ProgressBar":
					case "Label":
					case "DataGridView":
					case "DataGrid":
					case "PictureBox":
						break;
					default:
						MessageBox.Show(ctrl.Name + ": " + ctrl.GetType().Name);
						break;
#endif
				}
			}
		}
	}
}