from PySide6.QtGui import QCloseEvent
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, \
    QMessageBox
from typing import final
import sys

from Movie import movies
from MovieView import MovieView
from MovieButton import MovieButton
from Cart import Cart, cart

# Main Window
@final
class MainWindow(QMainWindow):
    """
    The main window for Max's movie tickets application.
    Usage:
        window = MainWindow()
        window.show()
    """

    currentMovie: MovieView | None = None

    def __init__(self, cart: Cart):
        super().__init__()

        self.setWindowTitle("Cinema Tickets")

        # Main layout, contains button and movies
        mainLayout = QVBoxLayout()

        # Title bar layout, contains the cart button and instructions on
        # how to pick a movie
        titleLayout = QHBoxLayout()

        titleLayout.addWidget(QLabel("Welcome to Max's Theatre! Click a movie to view information and buy tickets!"))
        titleLayout.addStretch()

        button = QPushButton("View Cart and Checkout")
        button.setMaximumWidth(200)
        button.clicked.connect(lambda: cart.show())
        titleLayout.addWidget(button)

        mainLayout.addLayout(titleLayout)

        # Create the layout for all the movies
        layout = QHBoxLayout()

        # Create the buttons for each movie
        self.spiderman = MovieButton(movies["spiderman"], self.buySpiderman)
        layout.addWidget(self.spiderman)
        self.cars = MovieButton(movies["cars2"], self.buyCars)
        layout.addWidget(self.cars)
        self.leo = MovieButton(movies["leo"], self.buyLeo)
        layout.addWidget(self.leo)
        self.hairypotty = MovieButton(movies["hairypotty"], self.buyHairyPotty)
        layout.addWidget(self.hairypotty)

        mainLayout.addLayout(layout)

        centralWidget = QWidget()
        centralWidget.setLayout(mainLayout)
        self.setCentralWidget(centralWidget)

    # Callback functions to set the movie
    def buySpiderman(self):
        self.currentMovie = MovieView(movies["spiderman"])
        self.currentMovie.show()

    def buyLeo(self):
        self.currentMovie = MovieView(movies["leo"])
        self.currentMovie.show()

    def buyCars(self):
        self.currentMovie = MovieView(movies["cars2"])
        self.currentMovie.show()

    def buyHairyPotty(self):
        self.currentMovie = MovieView(movies["hairypotty"])
        self.currentMovie.show()

    def closeEvent(self, event: QCloseEvent):
        global cart
        if len(cart.contents) == 0:
            event.accept()
        else:
            reply = QMessageBox.question(self, 'Items in cart', "Your cart is not empty! It will be lost if you close the app.\n\nAre you sure you want to close the app?", buttons=QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, defaultButton=QMessageBox.StandardButton.No)
            if reply == QMessageBox.StandardButton.Yes:
                event.accept()
            else:
                event.ignore()


app = QApplication(sys.argv)
window = MainWindow(cart)
window.show()
app.exec()
