using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace B4Restore.Common
{
	public class WorkWithControl
	{
		private readonly List<System.Web.UI.Control> controlList = new List<System.Web.UI.Control>();

		public static object GetPostSessionParameterValue(string paramName)
		{
			return GetPostSessionParameterValue(paramName, new[] { GetPostSession.Get, GetPostSession.Session });
		}

		public static object ServerId
		{
			get
			{
				return GetPostSessionParameterValue("Server_id",
													new[] { GetPostSession.Session, GetPostSession.Get });
			}
		}

		public static object ClusterId
		{
			get
			{
				return GetPostSessionParameterValue("ClusterID",
													new[] { GetPostSession.Session, GetPostSession.Get });
			}
		}

		public List<System.Web.UI.Control> GetControlList(System.Web.UI.Control root, string id)
		{
			FindControlRecursive(root, id, true);

			return controlList;
		}

		public System.Web.UI.Control FindControlRecursive(System.Web.UI.Control root, string id, bool findAll)
		{
			System.Web.UI.Control result = null;

			if (root.ID == id)
				result = root;
			else
				foreach (System.Web.UI.Control ctl in root.Controls)
				{
					var foundCtl = FindControlRecursive(ctl, id, findAll);
					if (foundCtl != null)
						if (findAll)
							controlList.Add(foundCtl);
						else
							return foundCtl;
				}

			return result;
		}


		public System.Web.UI.Control FindControlRecursiveEndsWith(System.Web.UI.Control root, string endWith, bool findAll)
		{
			System.Web.UI.Control result = null;
			if (root.ID.EndsWith(endWith))
				result = root;
			else
				foreach (System.Web.UI.Control ctl in root.Controls)
				{
					var foundCtl = FindControlRecursiveEndsWith(ctl, endWith, findAll);
					if (foundCtl != null)
						if (findAll)
							controlList.Add(foundCtl);
						else
							return foundCtl;
				}

			return result;
		}


		public System.Web.UI.Control FindControlRecursive(System.Web.UI.Control root, string id, bool findAll, bool findVisibleOnly)
		{
			System.Web.UI.Control result = null;

			if (root.ID == id)
				result = root;
			else
				foreach (System.Web.UI.Control ctl in root.Controls)
				{
					var foundCtl = FindControlRecursive(ctl, id, findAll, findVisibleOnly);
					if (foundCtl != null)
						if (findAll)
						{
							if ((findVisibleOnly) && (foundCtl.Visible))
								controlList.Add(foundCtl);

						}
						else
						{
							if ((findVisibleOnly) && (foundCtl.Visible))
								return foundCtl;
						}
				}

			return result;
		}


		public IList<System.Web.UI.Control> GetControlList(System.Web.UI.Control root, Type t)
		{
			FindControlRecursive(root, t, true);

			return controlList;
		}

		public System.Web.UI.Control FindControlRecursive(System.Web.UI.Control root, Type t, bool findAll)
		{
			System.Web.UI.Control result = null;

			if (root.GetType().Equals(t))
				result = root;
			else
				foreach (System.Web.UI.Control ctl in root.Controls)
				{
					var foundCtl = FindControlRecursive(ctl, t, findAll);
					if (foundCtl != null)
						if (findAll)
						{
							controlList.Add(foundCtl);
						}
						else
						{
							return foundCtl;
						}
				}

			return result;
		}

		public static TType FindControl<TType>(System.Web.UI.Control root, string id)
			where TType : System.Web.UI.Control
		{
			return root.FindControl(id) as TType;
		}


		public static void MoveBetweenList(ListBox leftList, ListBox rightList, DirectionMove direction, bool moveAll)
		{
			var realLeftList = (direction == DirectionMove.LeftToNowhere || direction == DirectionMove.LeftToRight || direction == DirectionMove.LeftToRightCopy) ? leftList : rightList;
			var realRightList = (direction == DirectionMove.LeftToNowhere || direction == DirectionMove.LeftToRight || direction == DirectionMove.LeftToRightCopy) ? rightList : leftList;

			for (var i = realLeftList.Items.Count - 1; i >= 0; i--)
				if (realLeftList.Items[i].Selected || moveAll)
				{
					if (direction != DirectionMove.LeftToNowhere && direction != DirectionMove.RightToNowhere)
						realRightList.Items.Add(realLeftList.Items[i]);
					if (direction != DirectionMove.LeftToRightCopy && direction != DirectionMove.RightToLeftCopy)
						realLeftList.Items.Remove(realLeftList.Items[i]);
				}
			realLeftList.SelectedIndex = -1;
			realRightList.SelectedIndex = -1;
		}

		public static void MoveFromTextToList(TextBox text, ListBox list)
		{
			if (text.Text == String.Empty || text.Text.IndexOf(' ') != -1)
				return;
			list.Items.Add(new ListItem(text.Text, text.Text));
			text.Text = "";
			list.SelectedIndex = -1;
		}

		public static void DeleteFromList(ListBox list, bool deleteAll)
		{
			for (var i = list.Items.Count - 1; i >= 0; i--)
				if (list.Items[i].Selected || deleteAll)
					list.Items.Remove(list.Items[i]);
			list.SelectedIndex = -1;
		}

		public static void MoveUp(ListBox list)
		{
			for (var i = 0; i < list.Items.Count; i++)
				if (list.Items[i].Selected)
					//swap with the top item(move up)
					if (i > 0 && !list.Items[i - 1].Selected)
					{
						var bottom = list.Items[i];
						list.Items.Remove(bottom);
						list.Items.Insert(i - 1, bottom);
						list.Items[i - 1].Selected = true;
					}
		}

		public static void MoveDown(ListBox list)
		{
			var startindex = list.Items.Count - 1;
			for (var i = startindex; i > -1; i--)
				if (list.Items[i].Selected)
					//swap with the lower item(move down)
					if (i < startindex && !list.Items[i + 1].Selected)
					{
						var bottom = list.Items[i];
						list.Items.Remove(bottom);
						list.Items.Insert(i + 1, bottom);
						list.Items[i + 1].Selected = true;
					}
		}


		public static void SynchroListBoxes(ListBox left, ListBox right)
		{
			if (right.Items.Count > 0)
			{
				foreach (ListItem item in right.Items)
				{
					left.Items.Remove(left.Items.FindByValue(item.Value));
				}
			}
		}

		public static void SortList(ListItemCollection items, bool Descending)
		{
			var list = new List<ListItem>();

			foreach (ListItem i in items)
				list.Add(i);

			if (Descending)
				list.Sort((x, y) => y.Text.CompareTo(x.Text));
			else
				list.Sort((x, y) => x.Text.CompareTo(y.Text));

			items.Clear();
			items.AddRange(list.ToArray());
		}

		private static object GetPostSessionParameterValue(string paramName, IEnumerable<GetPostSession> GetPostSessionPriority)
		{
			object result = null;

			foreach (var priority in GetPostSessionPriority)
			{
				switch (priority)
				{
					case GetPostSession.Get:
						{
							result = HttpContext.Current.Request.QueryString[paramName];
							break;
						}
					case GetPostSession.Post:
						{
							result = HttpContext.Current.Request.Form[paramName];
							break;
						}
					case GetPostSession.Session:
						{
							result = HttpContext.Current.Session[paramName];
							break;
						}
				}

				if (result != null)
					break;
			}

			return result;
		}
	}
}