using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using static System.Net.Mime.MediaTypeNames;

namespace WindowsFormsApp1
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
            

        }
        //connect to database
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=usersdb.mdb");
        OleDbCommand cmd = new OleDbCommand();
        OleDbDataAdapter da = new OleDbDataAdapter();
        private void RegisterButton_Click(object sender, EventArgs e)
        {
            string name = NameTextBox.Text;
            string email = EmailTextBox.Text;
            string address = AddressTextBox.Text;
            string phoneNumber = PhoneNumberTextBox.Text;
            string username = UsernameTextBox.Text;
            string password = PasswordTextBox.Text;
            string confirmpassword = ConPasswordTextBox.Text;
            

            // Check if registration information is valid
            bool isValid = ValidateRegistration(name, email, address, phoneNumber, username, password);

            if (isValid)
            {
                con.Open();
                bool usernameExists = CheckUsernameExists(UsernameTextBox, con);
                
                if (usernameExists)
                {
                    MessageBox.Show("Username already exists in database.");


                }
                else
                {

                    USERS newUser = new USERS(name, username, password, email, address, phoneNumber ,"Customer");
                    {
                    };
                    InsertMyClass(newUser);
                    
                    
                    // Successful registration, do something here
                    MessageBox.Show("Registration successful!");

                    //emptying the fields
                    NameTextBox.Text = "";
                    EmailTextBox.Text = "";
                    AddressTextBox.Text = "";
                    PhoneNumberTextBox.Text = "";
                    UsernameTextBox.Text = "";
                    PasswordTextBox.Text = "";
                    ConPasswordTextBox.Text = "";
                    this.Hide();
                    new Login().Show();
                }
                con.Close();
            }
            else if(password != confirmpassword)
            {
                MessageBox.Show("Passwords do not match.");
            }
            else if(password.Length < 8 || password.Length > 16)
            {
                MessageBox.Show("Your password must be between 8 and 16 characters long.");
            }
            else if (!password.Any(char.IsLower) || !password.Any(char.IsUpper))
            {
                MessageBox.Show("Your password must contain at least one lowercase and one uppercase letter.");
            }
            else if(!Regex.IsMatch(username, "^[a-zA-Z0-9]*$"))
            {
                MessageBox.Show("Username must only contain letters and numbers.");
            }
            else
            {
                // Failed registration, display error message
                MessageBox.Show("All required fields must be filled.");
            }
            

        }
        public void InsertMyClass(USERS newUser)
        {
            using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=usersdb.mdb"))
            {
                connection.Open();
                using (OleDbCommand command = new OleDbCommand("INSERT INTO tbl_dbusers (username, password, FullName, Email, Address, PhoneNumber, UserType) \r\nVALUES (@username, @password, @name, @Email, @Address, @PhoneNumber, @UserType)\r\n", connection))

                {
                    command.Parameters.AddWithValue("@username", newUser.Username);
                    command.Parameters.AddWithValue("@password", newUser.Password);
                    command.Parameters.AddWithValue("@Email", newUser.Email);
                    command.Parameters.AddWithValue("@Address", newUser.Address);
                    command.Parameters.AddWithValue("@PhoneNumber", newUser.PhoneNumber);
                    command.Parameters.AddWithValue("@UserType", newUser.UserType);
                    command.Parameters.AddWithValue("@FullName", newUser.Name);
                    command.ExecuteNonQuery();
                }
            }
        }
        //checks if username exists in the database

        public bool CheckUsernameExists(TextBox UsernameTextBox, OleDbConnection connection)
        {
            
            string username = UsernameTextBox.Text.Trim();
            string query = "SELECT COUNT(*) FROM tbl_dbusers WHERE username = @username";

            OleDbCommand command = new OleDbCommand(query, connection);
            command.Parameters.AddWithValue("@username", username);

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

            return count > 0;
            
        }
        
        

        private bool ValidateRegistration(string name, string email, string address, string phoneNumber, string username, string password)
        {

            //checks if all fields are not empty, password match and meet conditions
            return !string.IsNullOrEmpty(name) &&
                   !string.IsNullOrEmpty(email) &&
                   !string.IsNullOrEmpty(address) &&
                   !string.IsNullOrEmpty(phoneNumber) &&
                   !string.IsNullOrEmpty(username) &&
                   !string.IsNullOrEmpty(password) &&
                   password == ConPasswordTextBox.Text &&
                   password.Length > 8 && password.Length < 16 &&
                   password.Any(char.IsLower) &&
                   password.Any(char.IsUpper) &&
                   //check if username  contains only letters and numbers
                   Regex.IsMatch(username, "^[a-zA-Z0-9]*$");



        }
    }
}
check for any error and fix it giving syntax error in insert into stament for this line command.ExecuteNonQuery();