// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Globalization;

namespace Microsoft.TemplateEngine.Core.Expressions.Shared
{
    public static class CoreConverters
    {
        public static bool TryHexConvert(string prefix, ITypeConverter obj, object? source, out long result)
        {
            if (!obj.TryConvert(source, out string? ls))
            {
                result = 0;
                return false;
            }

            if (ls!.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && long.TryParse(ls.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result))
            {
                return true;
            }

            result = 0;
            return false;
        }

        public static bool TryHexConvert(string prefix, ITypeConverter obj, object? source, out int result)
        {
            if (!obj.TryConvert(source, out string? ls))
            {
                result = 0;
                return false;
            }

            if (ls!.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && int.TryParse(ls.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result))
            {
                return true;
            }

            result = 0;
            return false;
        }
    }
}
