using System.Linq;
using System.Windows.Data;
using C1.Silverlight.DataGrid;
using System.Reflection;

namespace C1DataGrid_Demo2010
{
    public static class Common
    {

        // *******************************************************************
        // common code for Column Auto-generation
        // i.e. creation of combo columns (we show the same data in all the grids)
        // *******************************************************************
        public static void HandleColumnAutoGeneration(DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.Name == "ProductModelID")
            {
                var comboCol = new DataGridComboBoxColumn(e.Property);
                var models = Data.GetModels().OrderBy(m => m.Name).ToList();
                models.Insert(0, new Model { Name = "Not specified", ProductModelID = 0 });
                comboCol.ItemsSource = models;
                comboCol.DisplayMemberPath = "Name";
                comboCol.SelectedValuePath = "ProductModelID";
                e.Column = comboCol;
            }
            if (e.Property.Name == "ImageUrl")
            {
                var imageColumn = new DataGridImageColumn(e.Property);
                imageColumn.SortMemberPath = "ImageUrl";
                imageColumn.Binding.Converter = new ImageSourceConverter();
                imageColumn.Width = new DataGridLength(85);
                imageColumn.GroupContentConverter = new ImageSourceConverter();
                e.Column = imageColumn;
            }
            if (e.Property.Name == "ExpirationDate")
            {
                var dateTimeColumn = e.Column as DataGridDateTimeColumn;
                //(e.Column as DataGridDateTimeColumn).EditMode = DataGridDateTimeColumnEditMode.Custom;
                //(e.Column as DataGridDateTimeColumn).FirstDayOfWeek = System.DayOfWeek.Wednesday;
                //(e.Column as DataGridDateTimeColumn).DateFormat = System.Windows.Controls.DatePickerFormat.Long;
                //(e.Column as DataGridDateTimeColumn).TimeFormat = C1.Silverlight.DateTimeEditors.C1TimeEditorFormat.ShortTime;
                dateTimeColumn.GroupConverter = new OutlookDateGroupConverter();
                dateTimeColumn.GroupContentConverter = new OutlookDateGroupNameConverter();
                dateTimeColumn.Format = "d";
            }
            if (e.Property.Name == "Name")
            {
                IValueConverter converter = new AlphabeticTextGroupConverter();
                e.Column.GroupConverter = converter;
            }
            if (e.Property.Name == "ProductNumber")
            {
                e.Column.GroupConverter = new ProductGroupConverter();
                e.Column.GroupContentConverter = new ProductGroupNameConverter();
                (e.Column as DataGridTextColumn).MaxLength = 10;
            }
            if (e.Property.Name == "StandardCost" && e.Column is DataGridBoundColumn)
            {
                e.Column.GroupConverter = new NumberRangeGroupConverter();
                e.Column.GroupContentConverter = new NumberRangeGroupNameConverter();
                ((DataGridBoundColumn)e.Column).Format = "C";
            }
        }
    }
}
