using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using C1.WPF;
using C1.WPF.DataGrid;

namespace DataGridSamples
{
    /// <summary>
    /// Interaction logic for CustomColumns.xaml
    /// </summary>
    public partial class CustomColumns : UserControl
    {
        public CustomColumns()
        {
            InitializeComponent();

            // update composite column after all the columns were loaded
            grid.AutoGeneratedColumns += new EventHandler(grid_AutoGeneratedColumns);

            // set items source
            grid.ItemsSource = Data.GetProducts((product) => product.Element("Image") != null && product.Element("Image").Value != "no_image_available_small.jpg");
        }

        private void grid_AutoGeneratedColumns(object sender, EventArgs e)
        {
            var cc = (CompositeColumn)grid.Columns.Where(c => c is CompositeColumn).FirstOrDefault();

            if (cc != null)
            {
                // update headers
                cc.Update();

                // update data grid header height, now we know how many nested levels we have
                grid.ColumnHeaderHeight = CompositeColumn.LevelHeaderHeight * (cc.NestedLevels + 1);
            }
        }


        private void grid_AutoGeneratingColumn(object sender, C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.Name == "Description")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "ID")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "Name")
            {
                e.Column = new DataGridMultiLineTextColumn(e.Property);
                e.Column.Header = "Multi-line column";
                e.Column.VerticalAlignment = VerticalAlignment.Top;
            }
            if (e.Property.Name == "ImageUrl")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "ProductSubcategoryID")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "Available")
            {
                e.Cancel = true;
            }
            if (e.Property.Name == "ExpirationDate")
            {
                e.Cancel = true;
                //e.Column.Format = "MM/dd/yy HH:mm:ss";
                //(e.Column as DataGridDateTimeColumn).DateFormat = 
                //(e.Column as DataGridDateTimeColumn).TimeFormat = C1TimeEditorFormat.ShortTime;
            }
            if (e.Property.Name == "StandardCost")
            {
                e.Column = new DataGridCustomNumericColumn(e.Property);
                TextBlock _txtBlock = new TextBlock()
                {
                    Text = "Std. Cost (tooltip)",
                    HorizontalAlignment = HorizontalAlignment.Right,
                    VerticalAlignment = VerticalAlignment.Center
                };
                ToolTipService.SetToolTip(_txtBlock, new ToolTip() { Content = "It's a custom tooltip." });
                e.Column.Header = _txtBlock;
            }
            if (e.Property.Name == "ProductNumber")
            {
                var column = new DataGridMaskedTextColumn(e.Property);
                column.Mask = "aa-aaaa-aa";
                column.TextMaskFormat = MaskFormat.IncludeLiterals;
                e.Column = column;
            }
            if (e.Property.Name == "ProductModelID")
            {
                var comboCol = new C1.WPF.DataGrid.DataGridComboBoxColumn(e.Property);
                comboCol.DisplayMemberPath = "Name";
                comboCol.SelectedValuePath = "ProductModelID";
                comboCol.ItemsSource = Data.GetModels();
                e.Column = comboCol;
            }
            if (e.Property.Name == "Color")
            {
                e.Column = new DataGridColorColumn(e.Property);
            }
        }

        private void CheckBox_Loaded(object sender, RoutedEventArgs e)
        {
            CheckBox chk = (sender as CheckBox);
            foreach (Product product in grid.ItemsSource)
            {
                product.PropertyChanged += (s, e2) =>
                {
                    if (e2.PropertyName == "Available")
                    {
                        if ((s as Product).Available != chk.IsChecked)
                        {
                            chk.IsChecked = null;
                        }
                    }
                };
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chk = (sender as CheckBox);
            foreach (Product product in grid.ItemsSource)
            {
                product.Available = chk.IsChecked;
            }
        }

        private void grid_LoadedRowPresenter(object sender, C1.WPF.DataGrid.DataGridRowEventArgs e)
        {
            //e.Row.HeaderPresenter.Content = (e.Row.DisplayIndex + 1).ToString();
        }

        private void grid_KeyDown(object sender, KeyEventArgs e)
        {
            MyNavigationStrategy navigationStrategy = new MyNavigationStrategy(grid);
            navigationStrategy.HandleKeyDown(e);
        }
    }
}
