using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using System.Xml.Serialization;

using C1.Silverlight.DataGrid;
using C1.Silverlight.DataGrid.Filters;
using C1DataGrid_Demo2010;

namespace C1DataGrid_SaveState
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            DataContext = GetProducts();
            grid.Rows.TopRows.Add(new DataGridFilterRow() { Height = DataGridLength.Auto });
        }

        private System.Collections.IEnumerable GetProducts()
        {
            IEnumerable<Product> products = Data.GetProducts((product) => !string.IsNullOrEmpty(product.Element("ProductModelID").Value) && product.Element("Image").Value != "no_image_available_small.jpg");
            foreach (var product in products)
            {
                product.ExpirationDate = GetDate(product.StandardCost);
                product.Available = GetAvailable(product.Name);
            }
            return products;
        }

        private static bool? GetAvailable(string seed)
        {
            return (seed.Length % 3 == 0 ? (bool?)true : (seed.Length % 3 == 1 ? (bool?)false : (bool?)null));
        }

        private static DateTime? GetDate(double seed)
        {
            return new DateTime(DateTime.Now.Ticks - ((long)seed * 10000000000));
        }

        private void grid_AutoGeneratingColumn(object sender, C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.Name == "Description")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "ProductSubcategoryID")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "ID")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "ProductModelID")
            {
                var comboCol = new DataGridComboBoxColumn(e.Property);
                comboCol.Header = "Model";
                comboCol.ItemsSource = Data.GetModels();
                comboCol.DisplayMemberPath = "Name";
                comboCol.SelectedValuePath = "ProductModelID";
                e.Column = comboCol;
            }
            if (e.Property.Name == "ImageUrl")
            {
#if SL5
                string resourcesUri = "/C1DataGrid_SaveState.5;component/Resources/";
#else
                string resourcesUri = "/C1DataGrid_SaveState;component/Resources/";
#endif
                var imageColumn = new DataGridImageColumn(e.Property);
                imageColumn.SortMemberPath = "ImageUrl";
                imageColumn.Binding.Converter = new C1DataGrid_Demo2010.ImageSourceConverter();
                imageColumn.Binding.ConverterParameter = resourcesUri;
                imageColumn.Header = "Image";
                imageColumn.Width = new DataGridLength(75);
                imageColumn.GroupContentConverter = new C1DataGrid_Demo2010.ImageSourceConverter();
                imageColumn.Format = resourcesUri;
                e.Column = imageColumn;
            }
            if (e.Property.Name == "ExpirationDate" && e.Column is DataGridBoundColumn)
            {
                ((DataGridBoundColumn)e.Column).Format = "D";
                e.Column.GroupConverter = new OutlookDateGroupConverter();
                e.Column.GroupContentConverter = new OutlookDateGroupNameConverter();
            }
            if (e.Property.Name == "Name")
            {
                IValueConverter converter = new AlphabeticTextGroupConverter();
                e.Column.GroupConverter = converter;
            }
            if (e.Property.Name == "ProductNumber")
            {
                e.Column.Header = "Product Number";
                e.Column.GroupConverter = new ProductGroupConverter();
                e.Column.GroupContentConverter = new ProductGroupNameConverter();
            }
            if (e.Property.Name == "StandardCost")
            {
                e.Column.GroupConverter = new NumberRangeGroupConverter();
                e.Column.GroupContentConverter = new NumberRangeGroupNameConverter();
            }
        }
    }
}
