using System;
using SlatedGameToolkit.Tools.CommandSystem.Interaction;

namespace SlatedGameToolkit.Tools.CommandSystem
{
    public class CommandProcessor
    {
        CommandMap commandMap;

        /// <summary>
        /// The general help is the string that is printed when the input is not understood.
        /// {input} in the string is replaced with the user input.
        /// </summary>
        /// <param name="commands"></param>
        public CommandProcessor(CommandMap commands) {
            this.commandMap = commands;
        }
        public void Process(IInteractable interactable) {
            string message = interactable.Listen().ToLower();
            string[] splitMessage = message.Split(' ');
            string invocation = splitMessage[0];

            IInvocable invocable = commandMap[invocation];
            if (invocable != null) {
                string[] args = new string[splitMessage.Length - 1];
                Array.Copy(splitMessage, 1, args, 0, splitMessage.Length - 1);
                if (!invocable.Execute(interactable, args)) {
                    interactable.Tell(string.Format("The command \"{0}\" arguments were incorrect. Please refer to Please type \"help {0}\" for more information.", invocation));
                }
                return;
            }
            interactable.Tell(string.Format("The command \"{0}\" was not understood. Please type \"help\" for more information.", invocation));
        }
    }
}