//-----------------------------------------------------------------------
// <copyright file="ViewGroupToStringConverter.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>
// Implements group title as name plus (count).
//</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Management.UI.Internal
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Collections.Generic;
    using System.Text;
    using System.Globalization;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// Converter from ViewGroup to group title string.
    /// </summary>
    [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
    internal class ViewGroupToStringConverter : IValueConverter
    {
        /// <summary>
        /// Convert each ViewGroup into its name and its count.
        /// </summary>
        /// <param name="value">Value to be converted.</param>
        /// <param name="targetType">Type to convert the value to.</param>
        /// <param name="parameter">The conversion parameter.</param>
        /// <param name="culture">Conversion culture.</param>
        /// <returns>The converted string.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            CollectionViewGroup cvg = value as CollectionViewGroup;
            if (null == cvg)
            {
                throw new ArgumentException("value must be of type CollectionViewGroup", "value");
            }

            string name = (!String.IsNullOrEmpty(cvg.Name.ToString())) ? cvg.Name.ToString() : UICultureResources.GroupTitleNone;
            string display = String.Format(CultureInfo.CurrentCulture, "{0} ({1})", name, cvg.ItemCount);

            return display;
        }

        /// <summary>
        /// ConvertBack is not supported.
        /// </summary>
        /// <param name="value">Value to be converted.</param>
        /// <param name="targetType">Type to convert the value to.</param>
        /// <param name="parameter">The conversion parameter.</param>
        /// <param name="culture">Conversion culture.</param>
        /// <returns>This method is not supported.</returns>
        /// <exception cref="NotSupportedException">when calling the method.</exception>
        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            System.Globalization.CultureInfo culture)
        {
            // I can't think of nothing that could be added to the exception message
            // that would be of further help
            throw new NotSupportedException();
        }
    }
}
