from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimediaWidgets
#from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtMultimedia import QMediaPlayer
from PyQt5.QtMultimedia import QMediaContent
from PyQt5.QtCore import QUrl
import os
import third_s4_details_win
import fourth_object_detection_win
import object_detection_details

class Ui_MainWindow(object):
    def setupUi(self, MainWindow, fname, Results):
        self.Results=Results
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(824, 711)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(824, 711))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
        self.centralwidget.setSizePolicy(sizePolicy)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setContentsMargins(70, 35, 70, 70)
        self.gridLayout.setSpacing(40)
        self.gridLayout.setObjectName("gridLayout")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox_2.sizePolicy().hasHeightForWidth())
        self.groupBox_2.setSizePolicy(sizePolicy)
        self.groupBox_2.setMinimumSize(QtCore.QSize(322, 283))
        self.groupBox_2.setTitle("")
        self.groupBox_2.setObjectName("groupBox_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_2)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setSpacing(10)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.video_widget2 = QtMultimediaWidgets.QVideoWidget(self.groupBox_2)
        self.video_widget2.setMinimumSize(QtCore.QSize(320, 241))
        self.video_widget2.setObjectName("video_widget2")
        self.verticalLayout_2.addWidget(self.video_widget2)
        self.lineEdit2 = QtWidgets.QLineEdit(self.groupBox_2)
        self.lineEdit2.setEnabled(False)
        self.lineEdit2.setMinimumSize(QtCore.QSize(0, 30))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit2.setFont(font)
        self.lineEdit2.setStyleSheet("color:#00aaff;")
        self.lineEdit2.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit2.setObjectName("lineEdit2")
        self.verticalLayout_2.addWidget(self.lineEdit2)
        self.gridLayout.addWidget(self.groupBox_2, 0, 1, 1, 1)
        self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox_3.sizePolicy().hasHeightForWidth())
        self.groupBox_3.setSizePolicy(sizePolicy)
        self.groupBox_3.setMinimumSize(QtCore.QSize(322, 283))
        self.groupBox_3.setTitle("")
        self.groupBox_3.setObjectName("groupBox_3")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.groupBox_3)
        self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_3.setSpacing(10)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.video_widget3 = QtMultimediaWidgets.QVideoWidget(self.groupBox_3)
        self.video_widget3.setMinimumSize(QtCore.QSize(320, 241))
        self.video_widget3.setObjectName("video_widget3")
        self.verticalLayout_3.addWidget(self.video_widget3)
        self.lineEdit3 = QtWidgets.QLineEdit(self.groupBox_3)
        self.lineEdit3.setEnabled(False)
        self.lineEdit3.setMinimumSize(QtCore.QSize(0, 30))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit3.setFont(font)
        self.lineEdit3.setStyleSheet("color:#00aaff;")
        self.lineEdit3.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit3.setObjectName("lineEdit3")
        self.verticalLayout_3.addWidget(self.lineEdit3)
        self.gridLayout.addWidget(self.groupBox_3, 1, 0, 1, 1)
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
        self.groupBox.setSizePolicy(sizePolicy)
        self.groupBox.setMinimumSize(QtCore.QSize(322, 283))
        self.groupBox.setTitle("")
        self.groupBox.setObjectName("groupBox")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.groupBox)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(10)
        self.verticalLayout.setObjectName("verticalLayout")
        self.video_widget1 = QtMultimediaWidgets.QVideoWidget(self.groupBox)
        self.video_widget1.setMinimumSize(QtCore.QSize(320, 241))
        self.video_widget1.setObjectName("video_widget1")
        self.verticalLayout.addWidget(self.video_widget1)
        self.lineEdit1 = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit1.setEnabled(False)
        self.lineEdit1.setMinimumSize(QtCore.QSize(0, 30))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit1.setFont(font)
        self.lineEdit1.setStyleSheet("color:#00aaff; ")
        self.lineEdit1.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit1.setObjectName("lineEdit1")
        self.verticalLayout.addWidget(self.lineEdit1)
        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
        self.groupBox_4 = QtWidgets.QGroupBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox_4.sizePolicy().hasHeightForWidth())
        self.groupBox_4.setSizePolicy(sizePolicy)
        self.groupBox_4.setMinimumSize(QtCore.QSize(320, 283))
        self.groupBox_4.setTitle("")
        self.groupBox_4.setObjectName("groupBox_4")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.groupBox_4)
        self.verticalLayout_4.setContentsMargins(20, -1, 20, -1)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.result_display = QtWidgets.QLineEdit(self.groupBox_4)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.result_display.sizePolicy().hasHeightForWidth())
        self.result_display.setSizePolicy(sizePolicy)
        self.result_display.setMinimumSize(QtCore.QSize(280, 0))
        font = QtGui.QFont()
        font.setPointSize(19)
        font.setBold(True)
        font.setWeight(75)
        self.result_display.setFont(font)
        self.result_display.setStyleSheet("color:#ff007f;")
        self.result_display.setAlignment(QtCore.Qt.AlignCenter)
        self.result_display.setReadOnly(True)
        self.result_display.setObjectName("result_display")
        self.verticalLayout_4.addWidget(self.result_display)
        self.play_pause_button = QtWidgets.QPushButton(self.groupBox_4)
        self.play_pause_button.setMinimumSize(QtCore.QSize(0, 35))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(False)
        font.setWeight(50)
        self.play_pause_button.setFont(font)
        self.play_pause_button.setStyleSheet("color:#ff007f;")
        self.play_pause_button.setObjectName("play_pause_button")
        self.verticalLayout_4.addWidget(self.play_pause_button)
        self.object_detection_details_button = QtWidgets.QPushButton(self.groupBox_4)
        self.object_detection_details_button.setMinimumSize(QtCore.QSize(0, 35))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.object_detection_details_button.setFont(font)
        self.object_detection_details_button.setStyleSheet("color:#ff007f;")
        self.object_detection_details_button.setObjectName("object_detection_details_button")
        self.verticalLayout_4.addWidget(self.object_detection_details_button)
        self.s4_details_button = QtWidgets.QPushButton(self.groupBox_4)
        self.s4_details_button.setMinimumSize(QtCore.QSize(0, 35))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(False)
        font.setWeight(50)
        self.s4_details_button.setFont(font)
        self.s4_details_button.setStyleSheet("color:#ff007f;")
        self.s4_details_button.setObjectName("s4_details_button")
        self.verticalLayout_4.addWidget(self.s4_details_button)
        self.gridLayout.addWidget(self.groupBox_4, 1, 1, 1, 1)

        self.s4_details_button.setEnabled(False)
        self.object_detection_details_button.setEnabled(False)
        MainWindow.setCentralWidget(self.centralwidget)
        self.setup_Video_Payer(fname)

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

    def setup_Video_Payer(self,fname):

        format=fname[-4:]
        self.original_player=QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.original_player.setVideoOutput(self.video_widget1)
        self.original_player.setMedia(QMediaContent( QUrl.fromLocalFile(os.getcwd()+"\\Output\\Original_video"+format)))
        
        self.gray_player=QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.gray_player.setVideoOutput(self.video_widget2)
        self.gray_player.setMedia(QMediaContent( QUrl.fromLocalFile(os.getcwd()+"\\Output\\Gray_video"+format)))
           
        self.object_detection_player=QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.object_detection_player.setVideoOutput(self.video_widget3)
        self.object_detection_player.setMedia(QMediaContent( QUrl.fromLocalFile(os.getcwd()+"\\Output\\Box_video"+format)))
        
        self.original_player.stateChanged.connect(self.action_state_changed)
        self.play_pause_button.clicked.connect(self.action_play_pause)
        self.s4_details_button.clicked.connect(self.action_s4_details)
        self.object_detection_details_button.clicked.connect(self.action_object_detection_details)
        self.action_play_pause() 

    def action_play_pause(self):
        if(self.original_player.state()==QMediaPlayer.PausedState):
            self.original_player.play()
            self.gray_player.play()
            self.object_detection_player.play()
            self.play_pause_button.setText("Pause Videos")
        elif(self.original_player.state()==QMediaPlayer.PlayingState):
            self.original_player.pause()
            self.gray_player.pause()
            self.object_detection_player.pause()
            self.play_pause_button.setText("Play Videos")
        else:
            self.original_player.play()
            self.gray_player.play()
            self.object_detection_player.play()
            self.play_pause_button.setText("Pause Videos")
        
    def action_s4_details(self):
        self.third_s4_ui = third_s4_details_win.Ui_MainWindow()
        self.MainWindow_s4_ui=QtWidgets.QMainWindow()
        self.third_s4_ui.setupUi(self.MainWindow_s4_ui,self.Results)
        self.MainWindow_s4_ui.show()

    def action_object_detection_details(self):
        object_detection_details.generate_images(self.Results)
        self.fourth_object_detection_ui=fourth_object_detection_win.Ui_MainWindow()
        self.MainWindow_object_detection_ui= QtWidgets.QMainWindow()
        self.fourth_object_detection_ui.setupUi(self.MainWindow_object_detection_ui)
        self.MainWindow_object_detection_ui.show()

    def action_state_changed(self):
        if(self.original_player.state() == 0): #QMediaPlayer.EndOfMedia):
            self.result_display.setText(self.Results['result']+" Video")
            self.play_pause_button.setText("Re-Play Videos")
            if(self.Results['result'] == "Forged"):
                self.s4_details_button.setEnabled(True)
                self.object_detection_details_button.setEnabled(True)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Stages of Video Processing"))
        self.lineEdit2.setText(_translate("MainWindow", "Gray Video"))
        self.lineEdit3.setText(_translate("MainWindow", "Object Detection Video"))
        self.lineEdit1.setText(_translate("MainWindow", "Original Video"))
        self.result_display.setText(_translate("MainWindow", "Awaiting Result"))
        self.s4_details_button.setText(_translate("MainWindow", "Results with S4 Filters Details"))
        self.play_pause_button.setText(_translate("MainWindow", "Pause Videos"))
        self.object_detection_details_button.setText(_translate("MainWindow", "Object Detection Details"))