using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Data;
using System.ComponentModel;
using Telerik.Windows.Controls;

namespace GridViewTabbedNavigation
{
	/// <summary>
	/// Interaction logic for TabbedNavigation.xaml
	/// </summary>
	public partial class TabbedNavigation : UserControl
	{
		public CollectionView View
		{
			get;
			set;
		}

        public TabbedNavigation()
		{
			InitializeComponent();

			var data = new Message[]{new Message { Sender = "tom@hanna-barbera.com", Subject = "Cats are cool", Size = 100 }, new Message { Sender = "jerry@hanna-barbera.com", Subject = "Mice are cool", Size = 100 }, new Message { Sender = "spike@hanna-barbera.com", Subject = "Dogs are cool", Size = 100 }};
			this.View = new CollectionView(data);

			this.RadGridView1.SelectionChanged += new EventHandler<SelectionChangeEventArgs>(RadGridView1_SelectionChanged);
			this.View.CurrentChanged += new EventHandler(View_CurrentChanged);
			this.RadGridView1.ItemsSource = this.View;
			this.DataContext = this.View;
		}

		void View_CurrentChanged(object sender, EventArgs e)
		{
			if(this.RadGridView1.SelectedItem != this.View.CurrentItem)
				this.RadGridView1.SelectedItem = this.View.CurrentItem;
		}

		void RadGridView1_SelectionChanged(object sender, SelectionChangeEventArgs e)
		{
			if (e.AddedRecords.Count == 0)
				return;

			DataRecord record = (DataRecord)e.AddedRecords[0];

			if(this.View.CurrentItem != record.Data)
				this.View.MoveCurrentTo(record.Data);
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
		}

		private bool RecordsAvailable()
		{
			if (this.RadGridView1 != null && this.RadGridView1.Records.Count > 0)
				return true;
			else
				return false;
		}

		private bool HasNext()
		{
			if (this.View == null || this.View.CurrentPosition >= this.View.Count-1)
				return false;
			else
				return true;
		}

		private bool HasPrevious()
		{
			if (this.View == null || this.View.CurrentPosition <= 0)
				return false;
			else
				return true;
		}

		private void NextCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			if (HasNext())
				e.CanExecute = true;
			else
				e.CanExecute = false;
		}

		private void NextCommand_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			this.View.MoveCurrentToNext();
		}

		private void FirstCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void FirstCommand_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			this.View.MoveCurrentToFirst();
		}

		private void LastCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void LastCommand_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			this.View.MoveCurrentToLast();
		}

		private void PreviousCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			if (HasPrevious())
				e.CanExecute = true;
			else
				e.CanExecute = false;
		}

		private void PreviousCommand_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			this.View.MoveCurrentToPrevious();
		}
	}
}
