
namespace SilverlightApplication1.Web.Services
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.ComponentModel.DataAnnotations;
	using System.Data;
	using System.Linq;
	using System.ServiceModel.DomainServices.EntityFramework;
	using System.ServiceModel.DomainServices.Hosting;
	using System.ServiceModel.DomainServices.Server;
	using SilverlightApplication1.Web.Models;
	using System.Linq.Expressions;


	// Implements application logic using the northwndEntities context.
	// TODO: Add your application logic to these methods or in additional methods.
	// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
	// Also consider adding roles to restrict access as appropriate.
	// [RequiresAuthentication]
	[EnableClientAccess()]
	public class NorthwindDomainService : LinqToEntitiesDomainService<northwndEntities>
	{

		// TODO:
		// Consider constraining the results of your query method.  If you need additional input you can
		// add parameters to this method or create additional query methods with different names.
		// To support paging you will need to add ordering to the 'Customers' query.
		public IQueryable<Customer> GetCustomers()
		{
			return this.ObjectContext.Customers
				.OrderBy(c=>c.CustomerID);
		}

		public IQueryable<Customer> GetCustomersByCountry(string country)
		{
			if (string.IsNullOrEmpty(country))
			{
				return this.ObjectContext.Customers
					.OrderBy(c => c.CustomerID);
			}
			
			return this.ObjectContext.Customers
				.Where(c => c.Country.Contains(country))
				.OrderBy(c => c.CustomerID);
		}

		public void InsertCustomer(Customer customer)
		{
			if ((customer.EntityState != EntityState.Detached))
			{
				this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
			}
			else
			{
				this.ObjectContext.Customers.AddObject(customer);
			}
		}

		public void UpdateCustomer(Customer currentCustomer)
		{
			this.ObjectContext.Customers.AttachAsModified(currentCustomer, this.ChangeSet.GetOriginal(currentCustomer));
		}

		public void DeleteCustomer(Customer customer)
		{
			if ((customer.EntityState != EntityState.Detached))
			{
				this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Deleted);
			}
			else
			{
				this.ObjectContext.Customers.Attach(customer);
				this.ObjectContext.Customers.DeleteObject(customer);
			}
		}

		public List<DistinctValue> GetDistinctValues(string propertyName)
		{
			var list = new List<DistinctValue>();
			var values = this.ObjectContext.Customers.Select(CreateSelectorExpression(propertyName)).Distinct();
			int i = 0;
			foreach (var value in values)
			{
				list.Add(new DistinctValue() { ID = i, Value = value });
				i++;
			}

			return list;
		}

		private static Expression<Func<Customer, string>> CreateSelectorExpression(string propertyName)
		{
			var paramterExpression = Expression.Parameter(typeof(Customer));
			return (Expression<Func<Customer, string>>)Expression.Lambda(Expression.PropertyOrField(paramterExpression, propertyName), paramterExpression);
		}
	}

	public class DistinctValue
	{
		[Key]
		public int ID { get; set; }
		public string Value { get; set; }
	}
}


