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;

namespace RadGridView_WPF_AR_6
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

			//this.clubsGrid.KeyboardCommandProvider = new CustomKeyboardCommandProvider(this.clubsGrid);

			this.clubsGrid.ItemsSource = new RadObservableCollection<Club>();

			this.clubsGrid.Loaded += (s, e) =>
			{
				this.clubsGrid.BeginInsert();
			};

			this.clubsGrid.KeyUp += new KeyEventHandler(clubsGrid_KeyUp);

			this.clubsGrid.RowEditEnded += (s, e) =>
			{
				if (e.EditAction == GridViewEditAction.Commit)
				{
					this.shouldAddNewRow = true;	
				}				
			};
		}

		private bool shouldAddNewRow = true;

		void clubsGrid_KeyUp(object sender, KeyEventArgs e)
		{
			if (shouldAddNewRow)
			{
				var clubs = (RadObservableCollection<Club>) (this.clubsGrid.ItemsSource);

				clubs.Add(new Club());

				shouldAddNewRow = false;
			}			
		}


		private void Button1_Click(object sender, RoutedEventArgs e)
		{

		}

		private void Button2_Click(object sender, RoutedEventArgs e)
		{

		}
	}

	//class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
	//{
	//    private GridViewDataControl parentGrid;
	//    private DefaultKeyboardCommandProvider defaultKeyboardProvider;
	//    private CustomKeyboardCommandProvider customKeyboardProvider;
	//    public CustomKeyboardCommandProvider(GridViewDataControl grid)
	//        : base(grid)
	//    {
	//        this.parentGrid = grid;
	//    }
	//    public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
	//    {
	//        List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
	//        if (key == Key.Enter)
	//        {
	//            commandsToExecute.Clear();
				
	//            var lastItem = this.parentGrid.Items[this.parentGrid.Items.Count - 1] as Club;
	//            var lastColumn = this.parentGrid.Columns[this.parentGrid.Columns.Count - 1] as Telerik.Windows.Controls.GridViewColumn;

	//            if (this.parentGrid.CurrentItem == lastItem && this.parentGrid.CurrentColumn == lastColumn)
	//            {
	//                this.parentGrid.CurrentColumn = this.parentGrid.Columns[0];
	//                commandsToExecute.Add(RadGridViewCommands.BeginInsert);
	//            }
	//            else if (this.parentGrid.CurrentColumn == lastColumn)
	//            {
	//                commandsToExecute.Add(RadGridViewCommands.MoveDown);
	//                commandsToExecute.Add(RadGridViewCommands.SelectCurrentUnit);
	//                commandsToExecute.Add(RadGridViewCommands.SelectCurrentItem);
	//            }
	//            else
	//            {
	//                commandsToExecute.Add(RadGridViewCommands.MoveRight);
	//            }
	//        }
			
	//        return commandsToExecute;
	//    }
	//}
}
