using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using System.Linq;
using System.Linq.Dynamic;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Dictionary<string, RadSortOrder> sortedColumns = new Dictionary<string, RadSortOrder>();
        Dictionary<RadSortOrder, string> sortDirections = new Dictionary<RadSortOrder, string>();

        int pageSize = 10;
        int currentPageIndex = 0;
        int totalPages = 100000 - 1;
        string orderBy = "";
        string where = "";

        public Form1()
        {
            InitializeComponent();

            sortDirections.Add(RadSortOrder.Ascending, "ASC");
            sortDirections.Add(RadSortOrder.Descending, "DESC");
        }

        private void OnFormLoad(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            this.radGridView1.GridElement.BeginUpdate();
            
            IQueryable queryable = new DataClasses1DataContext().MyTables.AsQueryable();

            if (!String.IsNullOrEmpty(where))
            {
                queryable = queryable.Where(where);
            }

            if (!String.IsNullOrEmpty(orderBy))
            {
                queryable = queryable.OrderBy(orderBy);
            }

            radGridView1.DataSource = queryable.Skip(currentPageIndex * pageSize).Take(pageSize);

            this.radGridView1.GridElement.EndUpdate(true);

            EnableDisablePager();
        }

        private void Page(object sender, EventArgs e)
        {
            if (sender == FirstPage)
            {
                currentPageIndex = 0;
            }
            else if (sender == PrevPage)
            {
                currentPageIndex--;
            }
            else if (sender == NextPage)
            {
                currentPageIndex++;
            }
            else
            {
                currentPageIndex = totalPages;
            }

            BindGrid();
        }

        private void Filter(object sender, EventArgs e)
        {
            string text = FilterTextBox.Text;
            where = (text == "") ? "" : String.Format(@"Name.Contains(""{0}"")", text);

            BindGrid();
        }

        private void radGridView1_SortChanging(object sender, GridViewCollectionChangingEventArgs e)
        {
            string fieldName = ((Telerik.WinControls.UI.GridViewDataColumn)(e.GridViewTemplate.CurrentColumn)).FieldName;

            if (!sortedColumns.ContainsKey(fieldName))
            {
                sortedColumns.Add(fieldName, RadSortOrder.Ascending);
            }
            else
            {
                RadSortOrder currentState = sortedColumns[fieldName];
                sortedColumns[fieldName] =
                    (currentState == RadSortOrder.Ascending) ? RadSortOrder.Descending : RadSortOrder.Ascending;
            }

            string direction = sortDirections[sortedColumns[fieldName]];
            orderBy = String.Format("{0} {1}", fieldName, direction);

            e.Cancel = true;

            currentPageIndex = 0;

            BindGrid();
        }

        private void EnableDisablePager()
        {
            FirstPage.Enabled = PrevPage.Enabled = currentPageIndex > 0;
            NextPage.Enabled = LastPage.Enabled = currentPageIndex < totalPages;
		}
    }
}
