using System;
using System.Collections.Generic;
using System.Linq;
using Quadarax.Foundation.Core.QConsole;
using Quadarax.Foundation.Core.QConsole.Argument;
using Quadarax.Foundation.Core.QConsole.Value;

namespace BO.Console.Commands.Base
{
    internal abstract class AbstractWrkGetCommand : AbstractGetCommand
    {
        #region *** Constants ***

        protected const string CS_ARG_NAME_APIK = "akey";
        private const string CS_ARG_HINT_APIK = "<api_key>";
        private const string CS_ARG_DESC_APIK = "Workspace related API-KEY, if not defined API-KEY is used from config.";

        protected const string CS_ARG_NAME_APIKP = "akeyp";
        private const string CS_ARG_HINT_APIKP = "<api_key_password>";
        private const string CS_ARG_DESC_APIKP = "Workspace related API-KEY password, if not defined API-KEY password is used from config.";

        #endregion


        #region *** Properties ***
        protected string ApiKey { get; private set; }
        protected string ApiKeyPassword { get; private set; }
        #endregion

        #region *** Constructors ***
        protected AbstractWrkGetCommand(Engine engine) : base(engine)
        {
        }
        #endregion

        #region *** Protected Overrides ***

        protected override void BeginExecute()
        {
            base.BeginExecute();
            ApiKey = Configuration.Defaults.Workspace.ApiKey;
            ApiKeyPassword = Configuration.Defaults.Workspace.ApiKeyPassword;

            var apiKey = GetArgumentValueOrDefault<string>(CS_ARG_NAME_APIK);
            var apiKeyPassword = GetArgumentValueOrDefault<string>(CS_ARG_NAME_APIKP);

            if (!string.IsNullOrEmpty(apiKey))
                ApiKey = apiKey;
            if (!string.IsNullOrEmpty(apiKeyPassword))
                ApiKey = apiKeyPassword;

            if (string.IsNullOrEmpty(ApiKey) || string.IsNullOrEmpty(ApiKeyPassword))
                throw new ArgumentException(
                    $"ApiKey or ApiKeyPassword must be specified by argument {Engine.Configuration.CharacterNamedArgumentSeparator}{CS_ARG_NAME_APIK} or {Engine.Configuration.CharacterNamedArgumentSeparator}{CS_ARG_NAME_APIKP} or specified in configuration file {Constants.CS_CONFIGURATION_FILE} in location 'defaults/workspace/api-key' or 'defaults/workspace/api-key-password'.");

            WriteInfo($"Using API-KEY: {ApiKey}");
            WriteDebugInfo($"Using API-KEY-PASSWORD: {ApiKeyPassword}");

        }

        protected override IEnumerable<AbstractArgument> OnSetupArguments()
        {
            var args =  base.OnSetupArguments().ToList();
            args.Add(new NamedArgument(CS_ARG_NAME_APIK, CS_ARG_DESC_APIK,CS_ARG_HINT_APIK, TypeValuesEnum.String, string.Empty, false));
            args.Add(new NamedArgument(CS_ARG_NAME_APIKP, CS_ARG_DESC_APIKP,CS_ARG_HINT_APIKP, TypeValuesEnum.String, string.Empty, false));
            return args;
        }

        #endregion
    }
}
