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.Printing;
using System.ComponentModel;
using C1.Silverlight.FlexGrid;

namespace Printing
{
    public partial class MainPage : UserControl
    {
        ICollectionView _data = Product.GetProducts(30);

        public MainPage()
        {
            InitializeComponent();

            // populate grids
            _flex.ItemsSource = _data;
            _flex.Columns["Line"].AllowMerging = true;
            _flex.Columns["Color"].AllowMerging = true;
            _flex2.ItemsSource = _data;

            // for testing
            _flex.AllowResizing = AllowResizing.Both;
        }

        #region ** event handlers

        // toggle grouping
        void _chkGroup_Click(object sender, RoutedEventArgs e)
        {
            using (_data.DeferRefresh())
            {
                var gd = _data.GroupDescriptions;
                gd.Clear();
                if (_chkGroup.IsChecked.Value)
                {
                    _data.GroupDescriptions.Add(new System.Windows.Data.PropertyGroupDescription("Line"));
                    _data.GroupDescriptions.Add(new System.Windows.Data.PropertyGroupDescription("Color"));
                }
            }
        }

        // toggle merging
        void _chkMerge_Click(object sender, RoutedEventArgs e)
        {
            _flex.AllowMerging = _chkMerge.IsChecked.Value ? AllowMerging.Cells : AllowMerging.None;
        }

        // toggle freezing
        void _chkFreeze_Click(object sender, RoutedEventArgs e)
        {
            if (_chkFreeze.IsChecked.Value)
            {
                _flex.Rows.Frozen = Math.Min(12, _flex.Selection.Row);
                _flex.Columns.Frozen = Math.Min(2, _flex.Selection.Column); ;
            }
            else
            {
                _flex.Rows.Frozen = 0;
                _flex.Columns.Frozen = 0;
            }
        }
        #endregion

        #region ** printing

        // advanced printing
        void _btnPrintGrids_Click(object sender, RoutedEventArgs e)
        {
            var pd = new System.Windows.Printing.PrintDocument();
            pd.BeginPrint += pd_BeginPrint;
            pd.PrintPage += pd_PrintPage;
            pd.EndPrint += pd_EndPrint;
            pd.Print("C1FlexGrid");
        }

        List<FrameworkElement> _pages;
        double _margin;
        ScaleMode _scaleMode;
        int _currentPage;
        void pd_BeginPrint(object sender, BeginPrintEventArgs e)
        {
            _pages = null;

            // get margins, scale mode
            _margin =
                _cmbMargins.SelectedIndex == 0 ? 0 :
                _cmbMargins.SelectedIndex == 1 ? 96.0 / 2 :
                96.0;
            _scaleMode =
                _cmbZoom.SelectedIndex == 0 ? ScaleMode.ActualSize :
                _cmbZoom.SelectedIndex == 1 ? ScaleMode.PageWidth :
                ScaleMode.SinglePage;
        }
        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            if (_pages == null)
            {
                // calculate page size, discounting margins
                var sz = e.PrintableArea;
                sz.Width -= 2 * _margin;
                sz.Height -= 2 * _margin;

                // generate images for every page
                _currentPage = 0;
                _pages = new List<FrameworkElement>();

                // first grid
                foreach (var page in _flex.GetPageImages(_scaleMode, sz, 100))
                {
                    _pages.Add(page);
                }

                // second grid
                foreach (var page in _flex2.GetPageImages(_scaleMode, sz, 100))
                {
                    _pages.Add(page);
                }
            }

            // create page element
            var pageTemplate = new PageTemplate();

            // set margins
            pageTemplate.SetPageMargin(new Thickness(_margin));

            // set content
            pageTemplate.PageContent.Child = _pages[_currentPage];
            pageTemplate.PageContent.Stretch = _scaleMode == ScaleMode.ActualSize
                ? System.Windows.Media.Stretch.None
                : System.Windows.Media.Stretch.Uniform;

            // set footer text
            pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}", 
                _currentPage + 1, _pages.Count);

            // render this page
            e.PageVisual = pageTemplate;

            // move on to next page
            _currentPage++;
            e.HasMorePages = _currentPage < _pages.Count;
        }
        void pd_EndPrint(object sender, EndPrintEventArgs e)
        {
            _pages = null;
        }
        #endregion
    }
}
