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

using System;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using CommunityServer.Components;

namespace CommunityServer.Data
{
	/// <summary>
	/// Summary description for WebSqlProviderHelper.
	/// </summary>
	public class WebSqlProviderHelper : ProviderHelper
	{

		#region Member Variables

		private string databaseOwner = "dbo";
		private string connectionString = null;
		const string cacheKey = "DataProviderKey.SettingsID";

		#endregion

		#region Constructor

		public WebSqlProviderHelper(string databaseOwner, string connectionString) 
		{
			this.connectionString = connectionString;
			this.databaseOwner = databaseOwner;
		}

		#endregion

		public override int GetSettingsID()
		{
			return CSContext.Current.SiteSettings.SettingsID;
//			HttpContext cntx  = HttpContext.Current;
//
//			//First check the context
//			if(cntx != null && cntx.Items[cacheKey] != null)
//				return (int)cntx.Items[cacheKey];
//
//			//get the applicatin name
//			string name = CSContext.Current.SiteUrl;
//            
//			int settingsID = SettingsIDLookUp(name,false);
//
//			if(settingsID == -1)
//				settingsID = SettingsIDLookUp(name,true);   //If the second look up fails, do we want to throw an exception?
//
//			if(cntx != null)
//				cntx.Items.Add(cacheKey,settingsID);
//            
//			return settingsID;
		}

		public override void SetSettingsID(int settingsID) { }

		public override SiteSettings GetSiteSettings()
		{
			return CSContext.Current.SiteSettings;
		}

		#region SettingsIDLookUp
		/// <summary>
		/// Provides the look up implementation for SettingsID's
		/// </summary>
		/// <param name="name">The specific application we are looking for</param>
		/// <param name="flush">Do we want to flush the cache?</param>
		protected int SettingsIDLookUp(string name, bool flush)
		{
			if(flush)
				CSCache.Remove(cacheKey);

			if(!name.EndsWith("/"))
				name += "/";

			//Look for the cached NameValueCollection
            
			NameValueCollection nvc = CSCache.Get(cacheKey) as NameValueCollection;
			if(nvc == null)
			{                
				using(SqlConnection connection = new SqlConnection(connectionString))
				{
					using(SqlCommand command =  new SqlCommand( databaseOwner + ".cs_SettingsIDs_Get", connection))
					{
						connection.Open();
						using(SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
						{
							nvc = new NameValueCollection();
							while(reader.Read())
							{
								string app = reader["SiteUrl"] as string;
								if(!app.EndsWith("/"))
									app += "/";
								nvc.Add(app,reader["SettingsID"].ToString());
							}
							reader.Close();
							command.Dispose();
							connection.Close();
							CSCache.Max(cacheKey,nvc);
						}
					}
				}
			}

			int settingsID = -1;

			string sid = nvc[name] as string;
			if(sid != null)
				settingsID = Int32.Parse(sid);

			System.Diagnostics.Debug.Assert(settingsID > 0,"SettingsID was not found.", "A valid SettingsID could not be found. Look for issue with background threads");

			return settingsID;
		}
		#endregion
	}
}
