namespace OfficeControlsTestApp.Converters
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Windows.Data;
    using System.Windows.Media.Imaging;

    [ValueConversion(typeof(string), typeof(bool))]
    class HeaderToImageConverter : IValueConverter
    {
        public static HeaderToImageConverter Instance = new HeaderToImageConverter();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string path = (value as string).ToLower();

            if (path.EndsWith(@"\"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/drive.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else if (path.EndsWith(".xls") || path.EndsWith(".xlsx"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/excel.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else if (path.EndsWith(".doc") || path.EndsWith(".docx") || path.EndsWith(".rtf"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/word.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else if (path.EndsWith(".txt"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/text.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else if (path.EndsWith(".bmp"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/image-bmp.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else if (path.EndsWith(".gif"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/image-gif.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else if (path.EndsWith(".jpg") || path.EndsWith(".jpeg"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/image-jpeg.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else if (path.EndsWith(".png"))
            {
                Uri uri = new Uri("pack://application:,,,/Images/image-png.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else
            {
                Uri uri = File.Exists(path) ? new Uri("pack://application:,,,/Images/unknown.png") : new Uri("pack://application:,,,/Images/folder.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("Cannot convert back");
        }
    }
}
