using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using C1.Silverlight;
using C1.Silverlight.DataGrid;

namespace C1DataGrid_Demo2010
{
    public partial class AsyncValidation : System.Windows.Controls.UserControl
    {
        public AsyncValidation()
        {
            InitializeComponent();

            grid.ItemsSource = GetOrders();

            //var converter = CustomConverter.Create((value, type, parameter, culture) =>
            //    {
            //        return value;
            //    });
        }

        private static IEnumerable<Order> GetOrders()
        {
            var products = Data.GetProducts().ToArray();
            var orders = new List<Order>();
            var rand = new Random();
            for (int i = 0; i < 50; i++)
            {
                orders.Add(new Order(products[i].ProductNumber)
                {
                    Created = DateTime.Now - new TimeSpan(rand.Next(1, 10), 0, 0, 0, 0),
                    Amount = rand.Next(1,10),
                });
            }
            return orders;
        }

        private void grid_AutoGeneratingColumn(object sender, C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.Name == "ProductNumber")
            {
                var column = new DataGridComboBoxColumn(e.Property);
                column.ItemsSource = Data.GetProducts();
                column.SelectedValuePath = "ProductNumber";
                column.DisplayMemberPath = "Name";
                e.Column = column;
            }
            if (e.Property.Name == "Created" && e.Column is DataGridBoundColumn)
            {
                ((DataGridBoundColumn)e.Column).Format = "d";
            }
            if (e.Property.Name == "Amount")
            {
                e.Column.Width = DataGridLength.Auto;
            }
        }

        private void grid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (e.Row.Type == DataGridRowType.Item)
            {
                var order = (e.Row.DataItem as Order);
                //Listen to the error changed event to update the errors in the row.
                order.ErrorsChanged += (s, a) =>
                    {
                        UpdateErrors(e.Row);
                    };
                order.CheckAvailability();
                UpdateErrors(e.Row);
            }
        }

        private static void UpdateErrors(DataGridRow row)
        {
            row.Errors.Clear();
            var order = (row.DataItem as Order);
            if (order.HasErrors)
            {
                foreach (var error in order.GetErrors(""))
                {
                    row.Errors.Add(new DataGridRowError(error.ToString()));
                }
            }
        }

    }

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if(value == null)
                return Visibility.Visible;

            return value.ToString() == "Yes" ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Convert(value, targetType, parameter, culture);
        }
    }
}
