context: #region Header
  //   Vorspire    _,-'/-'/  BattleList.cs
  //   .      __,-; ,'( '/
  //    \.    `-.__`-._`:_,-._       _ , . ``
  //     `:-._,------' ` _,`--` -: `_ , ` ,' :
  //        `---..__,,--'  (C) 2016  ` -'. -'
  //        #  Vita-Nex [http://core.vita-nex.com]  #
  //  {o)xxx|===============-   #   -===============|xxx(o}
  //        #        The MIT License (MIT)          #
  #endregion
  
  #region References
  using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.Text;
  
  using Server;
  using Server.Gumps;
  using Server.Misc;
  using Server.Mobiles;
  
  using VitaNex.Schedules;
  using VitaNex.SuperGumps;
  using VitaNex.SuperGumps.UI;
  #endregion
  
  namespace VitaNex.Modules.AutoPvP
  {
  	public class PvPBattleListGump : ListGump<PvPBattle>
  	{
  		public string Category { get; set; }
  		public bool UseConfirmDialog { get; set; }
  
  		public PvPBattleListGump(Mobile user, Gump parent = null, string category = "All", bool useConfirm = true)
  			: base(user, parent, emptyText: "There are no battles to display.", title: "PvP Battles")
  		{
  			Category = category.ToUpperWords() ?? "All";
  			UseConfirmDialog = useConfirm;
  
  			ForceRecompile = true;
  			AutoRefresh = true;
  		}
  
  		protected override void Compile()
  		{
  			if (AutoPvP.SeasonSchedule.Enabled)
  			{
  				Title = "PvP Battles (Season " + AutoPvP.CurrentSeason.Number + ")";
  			}
  
  			base.Compile();
  		}
  
  		public override int SortCompare(PvPBattle a, PvPBattle b)
  		{
  			var result = 0;
  
  			if (a.CompareNull(b, ref result))
  			{
  				return result;
  			}
  
  			if (a.Deleted && b.Deleted)
  			{
  				return 0;
  			}
  
  			if (a.Deleted)
  			{
  				return 1;
  			}
  
  			if (b.Deleted)
  			{
  				return -1;
  			}
  
  			if (a.Hidden && b.Hidden)
  			{
  				return 0;
  			}
  
  			if (a.Hidden)
  			{
  				return 1;
  			}
  
  			if (b.Hidden)
  			{
  				return -1;
  			}
  
  			var x = a.GetStateTimeLeft();
  			var y = b.GetStateTimeLeft();
  
  			if (x < y)
  			{
  				return -1;
  			}
  
  			if (x > y)
  			{
  				return 1;
  			}
  
  			return Insensitive.Compare(a.ToString(), b.ToString());
  		}
  
  		protected override void CompileMenuOptions(MenuGumpOptions list)
  		{
  			list.Clear();
  
  			if (User.AccessLevel >= AutoPvP.Access)
  			{
  				list.AppendEntry(
  					new ListGumpEntry(
  						"View Season Schedule",
  						b => Send(new ScheduleOverviewGump(User, AutoPvP.SeasonSchedule, Hide(true))),
  						(User.AccessLevel >= AutoPvP.Access) ? HighlightHue : TextHue));
  
  				list.AppendEntry(
  					new ListGumpEntry(
  						"Delete All",
  						b =>
  						{
  							if (UseConfirmDialog)
  							{
  								Send(
  									new ConfirmDialogGump(
  										User,
  										this,
  										title: "Delete All Battles?",
  										html:
  											"All battles in the database will be deleted, erasing all data associated with them.\nThis action can not be reversed.\n\nDo you want to continue?",
  										onAccept: OnConfirmDeleteAllBattles));
  							}
  							else
  							{
  								OnConfirmDeleteAllBattles(b);
  							}
  						},
  						HighlightHue));
  
  				list.AppendEntry(
  					new ListGumpEntry(
  						"Internalize All",
  						b =>
  						{
  							if (UseConfirmDialog)
  							{
  								Send(
  									new ConfirmDialogGump(
  										User,
  										this,
  										title: "Internalize All Battles?",
  										html:
  											"All battles in the database will be internalized, forcing them to end.\nThis action can not be reversed.\n\nDo you want to continue?",
  										onAccept: OnConfirmInternalizeAllBattles));
  							}
  							else
  							{
  								OnConfirmInternalizeAllBattles(b);
  							}
  						},
  						HighlightHue));
  
  				list.AppendEntry(
  					new ListGumpEntry(
  						"New Battle",
  						b => Send(new PvPScenarioListGump(User, Hide(true), UseConfirmDialog)),
  						HighlightHue));
  			}
  
  			base.CompileMenuOptions(list);
  		}
  
  		protected virtual void OnConfirmDeleteAllBattles(GumpButton button)
  		{
  			AutoPvP.DeleteAllBattles();
  			Refresh(true);
  		}
  
  		protected virtual void OnConfirmInternalizeAllBattles(GumpButton button)
  		{
  			AutoPvP.InternalizeAllBattles();
  			Refresh(true);
  		}
  
  		protected override void CompileList(List<PvPBattle> list)
  		{
  			list.Clear();
  
  			var viewInternal = (User.AccessLevel >= AutoPvP.Access);
  
  			foreach (var battle in AutoPvP.Battles.Values)
  			{
  				if (battle == null || battle.Deleted ||
  					((battle.State == PvPBattleState.Internal || battle.Hidden) && !viewInternal))
  				{
  					continue;
  				}
  
  				var cat = (String.IsNullOrWhiteSpace(battle.Category) ? "Misc" : battle.Category).ToUpperWords();
  
  				if (!String.IsNullOrWhiteSpace(Category))
  				{
  					if (!Insensitive.Equals(cat, Category) && !Insensitive.Equals(Category, "All"))
  					{
  						continue;
  					}
  				}
  
  				list.Add(battle);
  			}
  
  			base.CompileList(list);
  		}
  
  		public override string GetSearchKeyFor(PvPBattle key)
  		{
  			return key != null && !key.Deleted ? String.Format("{0} {1}", key.Name, key.Category) : base.GetSearchKeyFor(key);
  		}
  
  		protected override void SelectEntry(GumpButton button, PvPBattle entry)
  		{
  			base.SelectEntry(button, entry);
  
  			if (button == null || entry == null || entry.Deleted)
  			{
  				return;
  			}
  
  			var list = new MenuGumpOptions();
  
  			list.AppendEntry(
  				new ListGumpEntry(
  					"Overview",
  					b => Send(new PvPBattleOverviewGump(User, Hide(true), entry, UseConfirmDialog)),
  					TextHue));
  
  			if (User.AccessLevel >= AutoPvP.Access)
  			{
  				if (entry.State == PvPBattleState.Internal)
  				{
  					if (Selected.Validate(User))
  					{
  						list.AppendEntry(
  							new ListGumpEntry(
  								"Publish",
  								b =>
  								{
  									Selected.State = PvPBattleState.Queueing;
  									Refresh(true);
  								},
  								HighlightHue));
  					}
  				}
  				else
  				{
  					list.AppendEntry(
  						new ListGumpEntry(
  							"Internalize",
  							b =>
  							{
  								Selected.State = PvPBattleState.Internal;
  								Refresh(true);
  							},
  							HighlightHue));
  
  					if (!Selected.Hidden)
  					{
  						if (Selected.Validate(User))
  						{
  							list.AppendEntry(
  								new ListGumpEntry(
  									"Hide",
  									b =>
  									{
  										Selected.Hidden = true;
  										Refresh(true);
  									},
  									HighlightHue));
  						}
  					}
  					else
  					{
  						list.AppendEntry(
  							new ListGumpEntry(
  								"Unhide",
  								b =>
  								{
  									Selected.Hidden = false;
  									Refresh(true);
  								},
  								HighlightHue));
  					}
  				}
  
  				list.AppendEntry(
  					new ListGumpEntry(
  						"Delete",
  						b =>
  						{
  							if (UseConfirmDialog)
  							{
  								Send(
  									new ConfirmDialogGump(
  										User,
  										this,
  										title: "Delete Battle?",
  										html:
  											"All data associated with this battle will be deleted.\nThis action can not be reversed!\nDo you want to continue?",
  										onAccept: OnConfirmDeleteBattle));
  							}
  							else
  							{
  								OnConfirmDeleteBattle(b);
  							}
  						},
  						HighlightHue));
  			}
  
  			list.AppendEntry(
  				new ListGumpEntry(
  					"View Teams",
  					b => Send(new PvPTeamListGump(User, entry, Hide(true))),
  					(User.AccessLevel >= AutoPvP.Access) ? HighlightHue : TextHue));
  
  			if (User is PlayerMobile)
  			{
  				var user = (PlayerMobile)User;
  
  				if (entry.IsParticipant(user))
  				{
  					list.AppendEntry(new ListGumpEntry("Quit & Leave", b => entry.Eject(user, true)));
  				}
  				else
  				{
  					if (entry.IsQueued(user))
  					{
  						list.AppendEntry(new ListGumpEntry("Leave Queue", b => entry.Dequeue(user)));
  					}
  					else if (entry.CanQueue(user))
  					{
  						list.AppendEntry(new ListGumpEntry("Join Queue", b => entry.Enqueue(user)));
  					}
  
  					if (entry.IsSpectator(user))
  					{
  						list.AppendEntry(new ListGumpEntry("Leave Spectators", b => entry.RemoveSpectator(user, true)));
  					}
  					else if (entry.CanSpectate(user))
  					{
  						list.AppendEntry(new ListGumpEntry("Join Spectators", b => entry.AddSpectator(user, true)));
  					}
  				}
  			}
  
  			Send(new MenuGump(User, Refresh(), list, button));
  		}
  
  		protected virtual void OnConfirmDeleteBattle(GumpButton button)
  		{
  			if (Selected == null || Selected.Deleted)
  			{
  				Refresh(true);
  				return;
  			}
  
  			Selected.Delete();
  			Refresh(true);
  		}
  
  		protected override void CompileLayout(SuperGumpLayout layout)
  		{
  			base.CompileLayout(layout);
  
  			layout.Add(
  				"button/header/war",
  				() => AddButton(
  					85,
  					15,
  					2026,
  					2025,
  					b =>
  					{
  						var plg = Parent as PvPProfileListGump;
  
  						if (plg == null)
  						{
  							Send(new PvPProfileListGump(User, null, Hide(true), UseConfirmDialog));
  						}
  						else
  						{
  							plg.Refresh(true);
  						}
  					}));
  
  			layout.AddReplace(
  				"label/header/title",
  				() => AddLabelCropped(160, 15, 215, 20, GetTitleHue(), String.IsNullOrWhiteSpace(Title) ? DefaultTitle : Title));
  		}
  
  		protected override void CompileEntryLayout(SuperGumpLayout layout, Dictionary<int, PvPBattle> range)
  		{
  			layout.Add(
  				"background/footer/help",
  				() =>
  				{
  					AddBackground(0, 75 + (range.Count * 30), 420, 130, 9270);
  					AddImageTiled(10, 85 + (range.Count * 30), 400, 110, 2624);
  					//AddAlphaRegion(10, 85 + (range.Count * 30), 400, 110);
  				});
  
  			layout.Add("html/footer/help", () => AddHtml(20, 85 + (range.Count * 30), 390, 110, GetHelpText(), false, false));
  
  			layout.Add(
  				"background/footer/states",
  				() =>
  				{
  					AddBackground(0, 205 + (range.Count * 30), 420, 130, 9270);
  
gt: 					AddImageTiled(10, 215 + (range.Count * 30), 400, 110, 2624);