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

using System;
using System.Web.Caching;
using System.Collections;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for ThreadTracking.
	/// </summary>
	public class ThreadTracking
	{

		public static bool IsTracked(int threadID, int userID)
		{
            bool isTracked = false;
            string cacheKey = "ThreadSubscriptions-" + threadID + "-" + userID;

            // check and see if we have the item
            if (CSCache.Get(cacheKey) != null)
                return (bool) CSCache.Get(cacheKey);

            // we didn't find it in the cache
            CommonDataProvider dp = CommonDataProvider.Instance();
            isTracked = dp.IsThreadTracked(threadID, userID);

            // add into the cache
            CSCache.Insert(cacheKey, isTracked, 180);

            return isTracked;

		}

        public static void ResetCache(int threadID, int userID)
        {
			if (threadID != -1)
			{
				string cacheKey = "ThreadSubscriptions-" + threadID + "-" + userID;
				CSCache.Remove(cacheKey);
			}

			CSCache.Remove("SectionSubscriptions-User-" + userID);
        }

        
		#region Removed IsUserTrackingThread Method
        // *********************************************************************
        //  IsUserTrackingThread
        //
        // <summary>
        // Returns a boolean to indicate whether the user is tracking the thread.
        // </summary>
        // <param name="PostID">The ID of the Post to obtain information about.</param>
        // <param name="Username">The Username of the user viewing the post.</param>
        // 
        // ***********************************************************************/
        /*public static bool IsUserTrackingThread(int threadID, String username) 
        {
            if (username == null)
                return false;

            // Create Instance of the CommonDataProvider
            CommonDataProvider dp = CommonDataProvider.Instance();

            return dp.IsUserTrackingThread(threadID, username);
        }*/
		#endregion


        // *********************************************************************
        //  ReverseThreadTrackingOptions
        //
        /// <summary>
        /// This method reverses a user's thread tracking options for the thread containing a
        /// particular Post.
        /// </summary>
        /// <param name="Username">The user whose thread tracking options you wish to change.</param>
        /// <param name="PostID">The post of the thread whose tracking option you wish to reverse for
        /// the specified user.</param>
        /// 
        // ***********************************************************************/
        public static void ReverseThreadTrackingOptions(int threadid, int userID) 
        {
            // Create Instance of the CommonDataProvider
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.ReverseThreadTracking(userID, threadid);

            ResetCache(threadid,userID);
        }

		public static void RemoveThreadTracking(int sectionID, int userID) 
		{
			// Create Instance of the CommonDataProvider
			CommonDataProvider dp = CommonDataProvider.Instance();
			dp.RemoveThreadTracking(sectionID,userID);
		}

		public static void SetSectionSubscriptionType(int sectionID, EmailSubscriptionType est)
		{
			SetSectionSubscriptionType(sectionID,CSContext.Current.User.UserID,est);
		}

		public static void SetSectionSubscriptionType(int sectionID, int UserID, EmailSubscriptionType est) 
		{
			CommonDataProvider dp = CommonDataProvider.Instance();
			dp.SetSectionSubscriptionType(sectionID, UserID, est);

            Hashtable subs = FindSubscriptionTable(sectionID);
            subs[UserID] = est;

			ResetCache(-1,UserID);
        }

		public static EmailSubscriptionType GetSectionSubscriptionType(int sectionID)
		{
			CSContext csContext = CSContext.Current;

			if (csContext.User.IsAnonymous)
				return EmailSubscriptionType.None;

			return GetSectionSubscriptionType(sectionID,csContext.User.UserID);
		}

		public static EmailSubscriptionType GetSectionSubscriptionType(int sectionID, int userID) 
		{
			EmailSubscriptionType subscriptionType;

		    Hashtable sectionSubscriptions = FindSubscriptionTable(sectionID);

		    // check and see if we have the item
			if (sectionSubscriptions[userID] != null)
				return (EmailSubscriptionType) sectionSubscriptions[userID];

			// we didn't find it in the cache
			CommonDataProvider dp = CommonDataProvider.Instance();
			subscriptionType = dp.GetSectionSubscriptionType(sectionID, userID);

			// add into the cache
			sectionSubscriptions.Add(userID, subscriptionType);
			return subscriptionType;
		}

		public static Hashtable GetSubscriptionTypesBySectionID(int userID)
		{
			string cacheKey = "SectionSubscriptions-User-" + userID;
			Hashtable sectionSubscriptions = CSCache.Get(cacheKey) as Hashtable;

			if (sectionSubscriptions == null)
			{
				// we didn't find it in the cache
				CommonDataProvider dp = CommonDataProvider.Instance();
				sectionSubscriptions = dp.GetSubscriptionTypesBySectionID(userID);

				// add into the cache
				CSCache.Insert(cacheKey, sectionSubscriptions, CSCache.MinuteFactor, CacheItemPriority.Low);
			}

			return sectionSubscriptions;
		}

	    private static Hashtable FindSubscriptionTable(int sectionID)
	    {
            string cacheKey = "SectionSubscriptions-" + sectionID;
	        Hashtable sectionSubscriptions = CSCache.Get(cacheKey) as Hashtable;

			// insert into the cache if null
	        if (sectionSubscriptions == null) {
	            sectionSubscriptions = new Hashtable();
	            CSCache.Insert(cacheKey, sectionSubscriptions, CSCache.MinuteFactor, CacheItemPriority.Low);
	        }
	        return sectionSubscriptions;
	    }
	}
}
