namespace PCUT.Extensions
{
    public static class NumericExtensions
    {
        public static double GetValueByUnit(this string value)
        {
            if (value == null)
                return 0;

            double scale = 1;
            if (value.EndsWith("in"))
            {
                value = value.Replace("in", string.Empty);
                scale = 25.4;
            }
            else if (value.EndsWith("cm"))
            {
                value = value.Replace("cm", string.Empty);
                scale = 10;
            }
            else if (value.EndsWith("mm"))
            {
                value = value.Replace("mm", string.Empty);
                scale = 1;
            }
            else if (value.EndsWith("pc"))
            {
                value = value.Replace("pc", string.Empty);
                scale = (25.4 / 16);
            }
            else if (value.EndsWith("pt"))
            {
                value = value.Replace("pt", string.Empty);
                scale = (25.4 / 72);
            }
            else if (value.EndsWith("px"))
            {
                value = value.Replace("px", string.Empty);
                scale = (25.4 / 96);
            }

            if (double.TryParse(value, out var doubleValue))
            {
                return doubleValue * scale;
            }
            return 0;
        }
    }
}
