using System;
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.ComponentModel;
using C1.Silverlight;
using System.Globalization;

namespace C1TreeViewTemplateSample
{

    /// <summary>
    /// Represents a custom TypeConverter used to convert the item headers from the sample
    /// to a String representation so we can make use of the autosearch and find operations.
    /// </summary>
    public class CustomItemConverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            C1TreeViewItem itemValue = value as C1TreeViewItem;
            if ((itemValue != null) && (itemValue.Header != null))
            {
                var group = itemValue.Header as WorldCupGroup;
                var team = itemValue.Header as WorldCupTeam;
                
                if (group != null)
                    return group.GroupName;

                if (team != null)
                    return team.CountryName;
            }

            return value.ToString();
        }
    }
}
