﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using C1.Silverlight.FlexGrid;
using System.Windows.Input;
using System;

namespace MainTestApplication
{
    public partial class Performance : UserControl
    {
        public Performance()
        {
            InitializeComponent();
            // Enter data in the [editing] grid.
            Dispatcher.BeginInvoke(PopulatePerformanceGrid);
            // Save the description of sample in the Tag property.
            Tag =
                "In this sample, you can see the scroll performance by UI virtualization. The UI virtualization is only applied to the columns in DataGrid for Standard Silverlight and hence, as the number of columns increase, the scroll performance worsens. In Flexrid for Sliverlight, the UI Virtualization is applied to both rows and columns and therefore the number of columns does not have any effect on scroll performance. In this case, the scroll performance depends only on the actual number of cells that are displayed on the screen. ";
        }

        void PopulatePerformanceGrid()
        {
            this._flexPerformance.RowHeaders.Columns.Clear();
            this._cmbColumns.SelectedIndex = 5;

            _datagridPerformance.ItemsSource = list;
            _flexPerformance.ItemsSource = list;
        }

        int columns;
        int rows = 100;

        private void _btnGenerate_Click(object sender, RoutedEventArgs e)
        {
            _flexPerformance.Columns.Clear();
        }

        List<string[]> list;

        void GenerateData()
        {
            list = new List<string[]>(rows);
            try
            {
                for (int i = 0; i < rows; i++)
                {
                    var values = new string[columns];

                    for (int j = 0; j < columns; j++)
                    {
                        values[j] = (j + 1).ToAlphabet() + (i + 1).ToString();
                    }

                    list.Add(values);
                }
            }
            catch (OutOfMemoryException)
            {
                list.Clear();
                // Out of memory
            }
        }

        private void _cmbColumns_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_cmbColumns == null)
                return;

            if (int.TryParse(((ComboBoxItem)(_cmbColumns.SelectedItem)).Content.ToString(), out columns))
            {
                GenerateData();
            }

        }

        private void _btnDgBind_Click(object sender, RoutedEventArgs e)
        {
            _btnDgBind.Cursor = Cursors.Wait;

            int t = Environment.TickCount;

            for (int i = 0; i < columns; i++)
            {
                _datagridPerformance.Columns.Add(new DataGridTextColumn()
                {
                    Header = (i + 1).ToString() + "Column",
                    Binding = new System.Windows.Data.Binding()
                    {
                        Path = new PropertyPath("[" + i + "]"),
                        Mode = System.Windows.Data.BindingMode.OneTime,
                    }
                });
            }

            _datagridPerformance.ItemsSource = list;

            Dispatcher.BeginInvoke(() => _tbDgTime.Text = ((double)(Environment.TickCount - t) / 1000).ToString("0.00"));

            _btnDgClear.Visibility = Visibility.Visible;
            _btnDgBind.Cursor = Cursors.Arrow;
        }

        private void _btnFgBind_Click(object sender, RoutedEventArgs e)
        {
            _btnFgBind.Cursor = Cursors.Wait;

            int t = Environment.TickCount;

            for (int i = 0; i < columns; i++)
            {
                _flexPerformance.Columns.Add(new Column()
                {
                    Header = (i + 1).ToString() + "Column",
                    Binding = new System.Windows.Data.Binding()
                    {
                        Path = new PropertyPath("[" + i + "]"),
                        Mode = System.Windows.Data.BindingMode.OneTime,
                    }
                });
            }

            _flexPerformance.ItemsSource = list;

            Dispatcher.BeginInvoke(() => _tbFgTime.Text = ((double)(Environment.TickCount - t) / 1000).ToString("0.00"));

            _btnFgClear.Visibility = Visibility.Visible;
            _btnFgBind.Cursor = Cursors.Arrow;
        }

        private void _btnDgClear_Click(object sender, RoutedEventArgs e)
        {
            _tbDgTime.Text = "0.00";
            _datagridPerformance.ItemsSource = null;
            _datagridPerformance.Columns.Clear();
            _btnDgClear.Visibility = Visibility.Collapsed;
        }

        private void _btnFgClear_Click(object sender, RoutedEventArgs e)
        {
            _tbFgTime.Text = "0.00";
            _flexPerformance.ItemsSource = null;
            _flexPerformance.Columns.Clear();
            _btnFgClear.Visibility = Visibility.Collapsed;
        }
    }

    static class ExtendClass
    {
        public static string ToAlphabet(this int self)
        {
            Func<int, string> f = x =>
            {
                var s = ((char)(((x == 0) ? 26 : x) + 64)).ToString();
                return (self == x) ? s : ((self - x) / 26).ToAlphabet() + s;
            };
            return (self <= 0) ? "" : f(self % 26);
        }

    }
}
