using System;
using System.Collections.Generic;
using System.Linq;
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.Value;

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

        #region *** Constructors ***
        public CmdSelection(Engine engine) : base(engine)
        {
        }
        #endregion
       
        #region *** Protected overrides ***

        protected override IEnumerable<AbstractArgument> OnSetupArguments()
        {
            var arguments = base.OnSetupArguments().ToList();
            arguments.AddRange(
                new []
                {
                    new FlagArgument(Constants.Arguments.Selection.List.Code,
                        Constants.Arguments.Selection.List.Description, Constants.Arguments.Selection.List.Hint, true),
                    new FlagArgument(Constants.Arguments.Selection.Clear.Code,
                        Constants.Arguments.Selection.Clear.Description, Constants.Arguments.Selection.Clear.Hint,
                        false),
                    new NamedArgument(Constants.Arguments.Selection.Delete.Code,
                        Constants.Arguments.Selection.Delete.Description, Constants.Arguments.Selection.Delete.Hint,
                        TypeValuesEnum.String, string.Empty, false),
                });
            return arguments;
        }

        protected override void OnWriteEntry(ConsoleWriter writer, ISelectionEntry entry)
        {
            writer.Write("ID: ");
            writer.Write(entry.GetIdentifier());
            writer.Write("\t");
            writer.Write("Value: ");
            writer.WriteLine(entry.GetDisplayValue());
        }

        protected override Result EndExecute(Result incommingResult)
        {
            var result =  base.EndExecute(incommingResult);
            using (var writerWhite = new ConsoleWriter(ConsoleColor.White))
            {
                writerWhite.WriteLine(
                    $"For selection variable usage use {QConsole.Defaults.RoleCharacters.SelectionVariablePrefixCharacter}<selection_ordinal>.<property_name> in console.");
            }
            return result;
        }

        protected override Result OnExecute()
        {
            SetModel(Engine.Selections);
            return new Result();
        }
        #endregion
    }
}
