using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace The_Contacts
{
    public partial class ChangePassword : Form
    {
        public ChangePassword(string userid)
        {
            InitializeComponent();
            UserID = userid;
            ChangePassword.CheckForIllegalCrossThreadCalls = false;
        }
        SqlConnection objSqlConnection;
        SqlCommand objSQLcommand;
        string UserID;
        String DataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Pars Contacts\DataBase.mdf";
        System.Speech.Synthesis.SpeechSynthesizer Speech = new System.Speech.Synthesis.SpeechSynthesizer();
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (Encrypt.DecryptStringAES(Password,"X2") == textBox1.Text)
            {
                button1.Enabled = false;
                textBox2.Select();
                button2.Enabled = true;
            }
            else
            {

                toolTip1.Show("کلمه عبور صحیح نیست", textBox1, 0, 0);
                
        
            }
        }
        string Password;
       


        public string RandomNumber()
        {
            Random rnd = new Random();
            return rnd.Next(10000, 99999).ToString();
        }
        string Captcha;
        private void ChangePassword_Load(object sender, EventArgs e)
        {
            RefreshCaptcha();
            objSqlConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + DataDirectory + ";Integrated Security=True;User Instance=True");
            try
            {
                objSqlConnection.Open();
                objSQLcommand = new SqlCommand();
                objSQLcommand.Connection = objSqlConnection;

                objSQLcommand.CommandText = "select * from Users where id=@";
                objSQLcommand.Parameters.AddWithValue("@", UserID);
                SqlDataReader Dr = objSQLcommand.ExecuteReader();
                Dr.Read();
                Password = Dr[2].ToString();
                lblID.Text = Dr[1].ToString();
                if (Dr.IsClosed == false) Dr.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\nPlease try later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.Close(); }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == textBox3.Text)
            {

            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text == textBox2.Text)
            {
                picConfirm.Hide();
            }
            else
            {
                picConfirm.Show();
            }
        }

        private void commandLink1_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBox1.UseSystemPasswordChar = checkBox1.Checked;
        }
        void RefreshCaptcha()
        {
            CaptchaImage.CaptchaImage C = new CaptchaImage.CaptchaImage(RandomNumber(), 100, 39);
            pictureBox1.Image = C.Image;
            Captcha = C.Text;
        }
        private void button2_Click_1(object sender, EventArgs e)
        {
            RefreshCaptcha();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            btnRetry.Enabled = btnSpeek.Enabled = false;
            System.Threading.Thread T = new System.Threading.Thread(new System.Threading.ThreadStart(SpeekCaptcha));
            T.Start();
            txtSecurityCode.Select();
           
        }
        void SpeekCaptcha()
        {
            Speech.Volume = 100;
            foreach (Char objChar in Captcha.ToCharArray())
            {
                Speech.Speak(objChar.ToString());
            }
            btnSpeek.Enabled = btnRetry.Enabled = true;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            if (textBox3.Text.Trim() == "")
            {
                MessageBox.Show("لطفا یک رمز را وارد کنید", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return;
            }
            if (txtSecurityCode.Text != Captcha)
            {
                MessageBox.Show("کد امنیتی صحیح نیست", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return;
            }
            if (textBox3.Text == textBox2.Text)
            {
                picConfirm.Hide();
                try
                {
                    objSQLcommand.CommandText = "update users set password=@n where id=" + UserID;
                    objSQLcommand.Parameters.AddWithValue("@n", Encrypt.EncryptStringAES(textBox3.Text, "X2"));
                    objSQLcommand.ExecuteNonQuery();
                    FarsiDate.FarsiDateTime F = new FarsiDate.FarsiDateTime();
                    MessageBox.Show("کلمه عبور شما در این تاریخ تغییر کرد\n" + F.Today, "User Setting", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error in Save Setting\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Close();
                }
            }
            else
            {
                picConfirm.Show();
                textBox3.Select();
            }
        }
    }
}
