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

# Form implementation generated from reading ui file '.\TemperatureConverter.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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


class Ui_MainWindow(object):
    def __init__(self):
        self.convertFromState = None
        self.convertToState = None
        self.kelvinTemperature = 0

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 325)
        MainWindow.setWindowTitle("Temperature Converter")

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

        self.label_EnterTemperature = QtWidgets.QLabel(self.centralwidget)
        self.label_EnterTemperature.setGeometry(QtCore.QRect(25, 25, 200, 50))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.label_EnterTemperature.setFont(font)
        self.label_EnterTemperature.setText("Enter Temperature")
        self.label_EnterTemperature.setObjectName("label_EnterTemperature")

        self.lineEdit_TemperatureInput = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_TemperatureInput.setGeometry(QtCore.QRect(250, 25, 125, 50))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.lineEdit_TemperatureInput.setFont(font)
        self.lineEdit_TemperatureInput.setText("0")
        self.lineEdit_TemperatureInput.setObjectName("lineEdit_TemperatureInput")

        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(200, 110, 175, 30))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.comboBox.setFont(font)
        self.comboBox.setCurrentText("Celcius")
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.setItemText(0, "Celcius")
        self.comboBox.addItem("")
        self.comboBox.setItemText(1, "Fahrenheit")
        self.comboBox.addItem("")
        self.comboBox.setItemText(2, "Kelvin")

        self.comboBox.currentIndexChanged.connect(self.selectionChangeConvertFrom)

        self.label_ConvertFrom = QtWidgets.QLabel(self.centralwidget)
        self.label_ConvertFrom.setGeometry(QtCore.QRect(25, 100, 150, 50))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.label_ConvertFrom.setFont(font)
        self.label_ConvertFrom.setText("Convert From:")
        self.label_ConvertFrom.setObjectName("label_ConvertFrom")

        self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox_2.setGeometry(QtCore.QRect(200, 185, 175, 30))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.comboBox_2.setFont(font)
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("")
        self.comboBox_2.setItemText(0, "Fahrenheit")
        self.comboBox_2.addItem("")
        self.comboBox_2.setItemText(1, "Kelvin")

        self.comboBox_2.currentIndexChanged.connect(self.selectionChangeConvertTo)

        self.label_ConvertTo = QtWidgets.QLabel(self.centralwidget)
        self.label_ConvertTo.setGeometry(QtCore.QRect(25, 175, 150, 50))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.label_ConvertTo.setFont(font)
        self.label_ConvertTo.setText("Convert To:")
        self.label_ConvertTo.setObjectName("label_ConvertTo")

        self.pushButton_Convert = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_Convert.setGeometry(QtCore.QRect(25, 250, 150, 50))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.pushButton_Convert.setFont(font)
        self.pushButton_Convert.setText("Convert")
        self.pushButton_Convert.setObjectName("pushButton_Convert")

        self.pushButton_Convert.clicked.connect(self.convertTemperatureUpdateLabel)

        self.label_TemperatureOutput = QtWidgets.QLabel(self.centralwidget)
        self.label_TemperatureOutput.setGeometry(QtCore.QRect(200, 250, 150, 50))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.label_TemperatureOutput.setFont(font)
        self.label_TemperatureOutput.setText("0 °C")
        self.label_TemperatureOutput.setObjectName("label_TemperatureOutput")

        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        pass

    def selectionChangeConvertFrom(self):
        self.convertFromState = self.comboBox.currentText()
        print(self.convertFromState)
        if self.convertFromState == 'Celcius':
            self.comboBox_2.setItemText(0, "Fahrenheit")
            self.comboBox_2.setItemText(1, "Kelvin")
        elif self.convertFromState == 'Kelvin':
            self.comboBox_2.setItemText(0, "Celcius")
            self.comboBox_2.setItemText(1, "Fahrenheit")
        elif self.convertFromState == 'Fahrenheit':
            self.comboBox_2.setItemText(0, "Celcius")
            self.comboBox_2.setItemText(1, "Kelvin")
        else:
            pass

    def selectionChangeConvertTo(self):
        self.convertToState = self.comboBox_2.currentText()
        print(self.convertToState)

    def convertTemperatureUpdateLabel(self):
        self.convertFromState = self.comboBox.currentText()
        self.convertToState = self.comboBox_2.currentText()

        # inputTemperature = self.lineEdit_TemperatureInput.text()
        # print(inputTemperature)
        try:
            inputTemperature = float(self.lineEdit_TemperatureInput.text())
        except ValueError:
            inputTemperature = 0.0
            print('Enter a Number')

        
        if self.convertFromState == 'Celcius':
            self.kelvinTemperature = inputTemperature + 273.15
        elif self.convertFromState == 'Kelvin':
             self.kelvinTemperature = inputTemperature
        elif self.convertFromState == 'Fahrenheit':
            self.kelvinTemperature =  (inputTemperature - 32.0) * (5/9) + 273.15
        else:
            pass

        # # print(inputTemperature, self.kelvinTemperature)

        if self.convertToState == 'Celcius':
            outputTemperature = self.kelvinTemperature - 273.15
            self.label_TemperatureOutput.setText(str(round(outputTemperature,2)) + " °C")
        elif self.convertToState == 'Kelvin':
            outputTemperature = self.kelvinTemperature
            self.label_TemperatureOutput.setText(str(round(outputTemperature,2)) + " °K")
        elif self.convertToState == 'Fahrenheit':
            outputTemperature =  (self.kelvinTemperature - 273.15) * (9/5) + 32
            self.label_TemperatureOutput.setText(str(round(outputTemperature,2)) + " °F")
        else:
            pass

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_())
