//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Web.Services.Protocols;
using System.Web.Services;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for Products.
	/// </summary>
	public class Products
	{
		private Products()
		{
		}

		public static ArrayList GetProducts()
		{
			if (!Configuration.CSConfiguration.GetConfig().EnableProductListing)
				throw new CSException(CSExceptionType.GeneralAccessDenied, "Product listing has been disabled");

			ArrayList products = (ArrayList) CSCache.Get("Products");
			if (products == null)
			{
				products = new ArrayList((new StoreServices()).ProductCatalog(String.Empty));
				products.Sort(new ProductSorter());
				CSCache.Insert("Products", products, new System.Web.Caching.CacheDependency(null, new string[] { CommunityServer.Configuration.CSConfiguration.CacheKey }), 60 * 60 * 24);
			}

			return products;
		}

		#region Sorting class

		class ProductSorter : System.Collections.IComparer
		{
			#region IComparer Members

			public int Compare(object x, object y)
			{
				store_Product a = (store_Product) x;
				store_Product b = (store_Product) y;
				int result = 0;

				if ((result = a.Category.CompareTo(b.Category)) == 0)
					return a.SortOrder.CompareTo(b.SortOrder);
				else
					return result;				
			}

			#endregion
		}

		#endregion

		#region Web Service Access Class

		[System.Diagnostics.DebuggerStepThroughAttribute()]
		[System.ComponentModel.DesignerCategoryAttribute("code")]
		[System.Web.Services.WebServiceBindingAttribute(Name="StoreServicesSoap", Namespace="http://tempuri.org/")]
		class StoreServices : System.Web.Services.Protocols.SoapHttpClientProtocol 
		{
			public StoreServices() 
			{
				this.Url = "https://store.telligentsystems.com/ServicesWeb/StoreServices.asmx";
			}
    
			[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/IsStoreEnabled", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
			public bool IsStoreEnabled() 
			{
				object[] results = this.Invoke("IsStoreEnabled", new object[0]);
				return ((bool)(results[0]));
			}
    
			public System.IAsyncResult BeginIsStoreEnabled(System.AsyncCallback callback, object asyncState) 
			{
				return this.BeginInvoke("IsStoreEnabled", new object[0], callback, asyncState);
			}
    
			public bool EndIsStoreEnabled(System.IAsyncResult asyncResult) 
			{
				object[] results = this.EndInvoke(asyncResult);
				return ((bool)(results[0]));
			}
    
			[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/ProductCatalog", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
			[return: System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
			public store_Product[] ProductCatalog(string csInfo) 
			{
				object[] results = this.Invoke("ProductCatalog", new object[] {
																				  csInfo});
				return ((store_Product[])(results[0]));
			}
    
			public System.IAsyncResult BeginProductCatalog(string csInfo, System.AsyncCallback callback, object asyncState) 
			{
				return this.BeginInvoke("ProductCatalog", new object[] {
																		   csInfo}, callback, asyncState);
			}
    
			public store_Product[] EndProductCatalog(System.IAsyncResult asyncResult) 
			{
				object[] results = this.EndInvoke(asyncResult);
				return ((store_Product[])(results[0]));
			}
		}


		#endregion
	}
}
