from tkinter import messagebox

import constants
import traceback
from app import App
from db import MySQLConnection

# main_gui.py - Program entry point for Part 2 GUI
# NOTE: You do not need to modify this source file

if __name__ == "__main__":
    app = None
    db = None
    try:
        # Create connection to database
        db = MySQLConnection(
            host=constants.DB_HOST,
            user=constants.DB_USER,
            password=constants.DB_PASS,
            database=constants.DB_NAME)

        # Create and start app
        app = App(db)
        app.mainloop()
    except Exception as e:
        # Display error in console
        traceback.print_exception(e)

        # Display error as message box
        errorType = f"{type(e).__name__}"
        messagebox.showerror(errorType, e.__str__())

        # Close the app window
        if app is App:
            app.destroy()

        # Raise the error again for debugger
        raise e
    finally:
        # Close database connection
        if db is MySQLConnection:
            db.close()
