using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.DataGrid;
using C1.Silverlight.Data;
using C1.Silverlight;
using C1.Silverlight.Toolbar;
using System.Windows.Controls.Primitives;

namespace C1DataGrid_Demo2010
{
    public partial class AdHocMerging : UserControl
    {
        MergerAdHoc _merger = new MergerAdHoc();

        private C1ToolbarCommand MergeCommand { get { return Resources["cmdMergeCells"] as C1ToolbarCommand; } }
        private C1ToolbarCommand UnmergeCommand { get { return Resources["cmdUnmergeCells"] as C1ToolbarCommand; } }

        public AdHocMerging()
        {
            InitializeComponent();

            // initialize merging
            grid.MergingCells += new EventHandler<DataGridMergingCellsEventArgs>(grid_MergingCells);

            grid.ItemsSource = Data.GetData(false);

            // initialize toolbar commands
            CommandManager.RegisterClassCommandBinding(GetType(),
                new CommandBinding(MergeCommand, (s, e) => Merge(), (s, e) => e.CanExecute = CanMerge()));            
            CommandManager.RegisterClassCommandBinding(GetType(),
                new CommandBinding(UnmergeCommand, (s, e) => Unmerge(), (s, e) => e.CanExecute = CanUnmerge()));
            grid.SelectionChanged += (s, e) => CommandManager.InvalidateRequerySuggested();
        }



        void grid_MergingCells(object sender, DataGridMergingCellsEventArgs e)
        {
            if (_merger == null)
                return;

            foreach (var range in _merger.MergedRanges)
            {
                e.Merge(range);
            }
        }

        #region Unmerge

        private void Unmerge()
        {
            if (!CanUnmerge()) 
            {
                C1MessageBox.Show("Cannot unmerge the selection", "Error", C1MessageBoxButton.OK, C1MessageBoxIcon.Error);
                return;
            }

            var range = grid.Selection.SelectedRanges[0];
            _merger.UnmergeRange(range);
            grid.ReMerge();
            CommandManager.InvalidateRequerySuggested();
        }

        private bool CanUnmerge()
        {
            return ((grid.Selection.SelectedRanges.Count == 1)
                    && (_merger.CanUnmergeRange(grid.Selection.SelectedRanges[0])));
        }

        #endregion

        #region Merge

        private void Merge()
        {
            if (!CanMerge()) 
            {
                C1MessageBox.Show("Cannot merge the selection", "Error",  C1MessageBoxButton.OK, C1MessageBoxIcon.Error);
                return;
            }

            var range = grid.Selection.SelectedRanges[0];
            _merger.MergeRange(range);
            grid.ReMerge();
            CommandManager.InvalidateRequerySuggested();
        }

        private bool CanMerge()
        {
            return ((grid.Selection.SelectedRanges.Count == 1)
                    && _merger.CanMergeRange(grid.Selection.SelectedRanges[0]));
        }

        #endregion
    }
}
