using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Net;
//using System.Web.Mail;
using System.Net.Mail;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Web.UI;
public partial class Time_AttendanceApp_ForgotPassword : System.Web.UI.Page
{
    ResourceManager rm;
    ResetPwd obj_ResetPwd = new ResetPwd();
    cls_Common objcommon = new cls_Common();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    string strSetAlert;
    byte[] inputBytes, hashBytes;
    Base.BasePage objBase = new Base.BasePage();
    CultureInfo ci;
    string strScript = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //get the culture info to set the language
        rm = new ResourceManager("Resources.Resource_EN", System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        if (!IsPostBack)
        {
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("MaxLengthh", strSetAlert);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_EmpId.Text = rm.GetString("email_EmployeeId", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("ReqField_EmpIds", ci);
        btnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        btn_GetPwd.Text = rm.GetString("Caption_GetPassword", ci);
    }
    protected void btn_GetPwd_Click(object sender, EventArgs e)
    {
        string EmpCode = txt_EmployeeId.Text;
        string strEmailId = string.Empty;
        DataSet dsEmployee = new DataSet();
        dsEmployee = objEmployeeEventData.SelectSelectEmailByEmpId(EmpCode);
        if (dsEmployee.Tables[0].Rows.Count > 0)
        {
            if (dsEmployee.Tables[0].Rows[0]["EmailId"] != DBNull.Value)
            {
                strEmailId = dsEmployee.Tables[0].Rows[0]["EmailId"].ToString();
            }
        }
        else
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EmployeeIdnotfound", ci) + "');";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertForgetPW", strScript);
            return;
        }
        if (strEmailId == string.Empty)
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_NoEmailIdfound", ci) + "');";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertForgetPW", strScript);
            return;
        }
        Random objRandom = new Random();
        int intRandom = objRandom.Next(1000);
        string Password = NewPassword();
        string EncryptPassword = PasswordEncryption(Password);
        int intPwd = ChangePassword(EmpCode, EncryptPassword);
        if (intPwd >= 1)
        {
            sendEmailToEmployee(strEmailId, Password);
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_NewpasswordforTimecheckhasbeen", ci) + "');";
            strScript = strScript + "location.replace('Index.aspx');";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertForgetPW", strScript);
        }
        else if (intPwd == 0)
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_EmployeeIdnotfound", ci) + "');";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertForgetPW", strScript);
        }
    }
    //Generate New Password
    public string NewPassword()
    {
        int intMaxlength = 15;
        string PasswordChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()-+=";
        string strPass = "";
        Random ObjRandom = new Random();
        for (int x = 0; x < intMaxlength; x++)
        {
            int i = Convert.ToInt32(Math.Floor(Convert.ToDouble(ObjRandom.Next(76))));
            strPass += PasswordChars[i];
        }
        return strPass;
    }
    // Encoding Password
    public string PasswordEncryption(string EPassword)
    {
        MD5 md5 = MD5.Create();
        inputBytes = Encoding.ASCII.GetBytes(EPassword);
        hashBytes = md5.ComputeHash(inputBytes);
        EPassword = Convert.ToBase64String(hashBytes);
        return EPassword;
    }
    //Change Password in Database for that particular employee
    public int ChangePassword(string EmployeeId, string EncryptPassword)
    {
        int count = 0;
        count = obj_ResetPwd.ResetPassword(EmployeeId, EncryptPassword);
        return count;
        return 0;
    }
    private void sendEmailToEmployee(string ToEmailId, string Password)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            if (Convert.ToInt32(ConfigurationManager.AppSettings.Get("isRequiredEmail")) == 1)
            {
                mailMessage.From = new MailAddress(ConfigurationManager.AppSettings.Get("FromId"));
                mailMessage.To.Add(new MailAddress(ToEmailId));
                mailMessage.Subject = "Reg : New Password for Timecheck Application";
                mailMessage.Body = "Your New Password is : " + Password;
                objcommon.SendMail(mailMessage);
            }
            else
            {
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('Email notification is not in enabled mode, so please your contact administrator');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertForgetPW", strScript);
            }
        }
        catch (Exception Ex)
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert(" + Ex.ToString() + ");";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("AlertForgetPW", strScript);
        }
    }
}
