import mysql.connector
from customtkinter import *

class DatabaseMediator:
    """Handles the connection and interaction with the MySQL database."""
    def __init__(self, host, user, password, database, table_name,*columns):
        """
        Initializes the DatabaseMediator object.
        
        Args:
            host (str): The MySQL server host.
            user (str): The username for the database connection.
            password (str): The password for the database connection.
            database (str): The name of the database.
            table_name (str): The name of the table storing the passwords.
            *columns (str): Variable-length argument list of column names in the table.
        """
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.table_name = table_name
        self.columns = columns
        self.mydb = mysql.connector.connect(
        host=self.host,
        user=self.user,
        password=self.password,
        database=self.database
        )
        self.mycursor = self.mydb.cursor()

    def insert_data(self, *args):
        """
        Inserts data into the MySQL table.
        
        Args:
            *args: Variable-length argument list of values to be inserted.
        """
        self.mycursor.execute(f"INSERT INTO {self.table_name} ({', '.join(list(self.columns))}) VALUES {args}")
        self.mydb.commit()
        
    def retrieve_data(self):
        """
        Retrieves all data from the MySQL table.
        
        Returns:
            list: A list of tuples representing the retrieved data.
        """
        self.mycursor.execute(f"SELECT * FROM {self.table_name}")
        myresult = self.mycursor.fetchall()
        return myresult

class PasswordManager(CTk):
    """The main GUI class for the Password Manager application."""
    def __init__(self):
        """Initializes the PasswordManager GUI."""
        super().__init__()
        self.title("Password Manager")
		#change the host, username, and password to yours
        self.database_connection = DatabaseMediator("HOST", "USERNAME", "PASSWORD", "password_manager", "passwords", "account_name", "account_id", "account_password")
        
        self.frame = CTkFrame(self)
        self.frame.grid(row=0, columnspan=2, padx=10, pady=10)

        self.account_name = CTkEntry(self.frame, placeholder_text="Account name")
        self.account_name.pack(padx=10, pady=10)
        
        self.account_id = CTkEntry(self.frame, placeholder_text="Enter your user ID here")
        self.account_id.pack(padx=10, pady=10)

        self.account_password = CTkEntry(self.frame, placeholder_text="Enter your Password")
        self.account_password.pack(padx=10, pady=10)

        self.save_password = CTkButton(self, text="Save Password", command=self.save)
        self.save_password.grid(row=1, column=0, padx=10, pady=10)

        self.view_password = CTkButton(self, text="View Passwords", command=self.view)
        self.view_password.grid(row=1, column=1, padx=10, pady=10)

    def save(self):
        """Saves the password to the database."""
        acc_nm = self.account_name.get()
        acc_id = self.account_id.get()
        acc_password = self.account_password.get()
        self.database_connection.insert_data(acc_nm, acc_id, acc_password)
        label = CTkLabel(self, text="Passwords Saved successfully...")
        label.grid(row=2, columnspan=2, padx=10, pady=10)

    def view(self):
        """Retrieves and displays all passwords from the database."""
        passwords = self.database_connection.retrieve_data()
        result = ""
        for i in range(len(passwords)):
            result += f"\n{i+1}. {passwords[i]}"
        result_frame = CTkFrame(self)
        result_frame.grid(row=3, columnspan=2, padx=10, pady=10)
        res_label = CTkLabel(result_frame, text=result)
        res_label.pack()


app = PasswordManager()
app.mainloop()