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 Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.Windows.Threading;

namespace RadGridView_WPF_NoXAML_1
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{

		public MainWindow()
		{
			InitializeComponent();
			clubsGrid.KeyboardCommandProvider = new CustomProvider(clubsGrid, this);
		}

		public class CustomProvider : DefaultKeyboardCommandProvider
		{
			private GridViewHeaderCell focusedCell = null;
			private FrameworkElement focusScope = null;
			private GridViewHeaderRow headerRow = null;
			private RadGridView grid = null;

			public CustomProvider(RadGridView grid, FrameworkElement focusScope)
				: base(grid)
			{
				this.grid = grid;
				this.focusScope = focusScope;
				this.grid.Dispatcher.BeginInvoke(new Action(() => { this.headerRow = grid.ChildrenOfType<GridViewHeaderRow>().FirstOrDefault(); }), DispatcherPriority.Loaded);
			}

			public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
			{
				var pendingCommands = base.ProvideCommandsForKey(key).ToList();

                // Navigate forward
                if (key == Key.Tab && Keyboard.Modifiers != ModifierKeys.Shift)
                {
                    var focusedBorder = FocusManager.GetFocusedElement(focusScope) as Border;
                    if (focusedBorder != null && focusedBorder.Name == "FocusableSort")
                    {
                        focusedCell = focusedBorder.ParentOfType<GridViewHeaderCell>();
                    }

                    var focusedGridViewCell = FocusManager.GetFocusedElement(focusScope) as GridViewCell;

                    // If we are on the last gridviewheadercell or on a GridViewCell, we continue navigating through the GridViewCells
                    if (headerRow.Cells.IndexOf(focusedCell) == headerRow.Cells.Count - 1 || focusedGridViewCell != null)
                    {
                        return pendingCommands;
                    }

                    pendingCommands.Clear();
                }
                // Navigate backwards
                else if (key == Key.Tab && Keyboard.Modifiers == ModifierKeys.Shift)
                {
                    var focusedGridViewCell = FocusManager.GetFocusedElement(focusScope) as GridViewCell;

                    // We are on the first cell of the first column and we focus the Border in the last GridViewHeaderCell
                    if (focusedGridViewCell != null && focusedGridViewCell.Column.DisplayIndex == 0 && this.grid.Items.IndexOf(focusedGridViewCell.DataContext) == 0)
                    {
                        // If we have a lot of columns and the last column is not in the viewport, we should make sure to bring it into view first
                        var lastBorder = this.headerRow.Cells.Last().ChildrenOfType<Border>().FirstOrDefault(b => b.Name == "FocusableSort");
                        lastBorder.Focus();
                        pendingCommands.Clear();
                    }
                }
				return pendingCommands;
			}
            
		}
	}
}
