using AllocsFixes.PersistentData;
using System;
using System.Collections.Generic;

namespace AllocsFixes.CustomCommands
{
	public class ListKnownPlayers : ConsoleCommand
	{
		public ListKnownPlayers (ConsoleSdtd cons) : base(cons)
		{
		}

		public override string Description ()
		{
			return "lists all players that were ever online (optionally filtered)";
		}

		public override string[] Names ()
		{
			return new string[] { "listknownplayers", "lkp" };
		}

		public override void Run (string[] _params)
		{
			try {
				AdminTools admTools = CommonMappingFunctions.GetGameManager ().adminTools;

				bool onlineOnly = false;
				bool notBannedOnly = false;
				string nameFilter = string.Empty;

				if (_params.Length == 1) {
					if (_params [0].ToLower ().Equals ("-online")) {
						onlineOnly = true;
					} else if (_params [0].ToLower ().Equals ("-notbanned")) {
						notBannedOnly = true;
					} else {
						nameFilter = _params [0].ToLower ();
					}
				}

				int num = 0;
				foreach (string sid in PersistentContainer.Instance.Players.SteamIDs) {
					Player p = PersistentContainer.Instance.Players [sid];

					if (
						(!onlineOnly || p.IsOnline)
						&& (!notBannedOnly || !admTools.IsBanned (sid))
						&& (nameFilter.Length == 0 || p.Name.ToLower ().Contains (nameFilter))
					) {
						m_Console.SendResult (String.Format ("{0}. {1}, id={2}, steamid={3}, online={4}, ip={5}, playtime={6} m, seen={7}",
						                                    ++num, p.Name, p.EntityID, sid, p.IsOnline, p.IP,
						                                    p.TotalPlayTime / 60,
						                                    p.LastOnline.ToString ("yyyy-MM-dd HH:mm"))
						);
					}
				}
				m_Console.SendResult ("Total of " + PersistentContainer.Instance.Players.Count + " known");
			} catch (Exception e) {
				Log.Out ("Error in ListKnownPlayers.Run: " + e);
			}
		}
	}
}
