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 Password_Remember : Form
    {
        public Password_Remember()
        {
            InitializeComponent();
        }
        SqlConnection objSqlConnection;
        SqlCommand objSQLcommand;
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        string Answer = "";
        string Password = "";
        private void button1_Click(object sender, EventArgs e)
        {
            if (ISIN(textBox1.Text))
            {
                textBox1.Enabled = button1.Enabled = false;
                try
                {
                    objSQLcommand.CommandText = "select * from Users Where UserName=@";
                    objSQLcommand.Parameters.Clear();
                    objSQLcommand.Parameters.AddWithValue("@", textBox1.Text);
                    SqlDataReader DA = objSQLcommand.ExecuteReader();
                    DA.Read();
                    textBox2.Text = DA[4].ToString();
                    Answer = Encrypt.DecryptStringAES(DA[5].ToString(),"X2");
                    Password = Encrypt.DecryptStringAES(DA[2].ToString(),"X2");
                    textBox2.Enabled = textBox3.Enabled = button2.Enabled = button3.Enabled = true;
                    if (DA.IsClosed == false) DA.Close();
                }
                catch (Exception er)
                {

                    MessageBox.Show(er.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Close();
                }
                timer1.Start();
            }
            else
            {
                MessageBox.Show("نام کاربری معتبر نیست لطفا نام کاربری خود را صحیح وارد کنید \nاگر تا کنون حساب کاربری نساخته اید می توانید از صفحه ورود اقدام با ساخت ان کنید ,","Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                textBox1.Clear();
                textBox1.Select();
            }
        }
        public Boolean ISIN(string UserName)
        {
            objSQLcommand.CommandText = "select * from Users Where UserName=@";
            objSQLcommand.Parameters.Clear();
            objSQLcommand.Parameters.AddWithValue("@", UserName);
            SqlDataReader D = objSQLcommand.ExecuteReader();
            Boolean F = D.HasRows;
            if (!D.IsClosed) D.Close();
            return F;

        }
        String DataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Pars Contacts\DataBase.mdf";
        private void Password_Remember_Load(object sender, EventArgs e)
        {
            objSqlConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename="+DataDirectory+";Integrated Security=True;Connect Timeout=30;User Instance=True");
            try
            {
                objSqlConnection.Open();
                objSQLcommand = new SqlCommand();
                objSQLcommand.Connection = objSqlConnection;
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message, "Error");
                this.Close();
            }
        }
        int i = 139;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (i != 220)
            {
                i++;
                this.Size = new Size(351, i);
            }
            else
            {
                
                timer1.Stop();
                i = 139;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Enabled = textBox3.Enabled = button2.Enabled = false;
            timer2.Start();
        }
        int ib = 220;
        private void timer2_Tick(object sender, EventArgs e)
        {
            if(ib != 127)
            {
                ib--;
                this.Size = new Size(351, ib);
            }
            else
            {
                timer2.Stop();
                button1.Enabled = textBox1.Enabled = true;
                ib = 220;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        int ibs=220;
        private void timer3_Tick(object sender, EventArgs e)
        {
            if (ibs != 300)
            {
                ibs++;
                this.Size = new Size(351, ibs);
            }
            else
            {
                timer3.Stop();
                button1.Enabled = textBox1.Enabled = button3.Enabled = textBox3.Enabled = textBox2.Enabled = button2.Enabled = false; ;
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox3.Text.ToLower() ==Answer.ToLower())
            {
                
                timer3.Start();
                textBox4.Text = Password;
            }
            else
            {
                MessageBox.Show("پاسخ ورودی با پاسخ شما مطابقت ندارد","Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                textBox3.Clear();
                textBox3.Select();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                button1_Click(null, null);
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                button2_Click(null, null);
            }
        }
    }
}
