using System;
using System.Linq;
using System.Reflection;

namespace InABox.Core
{
    public static class EditorUtils
    {
        /// <summary>
        /// Returns a default editor for the given type, e.g., a <see cref="CheckBoxEditor"/> for <see cref="bool"/>.
        /// If the type does not have a sensible editor, returns <see langword="null"/>.
        /// </summary>
        /// <param name="type"></param>
        /// <returns>A new <see cref="BaseEditor"/>, or <see langword="null"/> if no sensible default.</returns>
        public static BaseEditor? GetEditor(Type type)
        {
            BaseEditor? editor = null;

            if (type == typeof(bool))
                editor = new CheckBoxEditor();
            else if (type == typeof(DateTime))
                editor = new DateTimeEditor();
            else if (type == typeof(TimeSpan))
                editor = new DurationEditor();
            else if (type == typeof(string))
                editor = new TextBoxEditor();
            else if (type.IsFloatingPoint())
                editor = new DoubleEditor();
            else if (type.IsOrdinal())
                editor = new IntegerEditor();
            else if (typeof(IPackable).IsAssignableFrom(type))
                editor = new NullEditor();
            else if(type.IsEnum)
                editor = new EnumLookupEditor(type);

            return editor;
        }
    }
}