using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.Xml.Linq;
using System.Xml;
using System.Reflection;
using System.Collections.ObjectModel;
using Telerik.Windows.Examples;
using System.Collections.Generic;

namespace TestColorPicker
{
	public class OrdersDataSource
	{
		private static ObservableCollection<Orders> orders;

		public static IEnumerable<Orders> GetOrders()
		{
			StreamResourceInfo resourceInfo = Application.GetResourceStream(GetResourceUri(OrderDataSource));
			if (resourceInfo == null)
				return null;

			XDocument document = XDocument.Load(XmlReader.Create(resourceInfo.Stream));

			if (orders == null)
			{
				orders = GetOrdersFromDocument(document);
			}

			return orders;
		}

		internal static Uri GetResourceUri(string resource)
		{
			AssemblyName assemblyName = new AssemblyName(typeof(OrdersDataSource).Assembly.FullName);
			string resourcePath = "/" + assemblyName.Name + ";component/" + resource;

			Uri resourceUri = new Uri(resourcePath, UriKind.Relative);

			return resourceUri;
		}

		protected const string OrderDataSource = "Orders/Orders.xml";

		protected static ObservableCollection<Orders> GetOrdersFromDocument(XDocument document)
		{
			ObservableCollection<Orders> list = new ObservableCollection<Orders>();

			foreach (XElement element in document.Descendants("Orders"))
			{
				Orders order = new Orders();
				order.OrderID = element.Element("OrderID").Value;
				order.ShipName = element.Element("ShipName").Value;
				order.OrderDate = element.Element("OrderDate").Value;
				

				list.Add(order);
			}

			return list;
		}

		public static void AddNewOrder(Orders order)
		{
			if (order != null)
			{
				orders.Add(order);
			}
		}

		public static void DeleteOrder(Orders order)
		{
			if (order != null)
			{
				orders.Remove(order);
			}
		}
	}
}
