using System;
using System.Text;

namespace RecrownedAthenaeum.Tools.CommandProcessor
{
    public abstract class EngineCommand
    {

        bool caseSensitiveName = false;

        /// <summary>
        /// the words a user can type that will invoke this command.
        /// </summary>
        protected string[] invokeStrings;
        public string[] InvokeStrings { get { return invokeStrings; } }

        /// <summary>
        /// Arguments that this command should accept and take into account.
        /// </summary>
        protected EngineCommandArgument[] commandArguments;
        protected string help;

        public EngineCommand(params string[] invokeStrings)
        {
            this.invokeStrings = invokeStrings ?? throw new ArgumentNullException();
        }

        /// <summary>
        /// Whether or not this command should be invoked given the string.
        /// </summary>
        /// <param name="command">The string that acts as the command.</param>
        /// <returns>whether or not this command is invoked.</returns>
        public bool IsInvoked(string command)
        {
            if (!caseSensitiveName) command = command.ToLower();
            for (int i = 0; i < invokeStrings.Length; i++)
            {
                if (!caseSensitiveName)
                {
                    if (invokeStrings[i].ToLower() == command) return true;
                }
                else
                {
                    if (invokeStrings[i] == command) return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Runs the command.
        /// </summary>
        /// <param name="arguments">arguments to be used. May be null.</param>
        public abstract void Run(string[] arguments = null);

        /// <summary>
        /// Check if given argument is viable for command.
        /// </summary>
        /// <param name="argument">Argument to check.</param>
        /// <returns>True if valid.</returns>
        public bool Validate(string argument)
        {
            return EngineCommandArgumentOf(argument) == null ? false : true;
        }

        /// <summary>
        /// Returns the <see cref="EngineCommandArgument"/> of the given argument or null if the string is an invalid argument.
        /// </summary>
        /// <param name="argument">The argument string.</param>
        /// <returns>null if not viable or the <see cref="EngineCommandArgumentOf(argument)"/> if viable.</returns>
        public EngineCommandArgument EngineCommandArgumentOf(string argument)
        {
            for (int i = 0; i < commandArguments.Length; i++)
            {
                if (commandArguments[i].invokeString == argument)
                {
                    return commandArguments[i];
                }
            }
            return null;
        }

        /// <summary>
        /// Finds the index of the argument given in an array of arguments for the command.
        /// </summary>
        /// <param name="argument">The argument to find the index of.</param>
        /// <param name="arguments">The array containing all arguments.</param>
        /// <returns>The index or throws argument exception if it doesn't exist.</returns>
        public int IndexOfArgument(string argument, string[] arguments)
        {
            if (arguments != null)
            {
                for (int i = 0; i < arguments.Length; i++)
                {
                    if (arguments[i] == argument)
                    {
                        return i;
                    }
                }
            }
            throw new ArgumentException("Expected argument " + argument + " is missing. Type \"help\" for more information.");
        }

        public bool HasArgument(string argument, string[] arguments)
        {
            try
            {
                IndexOfArgument(argument, arguments);
            }
            catch (ArgumentException)
            {
                return false;
            }
            return true;
        }

        public bool HasArgument(EngineCommandArgument argument, string[] arguments)
        {
            return HasArgument(argument.invokeString, arguments);
        }

        /// <summary>
        /// Finds the index of the argument given in an array of arguments for the command.
        /// </summary>
        /// <param name="argument">The argument to find the index of.</param>
        /// <param name="arguments">The array containing all arguments.</param>
        /// <returns>The index or throws argument exception if it doesn't exist.</returns>
        public int IndexOfArgumentIn(EngineCommandArgument argument, string[] arguments)
        {
            return IndexOfArgument(argument.invokeString, arguments);
        }

        /// <summary>
        /// Called when help "command trigger" [argument] is invoked. Argument is optional and therefore, may be null. If the valid arguments are properly registered, this command will take care of it. If no arguments are provided, it will return the general help defined for this command.
        /// </summary>
        /// <param name="argument">Potential argument to request help for.</param>
        /// <returns>The string for the help.</returns>
        public string Help(string argument = null)
        {
            if (argument != null && commandArguments != null)
            {
                if (Validate(argument))
                {
                    EngineCommandArgument eca = EngineCommandArgumentOf(argument);
                    string helpString = eca.help;
                    if (eca.required) helpString = helpString + " required.";
                    return EngineCommandArgumentOf(argument).help;
                }
                else
                {
                    return "The argument " + argument + " does not exist. Type \"help " + invokeStrings[0] + "\" (or any of its aliases) for a list of arguments.";
                }
            }
            else
            {
                StringBuilder helpBuilder = new StringBuilder();
                helpBuilder.Append(help);
                if (commandArguments != null)
                {
                    helpBuilder.AppendLine();
                    helpBuilder.Append("Possible arguments are: ");
                    for (int i = 0; i < commandArguments.Length; i++)
                    {
                        helpBuilder.Append(commandArguments[i].invokeString);
                        if (commandArguments[i].required) helpBuilder.Append('*');
                        if (i == commandArguments.Length - 2)
                        {
                            helpBuilder.Append(", and ");
                        }
                        else if (i < commandArguments.Length - 2)
                        {
                            helpBuilder.Append(", ");
                        }
                    }
                    helpBuilder.Append('.');
                    helpBuilder.AppendLine(" [* are required arguments.]");
                }
                return helpBuilder.ToString();
            }

        }
    }
}
