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

# Form implementation generated from reading ui file 'raw_ui/dialog_createProject.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
import numpy as np, pandas as pd, datetime, time, json, os
from classes.dialog_createField import Ui_Dialog_createField


class Ui_Dialog_createProject(object):
    def setupUi(self, Dialog_createProject):
        Dialog_createProject.setObjectName("Dialog_createProject")
        Dialog_createProject.resize(368, 227)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog_createProject)
        self.buttonBox.setGeometry(QtCore.QRect(190, 180, 161, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.label = QtWidgets.QLabel(Dialog_createProject)
        self.label.setGeometry(QtCore.QRect(20, 20, 47, 13))
        self.label.setObjectName("label")
        self.textEdit_name = QtWidgets.QTextEdit(Dialog_createProject)
        self.textEdit_name.setGeometry(QtCore.QRect(70, 17, 281, 21))
        self.textEdit_name.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.textEdit_name.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.textEdit_name.setObjectName("textEdit_name")
        self.textEdit_name.setTabChangesFocus(True)
        self.textEdit_goal = QtWidgets.QTextEdit(Dialog_createProject)
        self.textEdit_goal.setGeometry(QtCore.QRect(70, 47, 281, 61))
        self.textEdit_goal.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.textEdit_goal.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.textEdit_goal.setObjectName("textEdit_goal")
        self.textEdit_goal.setTabChangesFocus(True)
        self.label_2 = QtWidgets.QLabel(Dialog_createProject)
        self.label_2.setGeometry(QtCore.QRect(20, 123, 47, 13))
        self.label_2.setObjectName("label_2")
        self.comboBox_field = QtWidgets.QComboBox(Dialog_createProject)
        self.comboBox_field.setGeometry(QtCore.QRect(70, 120, 191, 22))
        self.comboBox_field.setObjectName("comboBox_field")
        self.pushButton_newField = QtWidgets.QPushButton(Dialog_createProject)
        self.pushButton_newField.setGeometry(QtCore.QRect(280, 120, 75, 25))
        self.pushButton_newField.setObjectName("pushButton_newField")
        self.label_3 = QtWidgets.QLabel(Dialog_createProject)
        self.label_3.setGeometry(QtCore.QRect(20, 50, 47, 13))
        self.label_3.setObjectName("label_3")

        self.retranslateUi(Dialog_createProject)
        self.buttonBox.accepted.connect(Dialog_createProject.accept)
        self.buttonBox.accepted.connect(self.submit)
        self.buttonBox.rejected.connect(Dialog_createProject.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog_createProject)

        self.new_project_name = ''
        self.new_project_goal = ''
        self.new_project_field = ''

        self.new_field = None
        self.init_combo_field()

        self.pushButton_newField.clicked.connect(self.create_new_field)

    def retranslateUi(self, Dialog_createProject):
        self._translate = QtCore.QCoreApplication.translate
        Dialog_createProject.setWindowTitle(self._translate("Dialog_createProject", "Create New Project"))
        self.label.setText(self._translate("Dialog_createProject", "Name:"))
        self.label_2.setText(self._translate("Dialog_createProject", "Field:"))
        self.pushButton_newField.setText(self._translate("Dialog_createProject", "New Field"))
        self.label_3.setText(self._translate("Dialog_createProject", "Goal:"))

    def submit(self):
        self.new_project_name = self.textEdit_name.toPlainText()
        self.new_project_goal = self.textEdit_goal.toPlainText()
        self.new_project_field = self.comboBox_field.currentText()

    def get_list_field(self):
        # get all field
        list_field = []
        for filename in os.listdir('data'):
            with open('data/'+filename,'r') as f:
                dic = json.load(f)
                list_field = dic['field'].lower()
                f.close()
        list_field = list(np.unique(np.array(list_field)))
        return list_field

    def init_combo_field(self):
        self.comboBox_field.clear()
        list_field = self.get_list_field()
        for field in list_field:
            self.comboBox_field.addItem("")
            self.comboBox_field.setItemText(self.comboBox_field.count()-1, self._translate("MainWindow", str(field)))


    def create_new_field(self):
        print('openning dialog create project')
        dialog = QtWidgets.QDialog()
        ui = Ui_Dialog_createField() # place dialog object here
        ui.setupUi(dialog)
        dialog.show()
        rsp = dialog.exec_()
        if rsp == QtWidgets.QDialog.Accepted:
            self.new_field = ui.textEdit.toPlainText()
            self.comboBox_field.addItem("")
            self.comboBox_field.setItemText(self.comboBox_field.count()-1, self._translate("MainWindow", str(self.new_field)))
            self.comboBox_field.setCurrentText(self.new_field)
        else:
            print('dialog for new project have been closed') 


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog_createProject = QtWidgets.QDialog()
    ui = Ui_Dialog_createProject()
    ui.setupUi(Dialog_createProject)
    Dialog_createProject.show()
    sys.exit(app.exec_())
