using System;
using System.Collections.Generic;
using System.Linq;
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.Document
{
    [CommandDefinition]
    internal class DocumentClearCommand: AbstractWorkspaceCommand 
    {
        #region *** Constants ***
        private const string CS_CMD_DOCCLR_NAME = "document_clear";
        private const string CS_CMD_DOCCLR_DESCR = "Add new metadocument with artifact to workspace or append artifact to existing document.";


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

        private const string CS_ARG_NAME_NAME = "name";
        private const string CS_ARG_HINT_NAME = "<document_name>";
        private const string CS_ARG_DESC_NAME = "Metadata document name. If not specified use first artifact name. If document already exists, appends artifact to existing metadocument.";

        private const string CS_ARG_DESC_FORCE = "Flag if set, then clears artifacts and ignores metadocument state, otherwise cancel operation.";

        #endregion

        #region *** Properties ***

        public override string Name => CS_CMD_DOCCLR_NAME;
        public override string Description => CS_CMD_DOCCLR_DESCR;


        protected string DocumentName { get; set; }

        #endregion

        #region *** Constructor ***
        public DocumentClearCommand(Engine engine) : base(engine)
        {
        }
        #endregion

        #region *** EXECUTE ***
        protected override Result OnExecute()
        {
            WriteDebugInfo($"Getting metadocument '{DocumentName}' in workspace...");
            var document = Client.GetWorkspaceDocumentByNameAsync(ApiKey, ApiKeyPassword, DocumentName, UserName).Result;
            if (document!=null)
            {
                WriteDebugInfo($"Document '{DocumentName}' exists [{document.Id}] in workspace. Deleting artifacts...");
                var deletedArtifacts = Client.ClearWorkspaceDocumentArtifactAsync(ApiKey, ApiKeyPassword, document.Id,
                    IsForce.GetValueOrDefault(), UserName).Result;

                int cnt = 0;
                foreach (var art in deletedArtifacts)
                {
                    WriteInfo($"Artifact '{art.Name + art.Extension}' [{art.Id}] (Created:{art.Audit.Created}) DELETED.");
                    cnt++;
                }
                WriteCaption($"Deleted {cnt} artifacts.");
            }
            else
                return new Result(new Exception($"Document '{DocumentName}' not found."));

            return new Result();
        }
        #endregion

        #region *** Protected overrides ***
        protected override void OnValidateArguments()
        {
            base.OnValidateArguments();
            DocumentName = GetArgumentValueOrDefault<string>(CS_ARG_NAME_NAME);

        }

        #endregion

        #region *** Private operations ***

        protected override IEnumerable<AbstractArgument> OnSetupArguments()
        {
            var args = base.OnSetupArguments().ToList();
            args.Add(new NamedArgument(CS_ARG_NAME_NAME, CS_ARG_DESC_NAME,CS_ARG_HINT_NAME, TypeValuesEnum.String, string.Empty, false));
            AppendUserArgument(args, CS_ARG_DESC_USER);
            AppendForceArgument(args, CS_ARG_DESC_FORCE);
            return args.ToArray();
        }

        #endregion
    }
}
