using System;
using System.Collections.Generic;

namespace Inspectron.Settings
{
    public static class TypeConverterRegistry
    {
        private static readonly Dictionary<Type, ITypeConverter> _converters = new Dictionary<Type, ITypeConverter>();

        public static void Register<T>(ITypeConverter converter)
        {
            _converters[typeof(T)] = converter;
        }

        public static ITypeConverter GetConverter<T>()
        {
            return _converters[typeof(T)];
        }

        public static ITypeConverter GetConverter(Type type)
        {
            if (_converters.TryGetValue(type, out var converter))
            {
                return converter;
            }
            throw new KeyNotFoundException($"No converter registered for type {type.FullName}");
        }

        public static bool TryGetConverter<T>(out ITypeConverter converter)
        {
            return _converters.TryGetValue(typeof(T), out converter);
        }

        public static bool TryGetConverter(Type type, out ITypeConverter converter)
        {
            return _converters.TryGetValue(type, out converter);
        }

        public static bool CanConvert<T>()
        {
            return _converters.ContainsKey(typeof(T));
        }
        public static bool CanConvert(Type type)
        {
            return _converters.ContainsKey(type);
        }
    }

    public interface ITypeConverter
    {
        // Converts an object from one type to another
        object ConvertFrom(object value);

        // Converts an object to a specified type
        object ConvertTo(object value, Type destinationType);
    }
}