from PySide6.QtGui import Qt
from PySide6.QtWidgets import QHBoxLayout, QPushButton, QSpinBox, QVBoxLayout, QWidget, QGridLayout, QLabel

from Cart import ShopItem, ShopItemType
from Movie import Movie, createImage
from config import cart

class MoviePurchase(QWidget):
    """
    Widget that helps the user to select the amount of tickets they would like.
    Also attempts to upsell the user with popcorn and soft drink.
    """
    def __init__(self, movie: Movie):
        super().__init__()

        self.movie: Movie = movie

        bodylayout = QVBoxLayout()
        sidebysidelayout = QHBoxLayout()

        self.setMaximumWidth(800)
        self.setMaximumHeight(600)

        self.setMinimumWidth(800)
        self.setMinimumHeight(600)

        # Left layout - contains the box for the quantity of tickets
        leftlayout = QGridLayout()
        leftlayout.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        leftlayout.addWidget(QLabel("Amount of tickets to get:"), 0, 0)
        
        self.tickets: QSpinBox = QSpinBox()
        self.tickets.setRange(1, 50)
        self.tickets.setSingleStep(1)
        self.tickets.setValue(1)
        leftlayout.addWidget(self.tickets, 1, 0)

        # Right layout - contains the box for upselling the customer
        rightlayout = QGridLayout()
        rightlayout.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        rightlayout.addWidget(QLabel("The Snackbar"), 0, 0)

        # Popcorn
        rightlayout.addWidget(QLabel("Popcorn ($8.50 per unit)"), 1, 0)
        self.popcorn: QSpinBox = QSpinBox()
        self.popcorn.setRange(0, 50)
        self.popcorn.setSingleStep(1)
        self.popcorn.setValue(1)
        rightlayout.addWidget(self.popcorn, 2, 0)
        
        # Frozen Coke
        rightlayout.addWidget(QLabel("Frozen Coke ($4.50 per unit)"), 3, 0)
        self.frozencoke: QSpinBox = QSpinBox()
        self.frozencoke.setRange(0, 50)
        self.frozencoke.setSingleStep(1)
        self.frozencoke.setValue(1)
        rightlayout.addWidget(self.frozencoke, 4, 0)
        
        # Lolly bag
        rightlayout.addWidget(QLabel("Lolly Bag ($3.50 per unit)"), 5, 0)
        self.lollybag: QSpinBox = QSpinBox()
        self.lollybag.setRange(0, 50)
        self.lollybag.setSingleStep(1)
        self.lollybag.setValue(1)
        rightlayout.addWidget(self.lollybag, 6, 0)
        

        sidebysidelayout.addLayout(leftlayout)
        sidebysidelayout.addLayout(rightlayout)

        # Button to add stuff to cart
        self.cartButton: QPushButton = QPushButton("Add to Cart")
        _ = self.cartButton.clicked.connect(self.buttonCallback)

        bodylayout.addLayout(sidebysidelayout)
        bodylayout.addWidget(self.cartButton)

        self.setLayout(bodylayout)
        self.setWindowTitle("Adding movie to cart")

    def buttonCallback(self):
        global cart
        cart.addItem(ShopItem(ShopItemType.Movie, 1, self.movie))

        print("Yay adding to cart")
        confirmLayout = QVBoxLayout()
        confirmLayout.addWidget(QLabel("Added to cart!"))
        self.setLayout(confirmLayout)

        pass

class MovieView(QWidget):
    """
    A QWidget which shows details about a given movie.
    Shows cover image, title, price, description.
    Also provided a button for buying tickets to the movie.
    """
    def __init__(self, movie: Movie):
        super().__init__()

        self.movie: Movie = movie

        self.mp: MoviePurchase = MoviePurchase(self.movie)

        self.setMaximumWidth(800)
        self.setMaximumHeight(600)
        self.setWindowTitle("Movie Information")

        # Create layouts
        bodylayout = QHBoxLayout()
        contentlayout = QGridLayout()

        # Create labels and buttons
        label1 = QLabel(f"Buying a ticket for {movie.name}\nFor cost ${movie.price}\n\n{movie.description}")
        label1.setWordWrap(True)
        label2 = createImage(movie.image_path)

        button1 = QPushButton("Add to cart")
        button1.setMaximumWidth(100)
        _ = button1.clicked.connect(self.buyCallback)

        contentlayout.addWidget(label1, 0, 0, alignment=Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
        contentlayout.addWidget(button1, 1, 0, alignment=Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignLeft)

        bodylayout.addWidget(label2)
        bodylayout.addLayout(contentlayout)

        self.setLayout(bodylayout)

    def buyCallback(self):
        self.mp = MoviePurchase(self.movie)
        self.mp.show()


