from PyQt5 import QtCore, QtGui, QtWidgets
import cv2
import sys
from CameraStream import YOLOv8CameraStream  # Import the YOLOv8CameraStream class

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1090, 675)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # Create a horizontal layout for the main window
        self.layout = QtWidgets.QHBoxLayout(self.centralwidget)

        # Left Column (Can be empty or have some widgets)
        self.left_column = QtWidgets.QWidget(self.centralwidget)
        self.left_layout = QtWidgets.QVBoxLayout(self.left_column)
        
        # You can add more widgets to the left column here if needed
        self.label = QtWidgets.QLabel("Left Column (Empty)", self.left_column)
        self.left_layout.addWidget(self.label)
        
        # Add left column widget to the main layout
        self.layout.addWidget(self.left_column)

        # Right Column
        self.right_column = QtWidgets.QWidget(self.centralwidget)
        self.right_layout = QtWidgets.QVBoxLayout(self.right_column)

        # Add your widgets to the right layout
        self.AuftragsdetailsTable = QtWidgets.QTableWidget(self.right_column)
        self.AuftragsdetailsTable.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.AuftragsdetailsTable.setColumnCount(5)
        self.AuftragsdetailsTable.setRowCount(1)
        self.right_layout.addWidget(self.AuftragsdetailsTable)

        self.textEdit = QtWidgets.QTextEdit(self.right_column)
        self.right_layout.addWidget(self.textEdit)

        self.bauteilTypBtn = QtWidgets.QPushButton("Bauteiltype erkennen", self.right_column)
        self.right_layout.addWidget(self.bauteilTypBtn)

        self.bauteiltypTextbox = QtWidgets.QTextEdit(self.right_column)
        self.right_layout.addWidget(self.bauteiltypTextbox)

        self.label_3 = QtWidgets.QLabel("Bezeichnung", self.right_column)
        self.right_layout.addWidget(self.label_3)

        self.label_4 = QtWidgets.QLabel("Pos.-Nr:", self.right_column)
        self.right_layout.addWidget(self.label_4)

        self.textEdit_3 = QtWidgets.QTextEdit(self.right_column)
        self.right_layout.addWidget(self.textEdit_3)

        self.checkBox = QtWidgets.QCheckBox("Bauteiltype locked", self.right_column)
        self.right_layout.addWidget(self.checkBox)

        # Create the QGraphicsView
        self.graphicsView = QtWidgets.QGraphicsView(self.right_column)
        self.right_layout.addWidget(self.graphicsView)

        # Add right column widget to the main layout
        self.layout.addWidget(self.right_column)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.bauteilTypBtn.setText(_translate("MainWindow", "Bauteiltype erkennen"))
        # Add more translations as needed

class CameraStreamApp(QtWidgets.QMainWindow):
    def __init__(self, ui):
        super().__init__()
        self.ui = ui
        self.yolo_stream = None  # Initialize YOLOv8CameraStream as None
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.ui.bauteilTypBtn.clicked.connect(self.start_camera)  # Connect the button to start the camera
        self.scene = QtWidgets.QGraphicsScene(self)
        self.ui.graphicsView.setScene(self.scene)

    def start_camera(self):
        # Start the YOLOv8 camera stream (only if not already started)
        if self.yolo_stream is None:
            self.yolo_stream = YOLOv8CameraStream(model_path="models/yolov8m_seg_e300.pt", logging_level="high")
            self.timer.start(30)  # Start the timer to update the frame every 30ms (about 33 FPS)

    def update_frame(self):
        # Update the frame from YOLOv8 stream
        if self.yolo_stream:
            ret, frame = self.yolo_stream.cap.read()  # Capture frame from YOLOv8 stream

            if ret:
                # Process frame via YOLO model
                processed_frame = self.yolo_stream.process_frame(frame)

                # Convert the frame from BGR (OpenCV format) to RGB
                frame = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)

                # Convert the frame to QImage
                h, w, ch = frame.shape
                bytes_per_line = ch * w
                qt_image = QtGui.QImage(frame.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)

                # Add the QImage to a QPixmap
                pixmap = QtGui.QPixmap.fromImage(qt_image)

                # Get the size of the graphicsView and scale the pixmap to fit
                view_size = self.ui.graphicsView.size()
                scaled_pixmap = pixmap.scaled(view_size, QtCore.Qt.KeepAspectRatio)

                # Update the scene with the scaled pixmap
                self.scene.clear()
                self.scene.addPixmap(scaled_pixmap)

    def closeEvent(self, event):
        # Release the camera when the application is closed
        if self.yolo_stream is not None:
            self.yolo_stream.cap.release()
        event.accept()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    
    # Initialize the CameraStreamApp with the UI
    camera_app = CameraStreamApp(ui)
    
    MainWindow.show()
    sys.exit(app.exec_())
