﻿using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Forms;
using FontFamily = System.Windows.Media.FontFamily;

namespace Tester.View
{
    public class PathShorter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var compactedString = string.Copy(values[0] as string);
            var maxSize = new Size((int) (double) values[1], 0);
            const TextFormatFlags formattingOptions = TextFormatFlags.PathEllipsis | TextFormatFlags.ModifyString;
            TextRenderer.MeasureText(compactedString, new Font((values[2] as FontFamily).Source,
                (float) (double) values[3]), maxSize, formattingOptions);
            if (compactedString.IndexOf("\0") > 0)
                compactedString = compactedString.Substring(0, compactedString.IndexOf("\0"));
            return compactedString;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}