using System.Collections.Generic;
using System.Linq;
using BO.AppServer.Metadata.Enums;
using BO.Console.Commands.Base;
using Quadarax.Foundation.Core.QConsole;
using Quadarax.Foundation.Core.QConsole.Argument;
using Quadarax.Foundation.Core.QConsole.Attributes;
using Quadarax.Foundation.Core.QConsole.Value;
using Quadarax.Foundation.Core.Value;

namespace BO.Console.Commands.Workspace
{
    [CommandDefinition]
    internal class WorkspacesGetCommand : AbstractListScopedCommand<WorkspaceScopeEnums>
    {
        #region *** Constants ***

        private const string CS_CMD_WRKSPGET_NAME = "workspace_list";
        private const string CS_CMD_WRKSPGET_DESCR = "Get list of workspaces by specification scope.";


        private 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 when scope 'My' is selected. If not specified, use current user context.";

        #endregion

        #region *** Properties ***

        public override string Name => CS_CMD_WRKSPGET_NAME;
        public override string Description => CS_CMD_WRKSPGET_DESCR;

        #endregion

        #region *** Constructor ***

        public WorkspacesGetCommand(Engine engine) : base(engine)
        {
        }

        #endregion

        #region *** EXECUTE ***

        protected override Result OnExecute()
        {
            var workspaces = Client.GetWorkspacesAsync(Scope,GetArgumentValueOrDefault<string>(CS_ARG_NAME_USER), Paging).Result;
            WriteDtoToConsole(workspaces,"Id","Name","Created","DocumentsCount", "ApiKey", "ApiPassword");
            return new Result();
        }

        #endregion

        #region *** Private operations ***

        protected override void OnValidateArguments()
        {
            base.OnValidateArguments();
            if (Scope != WorkspaceScopeEnums.My && WasArgumentSpecified(CS_ARG_NAME_USER))
            {
                WriteWarning($"Argument {Engine.Configuration.CharacterNamedArgumentSeparator}{CS_ARG_NAME_USER} is not allowed in scope '{Scope}'. Skipped.");
                var arg = GetArgument(CS_ARG_NAME_USER);
                arg.SetValueAsDefault();
            }

        }

        protected override IEnumerable<AbstractArgument> OnSetupArguments()
        {
            var args = base.OnSetupArguments().ToList();
            args.Add(new NamedArgument(CS_ARG_NAME_USER, CS_ARG_DESC_USER, CS_ARG_HINT_USER, TypeValuesEnum.String, string.Empty, false));
            return args;
        }

        #endregion
    }
}