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

# Form implementation generated from reading ui file 'equationList.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
from EquationConstants import DB_HOST, DB_NAME, DB_PASS, DB_USER
from EquationConstants import DB_TABLE_NAMES_Q
import psycopg2
import psycopg2.extras
import subprocess
# import imageio
# import cv2
#
# img = cv2.imread('eq_template.png')
# cv2.imshow('image', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# img = Image.open(fh, mode='r')
# roi_img = img.crop(box)

# p2 = subprocess.run(['convert', '-density 300', '-depth 8', '-quality 85', filename+'.pdf', filename+'.png'],
#   shell=True, capture_output=True, text=True, check=True)
# p1 = subprocess.run(['xelatex.exe', filename+'.tex','-interaction=batchmode'],
#   shell=True, capture_output=True, text=True, check=True)
# template_query = """INSERT INTO latex (data, image, thumbnail, insert_order, created_by)
#   VALUES ({data},{image},{thumbnail}, {insert_order}, {created_by})"""


class Ui_MainWindow(object):
    def loadData(self):
        connection = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASS, host=DB_HOST)

        cursor = connection.cursor()

        myTable = 'math_object'

        template_query = """SELECT (id, name) FROM {aTable}""".format(aTable=myTable)
        cursor.execute(template_query)

        myData = cursor.fetchall()
        self.tableWidget.setRowCount(0)

        for row_number, row_data in enumerate(myData):
            self.tableWidget.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))

        cursor.close()
        connection.close()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1467, 1264)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setRowCount(5)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(2)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        self.verticalLayout.addWidget(self.tableWidget)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.btn_loadData = QtWidgets.QPushButton(self.centralwidget)
        self.btn_loadData.setObjectName("btn_loadData")
        self.btn_loadData.clicked.connect(self.loadData)

        self.horizontalLayout.addWidget(self.btn_loadData)
        self.verticalLayout.addLayout(self.horizontalLayout)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "ID"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "New Column"))
        self.btn_loadData.setText(_translate("MainWindow", "Load Data"))


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