using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web.Security;
using NikaiDSR;
using BusinessLayer;
using DTO;
using Helper;

namespace NikaiDSR
{
    public partial class ChangePassword : PageBase
    {

        #region code declaration
        UserManager UM = new UserManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageTitle = "NIKAI- CHANGE PASSWORD";
            }
        }

        #region code btnSubmit_Command
        protected void btnSubmit_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "save")
            {

                if (ValidateInput())
                {

                    if (UM.ChangePassword(base.UserID, txtNewPass.Text))
                    {
                        ShowErrorMessage((string)GetGlobalResourceObject("Res", "Password_Changed"), errMsg, Common.ERROR_MESSAGE_TYPE.SUCCESS);
                        btnSubmit.Visible = false;
                        Session.Abandon(); // Remove session and redirect to login page
                        FormsAuthentication.SignOut();

                        String EmailBody = "Dear " + UserName + ",<br/><br/>Your account password has been changed successfully to access the online Nikai Sales Reporting website."
                                           + "<br/><br/> Your login details are : <b><br/>Site URL: <><br/>User name is: " + Email + "<br/>Password is: " + txtNewPass.Text
                                           + "<br/><br/>Best Regards,<br/>Nikai Admin";

                        Helper.Common.SendEmail(Email, "Nikai - Password Changed", EmailBody);
                        Response.Redirect("~/Login.aspx");
                    }
                    else
                        ShowErrorMessage((string)GetGlobalResourceObject("Res", "Password_Not_Changed"), errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
                }
            }
        }
        #endregion

        #region Validate Details
        private bool ValidateInput()
        {
            bool Flag = true;
            string Msg = "";
            if (txtNewPass.Text.Trim() != txtConPass.Text.Trim())
            {
                Msg = (string)GetGlobalResourceObject("Res", "Old_New_Password_Missmatch");  // // Password & Confirm Password not matching 
                Flag = false;
            }
            else if (Password != txtOldpass.Text.Trim())
            {
                Msg = (string)GetGlobalResourceObject("Res", "Old_Password_Mismatch"); // Existing password entered is wrong 
                Flag = false;
            }
            else if (Password == txtNewPass.Text)
            {
                Msg = (string)GetGlobalResourceObject("Res", "Old_New_Password_Same"); // // Existing password and new password is same 
                Flag = false;
            }

            if (Flag == false)
                ShowErrorMessage(Msg, errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
            return Flag;
        }

        #endregion
    }
}