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

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using System.ComponentModel;
using System.IO;

namespace CommunityServer.Controls 
{
    /// <summary>
    /// This Web control helps to change old user's secret question and answer.
    /// </summary>
    [
    ParseChildren(true)
    ]
    public class ChangePasswordAnswer : TemplatedWebControl
    {
        #region Child Controls

        TextBox currentAnswer;
        TextBox newAnswer1;
        TextBox newAnswer2;
        RequiredFieldValidator newAnswerValidator;
        CompareValidator newAnswerCompareVadlidator;
        RequiredFieldValidator validateAnswer2;
        Button changeAnswerButton;
        RegularExpressionValidator answerRegexValidator;
        RegularExpressionValidator answer1RegrexValidator;
        Label currentQuestion;
        QuestionsDropDownList newQuestion;
        Label userName;
        Panel showCurrentPasswordAnswer;

        CSContext csContext = CSContext.Current;
        User user = null;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Skin-ChangePasswordAnswer.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            if (csContext.UserID > 0 && 
                csContext.User.IsForumAdministrator) 
            {
                // Change passwd from admin
                //
                try 
                {
                    user = Users.GetUser( csContext.UserID, true );
                } 
                catch (Exception ex)
                {
                    CSException csException = new CSException( CSExceptionType.UserNotFound, ex.Message, ex );
                    csException.Log();

                    throw csException;
                }
            } 
            else 
            {
                // Change the user's password
                //
                user = csContext.User;
            }

            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            DataBind();

            base.OnLoad( e );
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            if (userName != null)
            {
                if (csContext.UserID > 0 && 
                    csContext.User.IsForumAdministrator)
                {
                    userName.Text = "- (" + user.Username + ")";
                }
            }

            if (user.UserID != csContext.User.UserID && 
                (csContext.User.IsForumAdministrator || csContext.User.IsModerator)) 
            {
                if (showCurrentPasswordAnswer != null)
                    showCurrentPasswordAnswer.Visible = false;
            } 
            else 
            {
                if (showCurrentPasswordAnswer != null)
                    showCurrentPasswordAnswer.Visible = true;

                if (answerRegexValidator != null)
                    answerRegexValidator.ErrorMessage = ResourceManager.GetString("ChangePasswordAnswer_RegExInvalidAnswer");
            }

            if (newAnswerValidator != null)
                newAnswerValidator.ErrorMessage = ResourceManager.GetString("ChangePasswordAnswer_NewAnswerRequired");

            if (answer1RegrexValidator != null)
                answer1RegrexValidator.ErrorMessage = ResourceManager.GetString("ChangePasswordAnswer_RegExInvalidAnswer1");

            if (newAnswerCompareVadlidator != null)
                newAnswerCompareVadlidator.ErrorMessage = ResourceManager.GetString("ChangePasswordAnswer_ReEnterNewAnswerRequired");

            if (validateAnswer2 != null)
                validateAnswer2.ErrorMessage = ResourceManager.GetString("ChangePasswordAnswer_ReEnterAnswerRequired");
            
            if (Globals.IsNullorEmpty( user.PasswordQuestion )) 
            {
                if (showCurrentPasswordAnswer != null)
                    showCurrentPasswordAnswer.Visible = false;

                if (currentQuestion != null)
                {
                    currentQuestion.Text = ResourceManager.GetString("ChangePasswordAnswer_NotAvailable");
                    currentQuestion.ForeColor = System.Drawing.Color.Red;
                }
            } 
            else 
            {
                if (currentQuestion != null)
                {
                    currentQuestion.Text = user.PasswordQuestion;
                    currentQuestion.Font.Bold = true;
                }
            }
            
            if (changeAnswerButton != null)
                changeAnswerButton.Text = ResourceManager.GetString("ChangePasswordAnswer_ChangeAnswer");

            if (csContext.User.IsForumAdministrator && 
                csContext.UserID == 0 &&
                changeAnswerButton != null)
            {
                changeAnswerButton.Enabled = false;
            }
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            userName = FindControl("UserName") as Label;
            showCurrentPasswordAnswer = FindControl("showCurrentPasswordAnswer") as Panel;
            currentQuestion = FindControl("CurrentQuestion") as Label;
            currentAnswer = FindControl("Answer") as TextBox;
            answerRegexValidator = FindControl("AnswerRegexValidator") as RegularExpressionValidator;
            newAnswer1 = FindControl("NewAnswer1") as TextBox;
            newAnswer2 = FindControl("NewAnswer2") as TextBox;
            newAnswerValidator  = FindControl("ValidateAnswer1") as RequiredFieldValidator;
            answer1RegrexValidator = FindControl("Answer1RegrexValidator") as RegularExpressionValidator;
            newAnswerCompareVadlidator = FindControl("CompareAnswer") as CompareValidator;
            validateAnswer2 = FindControl("ValidateAnswer2") as RequiredFieldValidator;
            newQuestion = FindControl("NewQuestion") as QuestionsDropDownList;
            changeAnswerButton = FindControl("ChangeAnswerButton") as Button;
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
		    if (changeAnswerButton != null)
                changeAnswerButton.Click += new EventHandler(ChangePasswordAnswer_Click);
        }
		        
        #endregion

        #region Event Handlers

        void ChangePasswordAnswer_Click (Object sender, EventArgs e) 
        {
            bool status = false;
            string currAnswer = "";

            if (currentAnswer != null && 
                currentAnswer.Text != "")
            {
                currAnswer = currentAnswer.Text;
            }

            if (!Page.IsValid)
                return;
            
            if (user != null)
                status = user.ChangePasswordAnswer( currAnswer, newQuestion.SelectedValue, newAnswer1.Text );
            
            if (status) 
                throw new CSException( CSExceptionType.UserPasswordAnswerChangeSuccess );
            else 
                throw new CSException( CSExceptionType.UserPasswordAnswerChangeFailed );
        }

        #endregion
    }
}

