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

using System;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls {
    
    public class StatusDropDownList: DropDownList {
        
        int threadID = 0;

        public StatusDropDownList () {
        }
        
        protected override void CreateChildControls () {
            base.CreateChildControls ();
                        
            this.DataBind();

            base.ChildControlsCreated = true;
        }

        public override void DataBind () {
            base.DataBind ();

            // Create list items unless items are already there
            //
            if (Items.Count > 0)
                return;

            //Items.Clear();
            Items.Add(new ListItem(ResourceManager.GetString("Status_NotSet"), ((int) ThreadStatus.NotSet).ToString()));
            Items.Add(new ListItem(ResourceManager.GetString("Status_Answered"), ((int) ThreadStatus.Answered).ToString()));
            Items.Add(new ListItem(ResourceManager.GetString("Status_NotAnswered"), ((int) ThreadStatus.NotAnswered).ToString()));
        }


        #region Render
        protected override void Render (System.Web.UI.HtmlTextWriter writer) 
        {                        
            // Is thread status enabled?            
            bool enabledThreadStatus = ForumConfiguration.Instance().EnableThreadStatus;
            if (!enabledThreadStatus)
                return;
            
            if (!this.Visible)
                return;
            
            base.Render (writer);
        }
        #endregion
        
        #region Properties
        public new ThreadStatus SelectedValue {
            get {
                this.EnsureChildControls();
                return (ThreadStatus) Convert.ToInt32( base.SelectedValue );
            }
            set {
                this.EnsureChildControls();
                base.SelectedValue = ((int) value).ToString();
            }
        }

        public int ThreadID {
            get { return threadID; }
            set { threadID = value; }
        }
        #endregion

        #region StatusChanged event
        public event ThreadStatusEventHandler StatusChanged;

        protected void OnStatusChanged (ThreadStatusEventArgs e) {
            if (StatusChanged != null)
                StatusChanged( this, e);
        }
        #endregion
        
        #region OnSelectedIndexChanged
        protected override void OnSelectedIndexChanged (EventArgs e) {
            base.OnSelectedIndexChanged (e);

            OnStatusChanged( new ThreadStatusEventArgs( this.ThreadID, this.SelectedValue ) );
        }
        #endregion
    }    
}
