// Copyright (C) 2015-2026 The Neo Project.
//
// UIntBaseConverter.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System.ComponentModel;
using System.Globalization;

namespace Neo.GUI.Wrappers;

internal class UIntBaseConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;
        return false;
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string))
            return true;
        return false;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string s)
            return context.PropertyDescriptor.PropertyType.GetMethod("Parse").Invoke(null, new[] { s });
        throw new NotSupportedException();
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType != typeof(string))
            throw new NotSupportedException();

        return value switch
        {
            UInt160 i => i.ToString(),
            UInt256 i => i.ToString(),
            _ => null,
        };
    }
}
