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 Examples.TreeView.CS.DataSource.Categories;
using Telerik.Windows.Examples.Examples.TreeView;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls.Primitives;

namespace TestColorPicker.Tests
{
	public partial class TreeviewBinding : UserControl
	{
		public TreeviewBinding()
		{
			InitializeComponent();
			List<Category> list = new List<Category>();
			list.Add(new Category() { Id = 1, Name="Bevareges"});
			list.Add(new Category() { Id = 2, Name = "Condiments" });
			list.Add(new Category() { Id = 3, Name = "Confections" });
			this.treeView.ItemsSource = list;
			this.treeView.LoadOnDemand += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(treeView_LoadOnDemand);
			this.treeView.ItemPrepared += new EventHandler<RadTreeViewItemPreparedEventArgs>(treeView_ItemPrepared);
			
		}

		void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
		{
			if ((sender as ItemContainerGenerator).Status == GeneratorStatus.ContainersGenerated)
			{
				RadTreeViewItem item = this.currentItem.ItemContainerGenerator.ContainerFromItem(this.currentProduct) as RadTreeViewItem;
				item.IsSelected = true;
			} 
		}


		void treeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
		{
			RadTreeViewItem item = e.PreparedItem;
			if (item.Item is OrderDetails)
			{
				item.IsLoadOnDemandEnabled = false;
			}
		}
		private RadTreeViewItem currentItem;
		private Products currentProduct;
		void treeView_LoadOnDemand(object sender, Telerik.Windows.RadRoutedEventArgs e)
		{
			RadTreeViewItem item = e.OriginalSource as RadTreeViewItem;
			currentItem = item;
		
			System.Windows.Threading.DispatcherTimer pageTimer = new System.Windows.Threading.DispatcherTimer();
			pageTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); // milliseconds
			pageTimer.Tick += new EventHandler(loadOnDemandTimer_Tick);
			pageTimer.Start();
		}

		void loadOnDemandTimer_Tick(object sender, EventArgs e)
		{
			Category c = currentItem.Item as Category;
			if (c != null)
			{
				IEnumerable<Products> products = ProductsDataSource.GetProductsByCategory(c.Id.ToString());
				AddRange(c.Products, products);
				foreach (Products item in products)
				{ 
					if(item.ProductID == "5")
					{
						currentProduct = item;
						this.currentItem.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
					}
				}
			
			}
			else
			{
				Products p = currentItem.Item as Products;
				if (p != null)
				{
					IEnumerable<OrderDetails> orderDetails = OrderDetailsDataSource.GetOrderDetailsByProduct(p.ProductID);
					AddRange(p.OrderDetails, orderDetails);
				}
				
			}

			currentItem.IsLoadOnDemandEnabled = false;

			(sender as DispatcherTimer).Stop();
		}

		private static void AddRange<T>(Collection<T> collection, IEnumerable<T> items)
		{
			foreach (T item in items)
			{
				collection.Add(item);
			}
		}
	}
}
