using System.Collections.Generic;
using BO.Connector.Console;
using Quadarax.Foundation.Core.QConsole;
using Quadarax.Foundation.Core.QConsole.Argument;
using Quadarax.Foundation.Core.QConsole.Value;
// ReSharper disable InconsistentNaming

namespace BO.Console.Commands.Base
{
    internal abstract class BaseCommand : BaseLocalCommand
    {
        #region *** Constants ***

        protected const string CS_ARG_NAME_DUMP = "dump";
        private const string CS_ARG_HINT_DUMP = "<is_dump_reponses>";
        private const string CS_ARG_DESC_DUMP = "Flag if set dumps incomming json messages.";

        protected const string CS_ARG_NAME_DEBUG = "dbg";
        private const string CS_ARG_HINT_DEBUG = "<is_debug_mode>";
        private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages.";

        protected const string CS_ARG_NAME_USER = "user";
        private const string CS_ARG_HINT_USER = "<user_name>";
        private const string CS_ARG_DESC_USER = "User name context. If not specified use default user context (who is logged in console).";

        protected const string CS_ARG_NAME_FORCE = "force";
        private const string CS_ARG_HINT_FORCE = "<is_force>";
        private const string CS_ARG_DESC_FORCE = "Flag if set, then overrite existing entity, otherwise cancel operation.";


        #endregion


        protected Connection Client { get; private set; }
        protected string UserName { get; private set; }

        protected BaseCommand(Engine engine) : base(engine)
        {
            
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            Client = new Connection(Configuration.ApiBaseUrl, Configuration.ApiTimeout, this, GetArgumentValueOrDefault<bool>(CS_ARG_NAME_DUMP));
            Client.Open(Configuration.ApiUser, Configuration.ApiUserPwd, Configuration.ApiMagic);
        }

        protected override void OnValidateArguments()
        {
            base.OnValidateArguments();
            if (ContainsArgument(CS_ARG_NAME_USER))
                UserName = GetArgumentValueOrDefault<string>(CS_ARG_NAME_USER);
        }

        
        protected void AppendUserArgument(IList<AbstractArgument> args, string alternativeUserDescription = null)
        {
            args.Add(new NamedArgument(CS_ARG_NAME_USER, string.IsNullOrEmpty(alternativeUserDescription)? CS_ARG_DESC_USER : alternativeUserDescription, CS_ARG_HINT_USER, TypeValuesEnum.String, string.Empty, false));
        }

    }
}
