namespace pdinklag
{
    public static class Height
    {
        private const float CM_TO_INCHES = 0.3937007874f;
        private const float INCHES_PER_FOOT = 12.0f;

        private static readonly string FeetAndInchesFormat = "{0}'{1}''";

        public enum Unit
        {
            [UIDisplayString("height.unit.Meters")] Meters,
            [UIDisplayString("height.unit.FeetAndInches")] FeetAndInches,
        }

        /// <summary>
        /// Formats a metric value in the specified target measure.
        /// </summary>
        /// <param name="value">the height in meters</param>
        /// <param name="unit">the target measure</param>
        public static string Format(float value, Unit unit)
        {
            switch (unit)
            {
                default:
                case Unit.Meters:
                    return value.ToString("F2");

                case Unit.FeetAndInches:
                    var centimeters = 100.0f * value;
                    var inchesTotal = CM_TO_INCHES * centimeters;
                    var feet = (int)(inchesTotal / INCHES_PER_FOOT);
                    var inches = (int)(inchesTotal - feet * INCHES_PER_FOOT);
                    return string.Format(FeetAndInchesFormat, feet, inches);
            }
        }
    }
}
