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

using System;
using CommunityServer.Components;

namespace CommunityServer.Discussions.Components {

    // *********************************************************************
    //
    //  Thread
    //
    /// <summary>
    /// The thread class is similar to the post class but has additional properties
    /// </summary>
    //
    // ********************************************************************/
    public class Thread : ForumPost, IComparable, IThread {

        static SortThreadsBy sortBy = SortThreadsBy.ThreadDateDesc;
        string mostRecentAuthor = "";   // Most recent post author
        int mostRecentAuthorID = 0;
        int mostRecentPostID = 0;       // Most recent post id
        int authorID = 0;

        public override object Copy()
        {
            Thread thread = base.Copy() as Thread;
            thread.MostRecentPostAuthor = this.MostRecentPostAuthor;
            thread.MostRecentPostAuthorID = this.MostRecentPostAuthorID;
            thread.AuthorID = this.AuthorID;
            thread.Status = this.Status;
            thread.IsSticky = this.IsSticky;
            thread.StickyDate = this.StickyDate;
            thread.MostRecentPostID = this.MostRecentPostID;

            return thread;
        }


        // *********************************************************************
        //  CompareTo
        //
        /// <summary>
        /// All forums have a SortOrder property. CompareTo compares on SortOrder
        /// to sort the forums appropriately.
        /// </summary>
        // ********************************************************************/
        public int CompareTo(object value) 
        {

            if (value == null) return 1;

            switch (SortBy) {
                case SortThreadsBy.Subject:
                    return (this.Subject.CompareTo( ((Thread) value).Subject) );

                case SortThreadsBy.SubjectDesc:
                    return ( ((Thread) value).Subject.CompareTo(Subject) );

                case SortThreadsBy.Author:
                    return (this.Username.CompareTo( ((Thread) value).Username) );

                case SortThreadsBy.AuthorDesc:
                    return ( ((Thread) value).Username.CompareTo(Username) );

                case SortThreadsBy.Replies:
                    return (this.Replies.CompareTo( ((Thread) value).Replies) );

                case SortThreadsBy.RepliesDesc:
                    return ( ((Thread) value).Replies.CompareTo(Replies) );

                case SortThreadsBy.Views:
                    return (this.Views.CompareTo( ((Thread) value).Views) );

                case SortThreadsBy.ViewsDesc:
                    return ( ((Thread) value).Views.CompareTo(Views) );

                case SortThreadsBy.ThreadDate:
                    return (this.ThreadDate.CompareTo( ((Thread) value).ThreadDate) );

                default:
                    return (((Thread) value).ThreadDate.CompareTo(ThreadDate) );
            }

        }

        public enum SortThreadsBy {
            Subject,
            SubjectDesc,
            Author,
            AuthorDesc,
            Replies,
            RepliesDesc,
            Views,
            ViewsDesc,
            ThreadDate,
            ThreadDateDesc
        }

        public static SortThreadsBy SortBy {
            get {
                return sortBy;
            }
            set {
                sortBy = value;
            }
        }
        

        ThreadStatus status = ThreadStatus.NotSet;
        public ThreadStatus Status {
            get { return status; }
            set { status = value; }
        }

        public int AuthorID {
            get {
                return authorID;
            }
            set {
                authorID = value;
            }
        }

        // *********************************************************************
        //
        //  IsPopular
        //
        /// <summary>
        /// If thread has activity in the last 2 days and > 20 replies
        /// </summary>
        //
        // ********************************************************************/
        public bool IsPopular {
            get { 
                if ((ThreadDate < DateTime.Now.AddDays(ForumConfiguration.Instance().PopularPostLevelDays)) && ( (Replies > ForumConfiguration.Instance().PopularPostLevelPosts) || (Views > ForumConfiguration.Instance().PopularPostLevelViews) ))
                    return true;

                return false;
            }
        }
        
        // *********************************************************************
        //
        //  IsAnnouncement
        //
        /// <summary>
        /// If post is locked and post date > 2 years
        /// </summary>
        //
        // ********************************************************************/
        public bool IsAnnouncement {
            get { 
                if ((StickyDate > DateTime.Now.AddYears(2)) && (IsLocked))
                    return true;
                else
                    return false;
            }
        }


        bool isSticky = false;
        public bool IsSticky {
            get {
                return isSticky;
            }
            set {
                isSticky = value;
            }
        }

        DateTime stickyDate = DateTime.Now.AddYears(-25);
        public DateTime StickyDate {
            get {
                return stickyDate;
            }
            set {
                stickyDate = value;
            }
        }


        // *********************************************************************
        //
        //  MostRecentPostAuthor
        //
        /// <summary>
        /// The author of the most recent post in the thread.
        /// </summary>
        //
        // ********************************************************************/
        public string MostRecentPostAuthor {
            get { 
				if (ThreadNeedsIgnoredScrubbing())
					return ResourceManager.GetString("IgnoredPost_Username");
				else
					return mostRecentAuthor; 
            }
            set { 
                mostRecentAuthor = value; 
            }
        }

        public int MostRecentPostAuthorID {
            get {
				if (ThreadNeedsIgnoredScrubbing())
					return 0;
				else
					return mostRecentAuthorID;
            }
            set {
                mostRecentAuthorID = value;
            }
        }

        public string PreviewBody {
            get {
				if  (ThreadNeedsIgnoredScrubbing())
					return ResourceManager.GetString("IgnoredPost_Body");
				else
					return Transforms.StripForPreview(base.Body);
            }
        }

        // *********************************************************************
        //
        //  MostRecentPostID
        //
        /// <summary>
        /// The post id of the most recent post in the thread.
        /// </summary>
        //
        // ********************************************************************/
        public int MostRecentPostID {
            get { 
                return mostRecentPostID; 
            }
            set { 
                mostRecentPostID = value; 
            }
        }   

		public new string Subject
		{
			get {
				if (ThreadNeedsIgnoredScrubbing())
					return ResourceManager.GetString("IgnoredPost_Subject");
				else
					return base.Subject;
			}
			set {base.Subject = value;}
		}
		
		public bool ThreadShouldBeIgnored
		{
			get {return this.ThreadNeedsIgnoredScrubbing();}
		}

		protected override RatingType RatingType
		{
			get { return RatingType.Thread; }
		}

		protected override int ItemID
		{
			get { return ThreadID; }
		}

		#region Helper Methods
	
		private bool ThreadNeedsIgnoredScrubbing()
		{
			bool needsScrubbing = false;
			User currentUser = CSContext.Current.User;

			if (this.IsIgnoredPost && 
				currentUser.UserID != this.mostRecentAuthorID)
			{
				if (!currentUser.IsForumAdministrator
					|| !currentUser.IsModerator)
				{
					needsScrubbing = true;  
											 
				}
			}
			return needsScrubbing;
		}
		#endregion
    }
}
