using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace SilverlightApplication2
{
	public partial class Page : UserControl
	{
		ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
		Customer currentCustomer;
		bool isCellChanged, handled;

		public Page()
		{
			InitializeComponent();

			customers.Add(new Customer("Alfreds Futterkiste", "Berlin", "Germany"));
			customers.Add(new Customer("Francisco Chang", "Mexico City", "Mexico"));
			customers.Add(new Customer("Howard Snyder", "New York", "USA"));

			RadTreeView1.ItemsSource = customers;
		}

		private void RadTreeView_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
		{
			RadTreeViewItem treeViewItem = e.Source as RadTreeViewItem;
			
			if (treeViewItem != null)
			{
				ObservableCollection<Customer> tmp = new ObservableCollection<Customer>();
				tmp.Add(treeViewItem.DataContext as Customer);
				RadGridView1.ItemsSource = tmp;

				this.isCellChanged = false;
			}
		}

		private void RadTreeView_PreviewUnselected(object sender, Telerik.Windows.RadRoutedEventArgs e)
		{
			if (this.isCellChanged)
			{
				if (MessageBox.Show("You have made some changes. Do you want to continue?",
									"Alert",
									MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
				{
					e.Handled = true;
				}
				else
				{
					this.customers[customers.IndexOf(this.currentCustomer)] = this.currentCustomer;
				}

				this.handled = e.Handled;
			}
		}

		private void RadTreeView_PreviewSelected(object sender, Telerik.Windows.RadRoutedEventArgs e)
		{
			if (this.isCellChanged)
			{
				if (this.handled)
				{
					e.Handled = true;
				}
				else
				{
					this.customers[customers.IndexOf(this.currentCustomer)] = this.currentCustomer;
				}
			}
		}

		private void RadGridView1_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
		{
			this.isCellChanged = true;
			this.currentCustomer = e.Cell.DataContext as Customer;
		}
	}

	/// <summary>
	/// Represents a customer
	/// </summary>
	public class Customer : INotifyPropertyChanged
	{
		private string city;
		private string contactName;
		private string country;

		public Customer(string name, string city, string country)
		{
			this.contactName = name;
			this.city = city;
			this.country = country;
		}

		/// <summary>
		/// Gets or sets the country.
		/// </summary>
		/// <value>The country.</value>
		public string Country
		{
			get
			{
				return this.country;
			}
			set
			{
				if (this.country != value)
				{
					this.country = value;
					this.OnPropertyChanged("Country");
				}
			}
		}

		/// <summary>
		/// Gets or sets the city.
		/// </summary>
		/// <value>The city.</value>
		public string City
		{
			get
			{
				return this.city;
			}
			set
			{
				if (this.city != value)
				{
					this.city = value;
					this.OnPropertyChanged("City");
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the contact.
		/// </summary>
		/// <value>The name of the contact.</value>
		public string ContactName
		{
			get
			{
				return this.contactName;
			}
			set
			{
				if (this.contactName != value)
				{
					this.contactName = value;
					this.OnPropertyChanged("ContactName");
				}
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		private void OnPropertyChanged(string propertyName)
		{
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
