using System;
using System.Net;
using System.Windows;
using Windows.UI.Xaml.Data;

namespace Fortune1000
{
    /// <summary>
    /// Converter that gets the first character of an item.
    /// </summary>
    public class InitialConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return char.ToUpper(value.ToString()[0]);
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter that groups values into ranges.
    /// </summary>
    public class MarketValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            // convert value to a double
            var d = (double)System.Convert.ChangeType(value, typeof(double));

            // handle extreme values
            if (d > 60)
            {
                return "> $60 billion";
            }
            if (d < 10)
            {
                return "< $10 billion";
            }

            // break up intermediate values into bins of size 10b
            var binSize = 10;
            var low = (int)(d / binSize) * binSize;
            var high = low + binSize;
            return string.Format("${0} to ${1} billion", low, high);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
