using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;

namespace RowDetailsInRadWindow
{
	public partial class MainPage : UserControl
	{
		RadWindow window;
		public MainPage()
		{
			InitializeComponent();
			this.window = new RadWindow();
			this.window.Content = new DetailsPresenter()
			{
				// Link the external details presenter to our RadGridView.
				DetailsProvider = this.clubsGrid.RowDetailsProvider
			};
			this.window.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner;
			this.window.Header = "Players";
			this.clubsGrid.RowDetailsProvider.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(RowDetailsProvider_PropertyChanged);
		}

		void RowDetailsProvider_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "DataContext")
			{				
				Club currentClub = this.clubsGrid.RowDetailsProvider.DataContext as Club;
				if (currentClub != null)
				{					
					var row = this.clubsGrid.ItemContainerGenerator.ContainerFromItem(currentClub) as GridViewRow;			
					this.window.Show();
				}
				else
				{					
					this.window.Close();
				}
			}
		}
		private void RadButton_Click(object sender, RoutedEventArgs e)
		{
			Club displayedClub = this.clubsGrid.RowDetailsProvider.DataContext as Club;
			var index = this.clubsGrid.Items.IndexOf(displayedClub);
			if(index >0)
			{
				Club previousClub = this.clubsGrid.Items[index - 1] as Club;
				if (previousClub != null)
				{
					var row = this.clubsGrid.ItemContainerGenerator.ContainerFromItem(previousClub) as GridViewRow;
					row.IsSelected = true;
				}
			}
			else
			{
				MessageBox.Show("There is no previous item to be displayed");
			}
		
		}

		private void RadButton_Click_1(object sender, RoutedEventArgs e)
		{
			Club displayedClub = this.clubsGrid.RowDetailsProvider.DataContext as Club;
			var index = this.clubsGrid.Items.IndexOf(displayedClub);
			if (index < this.clubsGrid.Items.Count-1)
			{
				Club previousClub = this.clubsGrid.Items[index + 1] as Club;
				if (previousClub != null)
				{
					var row = this.clubsGrid.ItemContainerGenerator.ContainerFromItem(previousClub) as GridViewRow;
					row.IsSelected = true;
				}
			}
			else
			{
				MessageBox.Show("There is no next item to be displayed");
			}
		}
		
	}
}
