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;
using System.IO;

namespace Library_Red_Diamonds
{
    public partial class login : Form
    {
        static SqlConnection con = new SqlConnection(Connection.con);
        public Boolean Result = false;
        public login()
        {
            InitializeComponent();
        }


        private string CalculateMD5Hash(string input)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            Byte[] inputByte = System.Text.Encoding.ASCII.GetBytes(input);
            Byte[] hash = md5.ComputeHash(inputByte);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
                sb.Append(hash[i].ToString("x2"));
            return sb.ToString().ToLower();
        }

        public void loginuser()
        {
            SqlCommand sqlcmdLogin = new SqlCommand();
            sqlcmdLogin = new SqlCommand("SelectUsers", con);
            sqlcmdLogin.CommandType = CommandType.StoredProcedure;
            SqlDataReader Myreader;
            sqlcmdLogin.Parameters.AddWithValue("@usernames", txt_username.Text);
            sqlcmdLogin.Parameters.AddWithValue("@passwords", CalculateMD5Hash(txt_password.Text));
            con.Open();
            Myreader = sqlcmdLogin.ExecuteReader();
            if (Myreader.Read())
            {
                Result = true;
                AccessLevel AC = new AccessLevel();
                AccessLevel.UserName = txt_username.Text;
                AC.SetAccess(txt_username.Text);
                Myreader.Close();
                this.Close();
                main main = new main();
                main.Show();

            }
            else
            {
                MessageBox.Show("نام کاربری یا کلمه عبور صحیح نیست");
            }
            con.Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            loginuser();
        }

        private void btn_keyborad_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("osk.exe");
        }

        private void ch_show_pass_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
        {
            if (ch_show_pass.Checked)
            txt_password.PasswordChar = '\0';
            else
                txt_password.PasswordChar = '*';
        }

        private void login_FormClosing(object sender, FormClosingEventArgs e)
        {
 
        }

        private void login_Load(object sender, EventArgs e)
        {
            txt_username.Focus();

        }

        private void login_KeyDown(object sender, KeyEventArgs e)
        {
          
        }

        private void txt_password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                loginuser();
            }
            else if (e.KeyData == Keys.Escape)
            {
                Application.Exit();
            }
        }

    }
}