using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using C1.Silverlight.DataGrid;
using C1DataGrid_WCF_DS.AdventureWorksDataService;

namespace C1DataGrid_WCF_DS
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            
            // Initialize WCF Data Context
            Uri serviceUri = C1.Silverlight.Extensions.GetAbsoluteUri("AdventureWorksDataService.svc");
            AdventureWorksEntities  awDataContext = new AdventureWorksEntities(serviceUri);
            awDataContext.MergeOption = MergeOption.OverwriteChanges;

            // Attach to DataGrid events and load data
            _dataGrid.CanUserGroup = true;
            _dataGrid.CommittingNewRow += _dataGrid_CommittingNewRow;
            _dataGrid.CancelingNewRow += _dataGrid_CancelingNewRow;
            _dataGrid.BeginningNewRow += _dataGrid_BeginningNewRow;
            _dataGrid.ItemsSource = new WCFCollectionView(awDataContext, _dataGrid);
        }

        private void _dataGrid_BeginningNewRow(object sender, DataGridBeginningNewRowEventArgs e)
        {
            Product newProduct = e.Item as Product;
            newProduct.SellStartDate = DateTime.Now;
            newProduct.ModifiedDate = DateTime.Now;
            newProduct.rowguid = System.Guid.NewGuid();
        }

        private void _dataGrid_CancelingNewRow(object sender, DataGridEndingNewRowEventArgs e)
        {
            _validationArea.Text = string.Empty;
            _validationArea.Visibility = Visibility.Collapsed;
        }

        private void _dataGrid_CommittingNewRow(object sender, DataGridEndingNewRowEventArgs e)
        {
            Product newProduct = e.NewRow.DataItem as Product;
            string validationMessage = string.Empty;
            if (string.IsNullOrEmpty(newProduct.ProductNumber))
            {
                validationMessage += "Product Number cannot be empty\n";
            }
            if (string.IsNullOrEmpty(newProduct.Name))
            {
                validationMessage += "Product Name cannot be empty\n";
            }
            if ((newProduct.SellStartDate < new DateTime(2000, 1, 1)) ||
                (newProduct.SellStartDate > new DateTime(2010, 12, 31)))
            {
                validationMessage += "Sell Start Date must be greater than 2000/01/01 and less than 2010/12/31\n";
            }
            if (string.IsNullOrEmpty(validationMessage))
            {
                _validationArea.Visibility = Visibility.Collapsed;
            }
            else
            {
                _validationArea.Text = validationMessage;
                _validationArea.Visibility = Visibility.Visible;
                e.Cancel = true;
            }
        }

        private void _dataGrid_SortChanging(object sender, C1.Silverlight.CancelEventArgs e)
        {
            _dataGrid.IsLoading = true;
        }

        private void _dataGrid_SortChanged(object sender, DataGridSortChangedEventArgs e)
        {
            _dataGrid.IsLoading = false;
        }

        private void _dataGrid_FilterChanging(object sender, DataGridFilterChangingEventArgs e)
        {
            _dataGrid.IsLoading = true;
        }

        private void _dataGrid_FilterChanged(object sender, DataGridFilterChangedEventArgs e)
        {
            _dataGrid.IsLoading = false;
        }

        private void _dataGrid_GroupChanging(object sender, DataGridGroupChangingEventArgs e)
        {
            _dataGrid.IsLoading = true;
        }

        private void _dataGrid_GroupChanged(object sender, DataGridGroupChangedEventArgs e)
        {
            _dataGrid.IsLoading = false;
        }
    }
}
