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.Globalization;
using Stimulsoft.Report.Dictionary;
using Stimulsoft.Report;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;

namespace Library_Red_Diamonds
{
    public partial class users : Form
    {
        static SqlConnection con = new SqlConnection(Connection.con);

        public users()
        {
            InitializeComponent();
        }

        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter adp = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        private void clear()
        {
            txt_user.Clear();
            txt_pass.Clear();
            ch_books.Checked = false;
            ch_categorys.Checked = false;
            ch_members.Checked = false;
            Ch_memberdebits.Checked = false;
            ch_borrowbooks.Checked = false;
            ch_backbooks.Checked = false;
            ch_fines.Checked = false;
            ch_users.Checked = false;
            ch_info.Checked = false;
            ch_backups.Checked = false;
            ch_restores.Checked = false;
        }


        private void count()
        {
            //--------------------------------------------------------------------------------------------
            SqlCommand com = new SqlCommand("select count (*) from users", con);
            con.Open();
            lbl_all_users.Text = Convert.ToString((int)com.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
        }


        private void lastrecord()
        {

            con.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select top 1 * from users order by id desc", con);
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                lbl_last_user.Text = (myReader["usernames"].ToString());
            }
            myReader.Close();
            con.Close();
        }

        private void select()
        {
            SqlCommand sqlcmdLogin = new SqlCommand();
            sqlcmdLogin = new SqlCommand("SelectAccessLevel", con);
            sqlcmdLogin.CommandType = CommandType.StoredProcedure;
            SqlDataReader Myreader;
            sqlcmdLogin.Parameters.AddWithValue("@usernames", txt_user.Text);
            con.Open();
            Myreader = sqlcmdLogin.ExecuteReader();
            if (Myreader.Read())
            {
                ch_books.Checked = Myreader["books"].ToString() == "True" ? true : false;
                ch_categorys.Checked = Myreader["categorys"].ToString() == "True" ? true : false;
                ch_members.Checked = Myreader["members"].ToString() == "True" ? true : false;
                Ch_memberdebits.Checked = Myreader["memberdebits"].ToString() == "True" ? true : false;
                ch_borrowbooks.Checked = Myreader["borrowbooks"].ToString() == "True" ? true : false;
                ch_backbooks.Checked = Myreader["backbooks"].ToString() == "True" ? true : false;
                ch_fines.Checked = Myreader["fines"].ToString() == "True" ? true : false;
                ch_users.Checked = Myreader["users"].ToString() == "True" ? true : false;
                ch_info.Checked = Myreader["info"].ToString() == "True" ? true : false;
                ch_backups.Checked = Myreader["backups"].ToString() == "True" ? true : false;
                ch_restores.Checked = Myreader["restores"].ToString() == "True" ? true : false;
            }
            Myreader.Close();
            con.Close();
        }

        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();
        }

        private void AddUser()
        {
            cmd = new SqlCommand("AddUsers", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@usernames", txt_user.Text);
            cmd.Parameters.AddWithValue("@passwords", CalculateMD5Hash(txt_pass.Text));
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

        private void EditUser()
        {
            cmd = new SqlCommand("EditUsers", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", lbl_id.Text);
            cmd.Parameters.AddWithValue("@usernames", txt_user.Text);
            cmd.Parameters.AddWithValue("@passwords", CalculateMD5Hash(txt_pass.Text));
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

        private void SetAccesslevel()
        {
            SqlCommand cmd_Accesslevel = new SqlCommand();
            cmd_Accesslevel = new SqlCommand("AddAccessLevel", con);
            cmd_Accesslevel.CommandType = CommandType.StoredProcedure;
            cmd_Accesslevel.Parameters.AddWithValue("@usernames", txt_user.Text);
            cmd_Accesslevel.Parameters.AddWithValue("@books", ch_books.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@categorys", ch_categorys.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@members", ch_members.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@memberdebits", ch_fines.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@borrowbooks", ch_borrowbooks.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@backbooks", ch_backbooks.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@fines", ch_fines.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@users", ch_users.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@info", ch_info.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@backups", ch_backups.Checked);
            cmd_Accesslevel.Parameters.AddWithValue("@restores", ch_restores.Checked);
            con.Open();
            cmd_Accesslevel.ExecuteNonQuery();
            con.Close();
        }
        private void UpdateAccessLevel()
        {
            SqlCommand sqlcmdAccess = new SqlCommand();
            sqlcmdAccess = new SqlCommand("EditAccessLevel", con);
            sqlcmdAccess.CommandType = CommandType.StoredProcedure;
            sqlcmdAccess.Parameters.AddWithValue("@usernames", txt_user.Text);
            sqlcmdAccess.Parameters.AddWithValue("@books", ch_books.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@categorys", ch_categorys.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@members", ch_members.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@memberdebits", ch_fines.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@borrowbooks", ch_borrowbooks.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@backbooks", ch_backbooks.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@fines", ch_fines.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@users", ch_users.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@info", ch_info.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@backups", ch_backups.Checked);
            sqlcmdAccess.Parameters.AddWithValue("@restores", ch_restores.Checked);
            con.Open();
            sqlcmdAccess.ExecuteNonQuery();
            con.Close();
        }



        private void deleteaccess()
        {
            SqlCommand cmd_del = new SqlCommand();
            cmd_del = new SqlCommand("DeleteAccessLevel", con);
            cmd_del.CommandType = CommandType.StoredProcedure;
            cmd_del.Parameters.AddWithValue("@usernames", dgv_users.CurrentRow.Cells[1].Value.ToString());
            con.Open();
            cmd_del.ExecuteNonQuery();
            con.Close();
        }

        private void delete()
        {
            cmd = new SqlCommand("DeleteUsers", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@usernames", dgv_users.CurrentRow.Cells[1].Value.ToString());
            con.Open();
            if (MessageBox.Show("آیا می خواهید این را حذف کنید", "حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("حذف شد");
            }
            con.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (dgv_users.Rows.Count == 3)
            {
                MessageBox.Show("شما از نسخه دموی برنامه استفاده می کنید");
                clear();
                lastrecord();
                count();
                LoadUsersInfo();
            }
            else
            {
                AddUser();
                SetAccesslevel();
                clear();
                lastrecord();
                count();
                LoadUsersInfo();
            }
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            EditUser();
            UpdateAccessLevel();
            clear();
            lastrecord();
            count();
            LoadUsersInfo();
        }

        public void LoadUsersInfo()
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter("AllUsers", con);
            da.Fill(dt);
            dgv_users.DataSource = dt;

        }
        private void users_Load(object sender, EventArgs e)
        {
            LoadUsersInfo();
            lastrecord();
            count();
            clear();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            delete();
            deleteaccess();
            lastrecord();
            count();
            LoadUsersInfo();
        }

        private void btn_show_Click(object sender, EventArgs e)
        {
            LoadUsersInfo();
        }

        private void dgv_users_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            lbl_id.Text = dgv_users.CurrentRow.Cells[0].Value.ToString();
            txt_user.Text = dgv_users.CurrentRow.Cells[1].Value.ToString();
            txt_pass.Text = dgv_users.CurrentRow.Cells[2].Value.ToString();
            select();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            clear();
        }

    }
}
