using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quadarax.Foundation.Common.Console;
using Quadarax.Foundation.Common.Value;
using Quadarax.Foundation.QConsole.Argument;
using Quadarax.Foundation.QConsole.Attributes;
using Quadarax.Foundation.QConsole.Command.Base;
using Quadarax.Foundation.QConsole.Extensions;
using Quadarax.Foundation.QConsole.Value;

namespace Quadarax.Foundation.QConsole.Command.Defaults
{
    [CommandDefinition]
    public class CmdHelp : AbstractCommand
    {
        #region *** Properties ***
        public override string Name => Constants.Commands.Help.Name;
        public override string Description => Constants.Commands.Help.Description;
        #endregion

        #region *** Constructors ***
        public CmdHelp(Engine engine) : base(engine)
        {
        }
        #endregion

        #region *** Protected overrides ***
        protected override Result OnExecute()
        {
            if (!GetArgument(Constants.Commands.Help.ArgCommandName.Code).Value.HasValue)
            {
                if (!Engine.Configuration.IsConsoleDebug)
                {
                    using (var writer = new ConsoleWriter(ConsoleColor.White))
                    {
                        writer.WriteLine(
                            "To enable debug mode - more verbosity - use argument \"-dbg\" at the end of line.");
                    }
                }

                using (var writer = new ConsoleWriter(ConsoleColor.Yellow))
                {
                    writer.WriteLine("Console commands list:");
                }

                using (var writer = new ConsoleWriter(ConsoleColor.Green))
                {
                    foreach (var command in Engine.Commands)
                    {
                        writer.WriteLine($"{command.Name}\t{command.Description}");
                    }

                    writer.WriteLine();
                }

                using (var writer = new ConsoleWriter(ConsoleColor.Yellow))
                {
                    writer.WriteLine($"To view more info for specific command type {Name} <{GetArgument(Constants.Commands.Help.ArgCommandName.Code).Hint}>");
                }
            }
        
            else
            {
                var argument = GetArgument(Constants.Commands.Help.ArgCommandName.Code);
                var command = Engine.GetCommand(argument.Value.AsString());
                if (command == null)
                    throw new Exception($"Unkonwn command '{argument.Value.AsString()}'");
                using (var writer = new ConsoleWriter(ConsoleColor.Green))
                {
                    using (var writerWhite = new ConsoleWriter(ConsoleColor.White))
                    {
                        writer.Write($"Command: \t");
                        writerWhite.WriteLine(command.Name);
                        writer.Write($"Description: \t");
                        writerWhite.WriteLine(command.Description);
                        writer.Write($"Parameters: \t");
                        writerWhite.WriteLine((command.Arguments.Length == 0 ? "none" : string.Empty));
                        foreach (var arg in command.Arguments.OrderBy(x => x.IsPositional() ? 0 : 1))
                        {
                            var optional = arg.IsMandatory ? "Mandatory." : "Optional.";
                            var argumentName = arg.IsPositional()
                                ? $"<{arg.Code}>"
                                : $"{Engine.Configuration.CharacterNamedArgumentSeparator}{arg.Code}";
                            writer.WriteLine($"{argumentName}\t{optional}{arg.Description}");
                        }

                        var usage = new StringBuilder();
                        writer.WriteLine("Usage:");
                        writer.Write($"\t{Engine.Configuration.CharacterLineIntroduce}");
                        usage.Append(command.Name);
                        foreach (var arg in command.Arguments.OrderBy(x => x.IsPositional() ? 0 : 1))
                        {
                            usage.Append(" ");
                            usage.Append(GetArgumentEntryAsDescription(arg));
                        }

                        writerWhite.WriteLine(usage.ToString());
                    }
                }

            }
            return new Result();
        }

        protected override IEnumerable<AbstractArgument> OnSetupArguments()
        {
            var result = base.OnSetupArguments().ToList();
            result.AddRange( new AbstractArgument[]
            {
                new OrdinalArgument(0, true, 
                    Constants.Commands.Help.ArgCommandName.Code,
                    Constants.Commands.Help.ArgCommandName.Description,
                    Constants.Commands.Help.ArgCommandName.Hint,
                    TypeValuesEnum.String,
                    string.Empty,
                    false)
            });
            return result;
        }
        #endregion

        #region *** Private Operations ***
        private string GetArgumentEntryAsDescription(AbstractArgument arg)
        {
            var argumentName = arg.IsPositional()
                ? $"<{arg.Code}>"
                : $"{Engine.Configuration.CharacterNamedArgumentSeparator}{arg.Code}";

            var argumentBody = argumentName + Engine.Configuration.CharacterNamedArgumentValueSeparator + arg.Hint;

            if (!arg.IsMandatory)
            {
                argumentBody = "[" + argumentBody + "]";
            }

            return argumentBody;
        }
        #endregion
    }
}
