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

namespace WMS_project
{

    public partial class loginScreen : Form
    {
        private string connectionString;
        private SqlConnection connection;
        public loginScreen()
        {
            InitializeComponent();
            textBoxPassword.UseSystemPasswordChar = true; //to hide passwords in textbox
        }

        private void TextBoxUsername_TextChanged(object sender, EventArgs e)
        {

        }

        private void ButtonLogin_Click(object sender, EventArgs e)
        {

            string username = textBoxUsername.Text;
            string password = textBoxPassword.Text;

            // Validate username and password
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("Please enter both username and password.");
                return;
            }

            // Check the credentials in the database
            if (CheckCredentials(username, password))
            {
                // Login successful, open the InformationManager form
                mainMenu mainmenuForm = new mainMenu(connectionString);
                mainmenuForm.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Invalid username or password.");
            }
        }

        private bool CheckCredentials(string username, string password)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    // Perform a SELECT query to check if the username and password match
                    string query = "SELECT COUNT(*) FROM Userinfo WHERE Username = @Username AND Password = @Password";
                    using (SqlCommand cmd = new SqlCommand(query, connection))
                    {
                        cmd.Parameters.AddWithValue("@Username", username);
                        cmd.Parameters.AddWithValue("@Password", password);

                        int count = (int)cmd.ExecuteScalar();

                        return count > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                return false;
            }
        }

        private void TextBoxUsername_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ButtonLogin_Click(sender, e);
            }
        }
        private void TextBoxPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ButtonLogin_Click(sender, e);
            }
        }

        private void PasswordLabel_Click(object sender, EventArgs e)
        {

        }

        private void ButtonClear_Click(object sender, EventArgs e)
        {
            textBoxUsername.Text = ""; // Clears the text in the username textbox
            textBoxPassword.Text = ""; // Clears the text in the password textbox
        }
        private void ButtonForgotPassword_Click(object sender, EventArgs e)
        {

        }

        private void ButtonExit_Click(object sender, EventArgs e)
        {
            Application.Exit(); // Close the entire application

        }

        private void ButtonForgotPassword_Click_1(object sender, EventArgs e)
        {
            forgotPassword forgotForm = new forgotPassword();
            forgotForm.Show();
        }

        private void userDBPath_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string userInput = userDBPath.Text.Trim();

                if (!string.IsNullOrEmpty(userInput))
                {
                    // Replacing the directory in the connection string
                    connectionString = $@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={userInput}\WMSDB.mdf;Integrated Security=True;";
                }
                else
                {
                    MessageBox.Show("Please provide a valid directory.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
    }
}