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

using System;
using CommunityServer.Components;

namespace CommunityServer.Components
{
    //NOTE: This class will be moving to CS.Components

	/// <summary>
	/// Common document structure for an indexed documen.
	/// </summary>
	[Serializable]
	public class IndexPost : CSCopy, IContent
	{

		private string _bestmatch = "", _body, _url, _title, _username, _name, _applicationkey,_appUrl, _sectionName;
        private string[] _postCategories;
        private int _postid, _sectionid, _groupid, _settingsID, _threadID;
        private DateTime _postDate;
        private ApplicationType _appType = ApplicationType.Unknown;
        private PostContentType _postType;

        public override object Copy()
        {
            IndexPost post = base.Copy () as IndexPost;
            post.BestMatch = this.BestMatch;
            post.Body = this.Body;
            post.Title = this.Title;
            post.Name = this.Name;
            post.ApplicationKey = this.ApplicationKey;
            post.ApplicationUrl = this.ApplicationUrl;
            post.SectionName = this.SectionName;
            post.PostID = this.PostID;
            post.SectionID = this.SectionID;
            post.GroupID = this.GroupID;
            post.SettingsID = this.SettingsID;
            post.ThreadID  = this.ThreadID;
            post.PostDate = this.PostDate;
            post.ApplicationType = this.ApplicationType;
            post.PostCategories = this.PostCategories;
            post.PostType = this.PostType;

            return post;
        }


		public string Body
		{
		    get {return _body;}
            set {_body = value;}
		}

        public string FormattedBody
        {
            get{ return this.Body;}
            set{this.Body = value;}
        }

        public string SectionName
        {
            get {return _sectionName;}
            set {_sectionName = value;}
        }

		public string Url
        {
            get {return _url;}
            set {_url = value;}
        }

        public string ApplicationUrl
        {
            get
            {
                if(_appUrl == null && this.ApplicationType != ApplicationType.Unknown)
                {
                    CSApplicationData data = ApplicationSet.Applications[this.ApplicationType];
                    if(data != null)
                        _appUrl = data.Home;
                }
                return _appUrl;
            }
            set {_appUrl = value;}
        }

		public string Title
        {
            get {return _title;}
            set {_title = value;}
        }

	    /// <summary>
	    /// An Excerpt of the body with search terms highlighted
	    /// if this is not set, the search results page will automatically format the body (searchbarrel default)
	    /// </summary>
		public string BestMatch
		{
			get{return _bestmatch;}
			set{_bestmatch = value;}
		}

        public string UserName
        {
            get {return _username;}
            set {_username = value;}
        }
        public string Name
        {
            get {return _name;}
            set {_name = value;}
        }

        public string ApplicationKey
        {
            get{ return _applicationkey;}
            set{_applicationkey = value;}
        }

		public int PostID
		{
		    get {return _postid;}
            set {_postid = value;}
		}

        public int ThreadID
        {
            get {return _threadID;}
            set {_threadID = value;}
        }


		public int SectionID
        {
            get {return _sectionid;}
            set {_sectionid = value;}
        }
		public int GroupID
        {
            get {return _groupid;}
            set {_groupid = value;}
        }

        public int SettingsID
        {
            get {return _settingsID;}
            set {_settingsID = value;}
        }

		public DateTime PostDate
		{
		    get{return _postDate;}
            set{_postDate = value;}
		}


		public ApplicationType ApplicationType
		{
		    get{return _appType;}
            set{_appType = value;}
		}

        public PostContentType PostType
        {
            get{return _postType;}
            set{_postType = value;}
        }

        public string[] PostCategories
	    {
            get { return _postCategories; }
            set { _postCategories = value; }
        }

	    public string RenderedBody(CommunityServer.Components.PostTarget target)
        {
            IndexPost p = this.Copy() as IndexPost;
            CSEvents.PreRenderPost(p,p.ApplicationType,target);
            return p.FormattedBody;
        }

        public bool HasPostCategories
        {
            get
            {
                return
                (this.PostCategories != null && this.PostCategories.Length > 0 &&
                 this.PostCategories[0].ToString() != "");
            }
        }
    }
}
