using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.Data;
using System.Data.OleDb;

namespace CustomSorting
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        DataView employees;
        DataView customers;

        List<Telerik.Windows.Controls.GridViewColumn> employeesColumns = new List<Telerik.Windows.Controls.GridViewColumn>();
        List<Telerik.Windows.Controls.GridViewColumn> customersColumns = new List<Telerik.Windows.Controls.GridViewColumn>();

        Telerik.Windows.Controls.GridViewColumn previouslySortedColumn;

        public Window1()
        {
            InitializeComponent();
            PopulateRadGridView();
        }

        private void InitializeData()
        {
            NwindDataSet dataSet = new NwindDataSet();

            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Nwind.mdb;Persist Security Info=True";
            OleDbConnection connection = new OleDbConnection(connString);
            OleDbDataAdapter employeesAdapter = new OleDbDataAdapter("SELECT * FROM [employees]", connection);
            OleDbDataAdapter customersAdapter = new OleDbDataAdapter("SELECT * FROM [customers]", connection);

            employeesAdapter.Fill(dataSet, "Employees");
            customersAdapter.Fill(dataSet, "Customers");

            employees = dataSet.Employees.DefaultView;
            customers = dataSet.Customers.DefaultView;
        }

        private void PopulateRadGridView()
        {
            this.InitializeData();

            InitializeEmployeesColumns();

            InitializeCustomersColumns();

            this.radGridView1.ItemsSource = employees;
        }

        private void InitializeEmployeesColumns()
        {
            GridViewDataColumn column;

            column = new GridViewDataColumn();
            column.UniqueName = "LastName";
            column.HeaderText = column.UniqueName;
            employeesColumns.Add(column);

            column = new GridViewDataColumn();
            column.UniqueName = "FirstName";
            column.HeaderText = column.UniqueName;
            employeesColumns.Add(column);

            column = new GridViewDataColumn();
            column.UniqueName = "Title";
            column.HeaderText = column.UniqueName;
            employeesColumns.Add(column);

            column = new GridViewDataColumn();
            column.UniqueName = "City";
            column.HeaderText = column.UniqueName;
            employeesColumns.Add(column);
        }

        private void InitializeCustomersColumns()
        {
            GridViewDataColumn column;

            column = new GridViewDataColumn();
            column.UniqueName = "CompanyName";
            column.HeaderText = column.UniqueName;
            customersColumns.Add(column);

            column = new GridViewDataColumn();
            column.UniqueName = "ContactName";
            column.HeaderText = column.UniqueName;
            customersColumns.Add(column);

            column = new GridViewDataColumn();
            column.UniqueName = "City";
            column.HeaderText = column.UniqueName;
            customersColumns.Add(column);

            column = new GridViewDataColumn();
            column.UniqueName = "Country";
            column.HeaderText = column.UniqueName;
            customersColumns.Add(column);
        }

        private void radGridView1_SortingRequested(object sender, SortingRequestedEventArgs e)
        {
            if (this.previouslySortedColumn != null)
            {
                previouslySortedColumn.SortingState = SortingState.None;
            }

            DataView currentSource = (DataView) radGridView1.ItemsSource;
            currentSource.Sort = e.FieldDescriptor.UniqueName + " DESC";

            if (e.SortOrder == SortingState.None)
            {
                e.SortOrder = SortingState.Ascending;
            }
            else if (e.SortOrder == SortingState.Ascending)
            {
                e.SortOrder = SortingState.Descending;
            }
            else
            {
                e.SortOrder = SortingState.None;
            }

            this.previouslySortedColumn = (Telerik.Windows.Controls.GridViewColumn) e.FieldDescriptor;

            e.Handled = true;
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.previouslySortedColumn = null;

            switch (tabControl1.SelectedIndex)
            {
                case 0:
                    radGridView1.Columns.Clear();
                    radGridView1.Columns.AddRange(employeesColumns);
                    radGridView1.ItemsSource = employees;
                    break;
                case 1:
                    radGridView1.Columns.Clear();
                    radGridView1.Columns.AddRange(customersColumns);
                    radGridView1.ItemsSource = customers;
                    break;
                default:
                    radGridView1.ItemsSource = employees;
                    break;
            }
        }
    }
}
