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;
using C1.WPF.DataGrid.Summaries;

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

            DataContext = GetProducts();
            Loaded += new RoutedEventHandler(GroupSummaryPage_Loaded);
        }

        void GroupSummaryPage_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var column in grid.Columns)
            {
                if (column.GroupState.Direction != DataGridSortDirection.None)
                {
                    cmbColumns.SelectedItem = column;
                    break;
                }
            }
            cmbColumns.SelectionChanged += (s, args) =>
            {
                grid.GroupBy(args.AddedItems[0] as C1.WPF.DataGrid.DataGridColumn);
            };
        }

        #region ** data

        public static IEnumerable<Product> 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));
        }

        #endregion

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

            if (e.Property.Name == "StandardCost")
            {
                DataGridAggregate.SetAggregateFunctions(e.Column, new DataGridAggregatesCollection { new DataGridAggregateSum() });
            }
        }


        private void grid_AutoGeneratedColumns(object sender, EventArgs e)
        {
            grid.Columns["ImageUrl"].DisplayIndex = 0;
            grid.Columns["StandardCost"].DisplayIndex = 6;
            grid.GroupBy(grid.Columns["ExpirationDate"]);
        }

        #region ** expand / collapse

        private void btnExpand_Click(object sender, RoutedEventArgs e)
        {
            grid.Rows.ExpandAll();
        }

        private void btnCollapse_Click(object sender, RoutedEventArgs e)
        {
            grid.Rows.CollapseAll();
        }

        #endregion

        #region ** new row

        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";
        }

        #endregion
    }


    public class Models : List<Model>
    {
        public Models()
        {
            foreach (var model in Data.GetModels())
            {
                Add(model);
            }
        }
    }
}
