using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace TwitterClient.Converters
{
    class DateTimeToPeriodConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            String ret = null;
            String text;
            try
            {
                DateTime dt1 = DateTime.Now;
                DateTime dt2 = (DateTime)value;
                TimeSpan ts = dt1.Subtract(dt2);
                if (ts.Days > 0) ret = ts.Days + " dni temu";
                else if (ts.Hours > 0)
                {
                    if (ts.Hours == 1) text = " godzina temu";
                    else if (ts.Hours > 1 && ts.Hours < 5) text = " godziny temu";
                    else text = " godzin temu";
                    ret = ts.Hours + text;
                }
                else if (ts.Minutes > 0)
                {
                    if (ts.Minutes == 1) text = " minuta temu";
                    else if (ts.Minutes > 1 && ts.Minutes < 5) text = " minuty temu";
                    else text = " minut temu";
                    ret = ts.Minutes + text;
                }
                else if (ts.Seconds > 0)
                {
                    if (ts.Seconds <= 1) text = " sekunda temu";
                    else if (ts.Seconds > 1 && ts.Seconds < 5) text = " sekundy temu";
                    else text = " sekund temu";
                    ret = ts.Seconds + text;
                }
                return ret;
            }
            catch (Exception)
            {
                return value.ToString();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
