import json
import uuid

from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton

class Payment(QWidget):
    """
    Widget that allows the user to enter their payment details.
    """

    widget: QWidget | None = None

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

        self.cart = cart

        self.setWindowTitle("Payment")

        layout = QVBoxLayout()
        layout.addWidget(QLabel("Payment"))
        layout.addWidget(QLabel(f"Total: ${cart.total():.2f}"))

        self.name = QLineEdit()
        self.name.setPlaceholderText("John Doe")
        self.number = QLineEdit()
        self.number.setPlaceholderText("1234 5678 9012 3456")
        self.cvv = QLineEdit()
        self.cvv.setPlaceholderText("123")
        self.expiry = QLineEdit()
        self.expiry.setPlaceholderText("12/26")

        layout.addWidget(QLabel("Name:"))
        layout.addWidget(self.name)

        layout.addWidget(QLabel("Credit Card Number:"))
        layout.addWidget(self.number)

        layout.addWidget(QLabel("CVV:"))
        layout.addWidget(self.cvv)

        layout.addWidget(QLabel("Expiry Date:"))
        layout.addWidget(self.expiry)

        submit = QPushButton("Submit")
        submit.clicked.connect(self.submit)
        layout.addWidget(submit)

        self.setLayout(layout)

    def submit(self):
        if self.name.text() == "":
            self.widget = QWidget()
            self.widget.setWindowTitle("Payment")
            layout = QVBoxLayout()
            layout.addWidget(QLabel("Please enter your name"))
            self.widget.setLayout(layout)
            self.widget.show()
            return
        if self.number.text() == "":
            self.widget = QWidget()
            self.widget.setWindowTitle("Payment")
            layout = QVBoxLayout()
            layout.addWidget(QLabel("Please enter your credit card number"))
            self.widget.setLayout(layout)
            self.widget.show()
            return
        if self.cvv.text() == "":
            self.widget = QWidget()
            self.widget.setWindowTitle("Payment")
            layout = QVBoxLayout()
            layout.addWidget(QLabel("Please enter your CVV"))
            self.widget.setLayout(layout)
            self.widget.show()
            return
        if self.expiry.text() == "":
            self.widget = QWidget()
            self.widget.setWindowTitle("Payment")
            layout = QVBoxLayout()
            layout.addWidget(QLabel("Please enter your expiry date"))
            self.widget.setLayout(layout)
            self.widget.show()
            return

        # If this was a real payment system, we would check all the details here
        # Luckily, this is just an assignment, so we'll just assume it's valid

        paymentId = uuid.uuid4()

        self.widget = QWidget()
        self.widget.setWindowTitle("Payment")
        layout = QVBoxLayout()
        layout.addWidget(QLabel(f"Payment Successful!\n\nYour payment ID is: {paymentId}"))
        self.widget.setLayout(layout)
        self.widget.show()
        self.close()

        # JSONify all the details
        dictionary = {
            "cardInfo": {
                "name": self.name.text(),
                "number": self.number.text(),
                "cvv": self.cvv.text(),
                "expiry": self.expiry.text()
            },
            "cart": []
        }
        for item in self.cart.contents:
            dictionary["cart"].append({
                "type": item.type.name,
                "quantity": item.quantity,
                "total": item.total(),
            })
            if item.movie is not None:
                dictionary["cart"][-1]["movie"] = item.movie.name

        string = json.dumps(dictionary, indent=4)
        with open(f"payment-{paymentId}.json", "w") as f:
            f.write(string)

        self.cart.contents.clear()

