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 System.Windows.Threading;

namespace CollectionViewDemo2010
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            IList<string> fieldNames = new string[] { "Description", "ID", "Company", "Location" };
            sortGroupFields.FieldNames = fieldNames;
            sortGroupFields.Select("Description");
            sortGroupFields.Select("Company");

        }

        private void runButton_Click(object sender, RoutedEventArgs e)
        {
            dataGridMS.ItemsSource = null;
            dataGridC1.ItemsSource = null;
            msTimeTextBlock.Text = "";
            c1TimeTextBlock.Text = "";
            cmpTextBlock.Text = "";
            runButton.IsEnabled = false;
            
            Cursor prevCursor = Cursor;
            Cursor = Cursors.Wait;
            //Use DispatcherTimer in order to allow UI update before the test execution.
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += (s, ea) =>
                {
                    timer.Stop();
                    try
                    {
                        //Microsoft CollectionView
                        System.ComponentModel.ICollectionView msCv = TestData.GetData((int)rowCountNB.Value);
                        int msStart = Environment.TickCount;
                        TestData.SetupCV(msCv, sortGroupFields.SelectedFieldNames);
                        double msTime = (double)(Environment.TickCount - msStart) / 1000.0;
                        msTimeTextBlock.Text = msTime.ToString();

                        //ComponentOne CollectionView
                        System.ComponentModel.ICollectionView c1Cv = TestData.GetC1Data((int)rowCountNB.Value);
                        int c1Start = Environment.TickCount;
                        TestData.SetupCV(c1Cv, sortGroupFields.SelectedFieldNames);
                        double c1Time = (double)(Environment.TickCount - c1Start) / 1000.0;
                        c1TimeTextBlock.Text = c1Time.ToString();

                        dataGridMS.ItemsSource = msCv;
                        dataGridC1.ItemsSource = c1Cv;

                        cmpTextBlock.Text = string.Format("C1CollectionView is {0:N2} times faster.", msTime / c1Time);
                    }
                    finally
                    {
                        Cursor = prevCursor;
                        runButton.IsEnabled = true;
                    }
                };

            timer.Interval = TimeSpan.FromMilliseconds(500);
            timer.Start();

        }

    }
}
