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

using System;
using System.Data;
using System.Data.SqlClient;
using CommunityServer.Configuration;

namespace CommunityServer.Components
{
    /// <summary>
	/// Summary description for GalleryDataProvider.
	/// </summary>
	public abstract class ProviderHelper
	{

		public static readonly string ProviderHelperName = "ProviderHelper";

		#region Instance

		private static ProviderHelper _defaultInstance = null;

		static ProviderHelper()
		{
			CreateDefaultCommonProvider();
		}

		/// <summary>
		/// Returns an instance of the user-specified data provider class.
		/// </summary>
		/// <returns>An instance of the user-specified data provider class.  This class must inherit the
		/// CommonDataProvider interface.</returns>
		public static ProviderHelper Instance() 
		{
			return _defaultInstance;
		}

		public static ProviderHelper Instance (Provider dataProvider) 
		{
			ProviderHelper fdp = CSCache.Get(dataProvider.Name) as ProviderHelper;
			if(fdp == null)
			{
				fdp = DataProviders.Invoke(dataProvider) as ProviderHelper;
				CSCache.Max(dataProvider.Name,fdp);
			}
			return fdp;
		}

		/// <summary>
		/// Creates the Default CommonDataProvider
		/// </summary>
		private static void CreateDefaultCommonProvider()
		{
			// Get the names of the providers
			//
			CSConfiguration config = CSConfiguration.GetConfig();

			// Read the configuration specific information
			// for this provider
			//
			Provider sqlForumsProvider = (Provider) config.Providers[ProviderHelperName];

			// Read the connection string for this provider
			//
            
			_defaultInstance = DataProviders.CreateInstance(sqlForumsProvider) as ProviderHelper;
		}
        
		#endregion

		public abstract int GetSettingsID();
		public abstract void SetSettingsID(int settingsID);

		public abstract SiteSettings GetSiteSettings();

		public SqlParameter SettingsIDParameter()
		{
			SqlParameter p = new SqlParameter("@SettingsID", SqlDbType.Int);
			p.Value = GetSettingsID();
			return p;
		}

		public static DateTime GetSafeSqlDateTime(DateTime date) 
		{
			if (date == DateTime.MinValue)
				return (DateTime) System.Data.SqlTypes.SqlDateTime.MinValue;
			return date;
		}

	}
}
