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.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Globalization;
using C1.Silverlight;
using System.Reflection;

namespace C1_Demo
{
    /// <summary>
    /// Country class used in the <see cref="C1ComboBox"/> of countries.
    /// </summary>
    public class Country
    {
        #region ** constructors

        public Country()
        { }

        public Country(string flagName)
        {
            _flagSource = flagName;
        }

        #endregion

        #region ** object model

        private string _flagSource;
        public ImageSource Flag
        {
            get
            {
                return new BitmapImage() { UriSource = FlagUri };
            }
        }

        private Uri FlagUri
        {
            get
            {
                string uriPattern = "/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/countries/{0}.jpg";
                if (string.IsNullOrEmpty(_flagSource))
                {
                    return new Uri(string.Format(uriPattern, Name.Substring(0, 3).ToLower()), UriKind.Relative);
                }
                else
                {
                    return new Uri(String.Format(uriPattern, _flagSource.ToLower()), UriKind.Relative);
                }
            }
        }

        public string Name { get; set; }

        #endregion

        #region ** static

        public static IList<Country> List
        {
            get
            {
                ObservableCollection<Country> countries = new ObservableCollection<Country>();
                foreach (var name in Country.CountryNames)
                {
                    countries.Add(new Country() { Name = name });
                }
                return countries;
            }
        }

        public static List<string> CountryNames = new List<string>(new string[] { 
            "Argentina",
            "Australia",
            "Brazil",
            "Canada",
            "China",
            "England",
            "Finland",
            "France",
            "Germany",
            "India",
            "Israel",
            "Ireland",
            "Italy",
            "Japan",
            "Mexico",
            "Norway",
            "New Zealand",
            "Russia",
            "Singapore",
            "South Korea",
            "Spain",
            "Sweeden",
            "Turkey",
            "United States",
            "Uruguay"
        });

        #endregion

        public override bool Equals(object obj)
        {
            Country c = obj as Country;
            if (c == null) return false;

            return (string.Compare(c.Name, Name, StringComparison.InvariantCultureIgnoreCase) == 0);
        }
    }
 

    /// <summary>
    /// Converted used to for AutoComplete and edition features of the <see cref="C1ComboBox"/>
    /// </summary>
    public class CountryConverter
        : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value == null) return string.Empty;

            return ((Country)value).Name;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string text = (string)value;

            if (Country.CountryNames.Contains(text))
            {
                return Country.List.First<Country>(new Func<Country, bool>((c) => { return c.Name == text; }));
            }
            else
            {
                return new Country("no-image") { Name = text };
            }
        }

    }

}
