using System;
using System.Globalization;
using System.IO;
using Xamarin.Forms;

namespace InABox.Mobile
{
    public class ByteArrayToImageSourceConverter : IValueConverter
    {
        public bool BlankIfEmpty { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is byte[] bytes)
                return ImageSource.FromStream(() => new MemoryStream(bytes));
            return BlankIfEmpty ? null : ImageSource.FromFile("cross");
        }

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