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

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

            DataContext = Data.GetProducts((product) => product.Element("Image") != null && product.Element("Image").Value != "no_image_available_small.jpg");

            if (grid.NewRowVisibility == DataGridNewRowVisibility.Top)
            {
                rbnTop.IsChecked = true;
            }
            else
            {
                rbnBottom.IsChecked = true;
            }
            rbnTop.Checked += (s, e) =>
            {
                UpdateNewRowVisibility();
            };
            rbnBottom.Checked += (s, e) =>
            {
                UpdateNewRowVisibility();
            };
            UpdateNewRowVisibility();
        }

        private void UpdateNewRowVisibility()
        {
            if (rbnTop.IsChecked.Value)
            {
                grid.NewRowVisibility = DataGridNewRowVisibility.Top;
                grid.FrozenTopRowsCount = 1;
                grid.FrozenBottomRowsCount = 0;
            }
            else
            {
                grid.NewRowVisibility = DataGridNewRowVisibility.Bottom;
                grid.FrozenTopRowsCount = 0;
                grid.FrozenBottomRowsCount = 1;
            }
        }

        private void grid_AutoGeneratingColumn(object sender, C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            Common.HandleColumnAutoGeneration(e);

            // avoid image columns for add/remove samples
            if (e.Column is DataGridImageColumn)
            {
                e.Cancel = true;
            }
        }

        private void grid_BeginningNewRow(object sender, DataGridBeginningNewRowEventArgs e)
        {
            Product product = (e.Item as Product);
            product.ExpirationDate = DateTime.Now.AddDays(7);
            product.Available = true;
            product.ImageUrl = "no_image_available_small.jpg";
        }

        private void grid_CommittingNewRow(object sender, DataGridEndingNewRowEventArgs e)
        {
            e.NewRow.Errors.Clear();
            Product product = e.NewRow.DataItem as Product;
            if (string.IsNullOrEmpty(product.ProductNumber))
            {
                e.Cancel = true;
                e.NewRow.Errors.Add(new DataGridRowError("You must enter the product number.", new List<string> { "ProductNumber" }));
            }
        }

        private void grid_CancelingNewRow(object sender, DataGridEndingNewRowEventArgs e)
        {
            e.NewRow.Errors.Clear();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            DataGridCell cell = grid.GetCellFromFrameworkElement(sender as System.Windows.Controls.Button);
            grid.RemoveRow(cell.Row.Index);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            grid.BeginNewRow();
        }

        private void grid_RowsAdded(object sender, DataGridRowsAddedEventArgs e)
        {
            //txtMsg.Foreground = new SolidColorBrush(Colors.Black);
            //txtMsg.Text = string.Format("Product } added", (e.AddedRows[0].DataItem as Product).ProductNumber);
        }

        private void grid_RowsDeleted(object sender, DataGridRowsDeletedEventArgs e)
        {
            //txtMsg.Foreground = new SolidColorBrush(Colors.Black);
            string[] products = e.DeletedRows.Select(row => (row.DataItem as Product).ProductNumber).ToArray();
            //txtMsg.Text = string.Format("Product ({0}) deleted", string.Join(", ", products));
        }

        bool deletingAccepted = false;
        private void grid_DeletingRows(object sender, DataGridDeletingRowsEventArgs e)
        {
            if (!deletingAccepted)
            {
                e.Cancel = true;
                C1MessageBox.Show(string.Format("Do you want to delete the following rows {0} ?", string.Join(",", e.DeletedRows.Select(row => (row.DataItem as Product).ProductNumber).ToArray())), "", C1MessageBoxButton.YesNo, C1MessageBoxIcon.Question, (result) =>
                {
                    if (result == MessageBoxResult.Yes)
                    {
                        deletingAccepted = true;
                        grid.RemoveRows(e.DeletedRows);
                        deletingAccepted = false;
                    }
                });
            }
        }

    }
}
