﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using EspaceClient.BackOffice.Domaine;
using System.Windows.Media.Imaging;
using nRoute.Components.Composition;
using EspaceClient.BackOffice.Silverlight.Business;
using System.Linq;
using EspaceClient.BackOffice.Silverlight.Business.Interfaces;

namespace EspaceClient.BackOffice.Silverlight.Converters
{
    public class CodePictoToPictoConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType() != typeof(string))
                throw new Exception("Incorrect value type.");

            if (targetType != typeof(ImageSource))
                throw new Exception("Incorrect target type.");
            
            var code = value as String;

            var image = ResourceLocator.GetResource<IApplicationContext>().Pictos.Where(x => x.Key.Equals(code)).Select(x => x.Value).FirstOrDefault();

            if (image == null)
                return null;

            var bitmap = new BitmapImage() { UriSource = new Uri(String.Format("/EspaceClient.BackOffice.Silverlight;component/Images/{0}", image), UriKind.Relative) };

            return bitmap;
        }

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