﻿using System.Reflection;
using Microsoft.Extensions.CommandLineUtils;

namespace M1.Fx.ConsoleUtils.Metadata
{
    internal class OptionMetadata
    {
        public OptionMetadata(PropertyInfo propertyInfo)
        {
            var attribute = propertyInfo.GetCustomAttribute<OptionAttribute>();

            PropertyInfo = propertyInfo;

            Template = attribute.Template;
            Description = attribute.Description;
        }

        public string Template { get; set; }
        public string Description { get; set; }
        public PropertyInfo PropertyInfo { get; set; }

        public CommandLineApplication Application { get; set; }
        public CommandOption CommandOption { get; set; }

        public OptionMetadata Bind(CommandLineApplication app)
        {
            Application = app;
            CommandOption = Application.Option(Template, Description, CommandOptionType.SingleValue);
            return this;
        }

        public void Apply(object command)
        {
            PropertyInfo.SetValue(command, CommandOption.Value());
        }
    }
}