# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'task6.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from task4 import findOrder, findReordering
import task5
class MyCustomWidget(QWidget):
    
    def __init__(self, name, parent=None):
        super(MyCustomWidget, self).__init__(parent)
        self.row = QHBoxLayout()
        self.name = name
        self.row.addWidget(QLabel(name))

        self.yesButton = QPushButton("Yes")
        self.yesButton.clicked.connect(self.yesClicked)
        self.yesButton.setStyleSheet("background-color : grey")
        self.row.addWidget(self.yesButton)

        self.noButton = QPushButton("No")
        self.noButton.clicked.connect(self.noClicked)
        self.noButton.setStyleSheet("background-color : grey")
        self.row.addWidget(self.noButton)

        self.setLayout(self.row)
        self.response = 0 # No Response
    
    def yesClicked(self):
        self.response = 1 # Yes
        self.yesButton.setStyleSheet("background-color : green")
        self.noButton.setStyleSheet("background-color : grey")
    
    def noClicked(self):
        self.response = -1 # No
        self.yesButton.setStyleSheet("background-color : grey")
        self.noButton.setStyleSheet("background-color : green")

    def getResponse(self):
        return self.response

class Ui_MainWindow(object):

    feedbackMechanims = ["Probabilistic relevance feedback", "Classifier-based relevance feedback"]
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1000, 1000)

        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        enterQueryLabel = QLabel(self.centralwidget)
        enterQueryLabel.setText("Enter Query Gesture: ")
        enterQueryLabel.setGeometry(QtCore.QRect(100, 60, 220, 20)) 

        self.queryParameterText = QTextEdit(self.centralwidget)
        self.queryParameterText.setGeometry(QtCore.QRect(380, 60, 250, 20))

        selectFeedbackMechanismLabel = QLabel(self.centralwidget)
        selectFeedbackMechanismLabel.setText("Choose Feedback Mechanism: ")
        selectFeedbackMechanismLabel.setGeometry(QtCore.QRect(100, 100, 220, 20)) 

        self.feedbackMechanismComboBox = QComboBox(self.centralwidget)
        self.feedbackMechanismComboBox.setGeometry(QtCore.QRect(380, 100, 250, 20))
        self.feedbackMechanismComboBox.setObjectName("feedbackMechanismComboBox")
        self.feedbackMechanismComboBox.addItems(self.feedbackMechanims)

        layersLabel = QLabel(self.centralwidget)
        layersLabel.setText("Enter the number of layers: ")
        layersLabel.setGeometry(QtCore.QRect(100, 140, 220, 20)) 

        self.layersText = QTextEdit(self.centralwidget)
        self.layersText.setGeometry(QtCore.QRect(380, 140, 250, 20))

        hashLabel = QLabel(self.centralwidget)
        hashLabel.setText("Enter number of hashes per layer: ")
        hashLabel.setGeometry(QtCore.QRect(100, 180, 220, 20)) 

        self.hashText = QTextEdit(self.centralwidget)
        self.hashText.setGeometry(QtCore.QRect(380, 180, 250, 20))

        tLabel = QLabel(self.centralwidget)
        tLabel.setText("Enter the no. of relevant gestures for output: ")
        tLabel.setGeometry(QtCore.QRect(100, 220, 270, 20)) 

        self.tText = QTextEdit(self.centralwidget)
        self.tText.setGeometry(QtCore.QRect(380, 220, 250, 20))

        getReleventGestureButton = QPushButton(self.centralwidget)
        getReleventGestureButton.setText("Get Relevant Gestures")
        getReleventGestureButton.setGeometry(QtCore.QRect(310, 260, 161, 28))
        getReleventGestureButton.clicked.connect(self.getReleventGestureButtonClicked)

        self.listWidget = QListWidget(self.centralwidget)
        self.listWidget.setGeometry(QtCore.QRect(200, 320, 400, 380))
        self.listWidget.setObjectName("listWidget")

        feedbackButton = QPushButton(self.centralwidget)
        feedbackButton.setText("Give Feedbacks")
        feedbackButton.setGeometry(QtCore.QRect(310, 720, 161, 28))
        feedbackButton.clicked.connect(self.feedbackButtonClicked)


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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

    def getReleventGestureButtonClicked(self):
        currentFeedbackMechanism = self.feedbackMechanismComboBox.currentText()
        queryParameter = self.queryParameterText.toPlainText()
        noOfLayers = int(self.layersText.toPlainText())
        noOfHashesPerLayer = int(self.hashText.toPlainText())
        noOfOutputGestures = int(self.tText.toPlainText())
        if currentFeedbackMechanism == self.feedbackMechanims[0]:
            predictions = findOrder(noOfLayers, noOfHashesPerLayer, queryParameter, noOfOutputGestures)
        elif currentFeedbackMechanism == self.feedbackMechanims[1]:
            predictions = task5.initialComputation(noOfLayers, noOfHashesPerLayer, queryParameter, noOfOutputGestures)
        self.showPredictions(predictions)
    
    def feedbackButtonClicked(self):
        currentFeedbackMechanism = self.feedbackMechanismComboBox.currentText()
        queryParameter = self.queryParameterText.toPlainText()
        noOfLayers = int(self.layersText.toPlainText())
        noOfHashesPerLayer = int(self.hashText.toPlainText())
        noOfOutputGestures = int(self.tText.toPlainText())
        feedback = {}
        for row in self.rows:
            key = row.name
            response = row.getResponse()
            feedback[key] = response
        
        if currentFeedbackMechanism == self.feedbackMechanims[0]:
            predictions = findReordering(queryParameter, noOfOutputGestures, feedback)
        elif currentFeedbackMechanism == self.feedbackMechanims[1]:
            predictions = task5.feedback(noOfLayers, noOfHashesPerLayer, queryParameter, noOfOutputGestures, feedback)
        
        self.showPredictions(predictions)

    def showPredictions(self, predictions):
        self.rows = []
        self.listWidget.clear()
        for i in predictions:
            item = QListWidgetItem(self.listWidget)
            self.listWidget.addItem(item)
            row = MyCustomWidget(i)
            self.rows.append(row)
            item.setSizeHint(row.minimumSizeHint())
            self.listWidget.setItemWidget(item, row)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
