using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using C1.Silverlight.FlexGrid;

namespace MainTestApplication
{
    public partial class Selection : UserControl
    {
        public Selection()
        {
            InitializeComponent();
            // create "Customers" data view and bind it to grid
            BindGrid();
            // store sample description in Tag property
            Tag =
                "The C1FlexGrid has a rich selection model controlled by the SelectionMode property. " +
                "You can set the selection mode to single cell, cell range (block of adjacent cells), " +
                "single row, row range, or list box (not necessarily contiguous). Regardless of the " +
                "selection mode, the C1FlexGrid always returns the selected cells as a CellRange " +
                "object so you can easily monitor and work with the selected data.";
        }

        private void BindGrid()
        {
            // create PagedCollectionView used as a data source for the
            // FlexGrid and for the MS DataGrid
            var data = Customer.GetCustomerList(100);
            var view = new PagedCollectionView(data);
            using (view.DeferRefresh())
            {
                view.GroupDescriptions.Clear();
                view.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
                view.GroupDescriptions.Add(new PropertyGroupDescription("Active"));
                var gd = view.GroupDescriptions[0] as PropertyGroupDescription;
                gd.Converter = new CountryInitialConverter();
            }

            // bind grids to PagedCollectionView
            _flexSelectionMode.ItemsSource = view;
        }

        // change selection mode
        private void _cmbSelectionMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_cmbSelectionMode != null)
            {
                _flexSelectionMode.SelectionMode = (C1.Silverlight.FlexGrid.SelectionMode)_cmbSelectionMode.SelectedIndex;
            }
        }

        // turn merging on and off
        private void _chkAllowMergingSelMode_Click(object sender, RoutedEventArgs e)
        {
            var f = _flexSelectionMode;
            if (f != null)
            {
                var allowMerging = ((CheckBox)sender).IsChecked.Value;
                f.AllowMerging = allowMerging
                    ? AllowMerging.Cells
                    : AllowMerging.None;
                f.Columns["Country"].AllowMerging = allowMerging;
                f.Columns["First"].AllowMerging = allowMerging;
                f.Columns["Last"].AllowMerging = allowMerging;
            }
        }

        private void _flexSelectionMode_SelectionChanged(object sender, CellRangeEventArgs e)
        {
            var fg = _flexSelectionMode;
            var customers = fg.SelectedItems;
            _lblSelState.Text = string.Format("{0} item{1} selected, {2} active, total weight: {3:n2}",
                customers.Count,
                customers.Count == 1 ? string.Empty : "s",
                (from Customer c in customers where c.Active select c).Count(),
                (from Customer c in customers select c.Weight).Sum());
         }
    }
}
