using System;
using System.Globalization;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using C1.Xaml.Chart;

namespace StockPortfolio.Common
{
    /// <summary>
    /// Value converter that translates true to <see cref="Visibility.Visible"/> and false to
    /// <see cref="Visibility.Collapsed"/>.
    /// </summary>
    public sealed class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;
        }
    }

    public sealed class StringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (value is string && (!string.IsNullOrEmpty((string)value))) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return new NotImplementedException();
        }
    }
    class DoubleToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value != null && !value.ToString().StartsWith("-"))
            {
                return new SolidColorBrush(Color.FromArgb(255, 113, 171, 4));
            }
            else
            {
                return new SolidColorBrush(Color.FromArgb(255, 204, 17, 33));
            }
        }

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

    class ChangeToTransformConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is double && (double)value > 0)
            {
                return 0;
            }
            else
            {
                return 180;
            }
        }

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

    class StringFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            // No format provided
            if (parameter == null)
            {
                return value;
            }
            return String.Format((String)parameter, value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }

    class DateFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is double && parameter != null)
            {
                try
                {
                    double d = (double)value;

                    if (parameter.ToString().Equals("l"))
                    {
                        DateTime date = d.FromOADate();
                        string longDateString = DateTimeFormatInfo.CurrentInfo.MonthNames[date.Month] + date.Day.ToString() + ", " + date.Year.ToString();
                        return longDateString;
                    }
                    else
                    {
                        return String.Format((String)parameter, d.FromOADate());
                    }
                }
                catch
                {

                }

            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }

    class DoubleToGlyphConverter: IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value != null && value.ToString().StartsWith("-"))
            {
                return ((char)(0xe1fd)).ToString();//down
            }
            else
            {
                return ((char)(0xe110)).ToString();//up
            }
        }

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

    class DateTimeToStringConverter:IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value!=null && value is DateTime)
            {
                var dateTime = (DateTime)value;

                return dateTime.ToString("yyyy-MM-dd");
            }
            else
            {
               return  DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

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