# Form implementation generated from reading ui file 'c:\Users\aky547\GitHub\Semesterapparate\ui\dialogs\settings.ui'
#
# Created by: PyQt6 UI code generator 6.3.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from omegaconf import OmegaConf
from PyQt6 import QtCore, QtWidgets

config = OmegaConf.load("config.yaml")


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(743, 576)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(120, 540, 621, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Orientation.Horizontal)
        self.buttonBox.setStandardButtons(
            QtWidgets.QDialogButtonBox.StandardButton.Cancel
            | QtWidgets.QDialogButtonBox.StandardButton.Ok
        )
        self.buttonBox.setObjectName("buttonBox")
        self.frame = QtWidgets.QFrame(Dialog)
        self.frame.setGeometry(QtCore.QRect(0, 0, 741, 541))
        self.frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.frame.setObjectName("frame")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(400, 30, 161, 21))
        self.label.setObjectName("label")
        self.line = QtWidgets.QFrame(self.frame)
        self.line.setGeometry(QtCore.QRect(370, 0, 20, 541))
        self.line.setFrameShadow(QtWidgets.QFrame.Shadow.Plain)
        self.line.setLineWidth(1)
        self.line.setMidLineWidth(0)
        self.line.setFrameShape(QtWidgets.QFrame.Shape.VLine)
        self.line.setObjectName("line")
        self.textBrowser = QtWidgets.QTextBrowser(self.frame)
        self.textBrowser.setGeometry(QtCore.QRect(400, 50, 311, 51))
        self.textBrowser.setFocusPolicy(QtCore.Qt.FocusPolicy.NoFocus)
        self.textBrowser.setInputMethodHints(QtCore.Qt.InputMethodHint.ImhNone)
        self.textBrowser.setObjectName("textBrowser")
        self.label_2 = QtWidgets.QLabel(self.frame)
        self.label_2.setGeometry(QtCore.QRect(10, 20, 161, 21))
        self.label_2.setObjectName("label_2")
        self.os_apps = QtWidgets.QCheckBox(self.frame)
        self.os_apps.setGeometry(QtCore.QRect(410, 110, 161, 17))
        self.os_apps.setObjectName("os_apps")
        self.formLayoutWidget = QtWidgets.QWidget(self.frame)
        self.formLayoutWidget.setGeometry(QtCore.QRect(10, 50, 361, 491))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.formLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.label_3 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 0, 0, 1, 1)
        self.label_5 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_5.setObjectName("label_5")
        self.gridLayout.addWidget(self.label_5, 2, 0, 1, 1)
        self.db_path = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.db_path.setEnabled(False)
        self.db_path.setObjectName("db_path")
        self.gridLayout.addWidget(self.db_path, 1, 1, 1, 1)
        self.save_path = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.save_path.setObjectName("save_path")
        self.gridLayout.addWidget(self.save_path, 2, 1, 1, 1)
        self.label_4 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 1, 0, 1, 1)
        self.db_name = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.db_name.setObjectName("db_name")
        self.gridLayout.addWidget(self.db_name, 0, 1, 1, 1)
        self.tb_set_save_path = QtWidgets.QToolButton(self.formLayoutWidget)
        self.tb_set_save_path.setObjectName("tb_set_save_path")
        self.gridLayout.addWidget(self.tb_set_save_path, 2, 2, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(
            20,
            40,
            QtWidgets.QSizePolicy.Policy.Minimum,
            QtWidgets.QSizePolicy.Policy.Expanding,
        )
        self.gridLayout.addItem(spacerItem, 3, 1, 1, 1)
        self.tb_select_db = QtWidgets.QToolButton(self.formLayoutWidget)
        self.tb_select_db.setObjectName("tb_select_db")
        self.gridLayout.addWidget(self.tb_select_db, 0, 2, 1, 1)
        self.scrollArea = QtWidgets.QScrollArea(self.frame)
        self.scrollArea.setGeometry(QtCore.QRect(400, 130, 331, 411))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 329, 409))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.treeWidget = QtWidgets.QTreeWidget(self.scrollAreaWidgetContents)
        self.treeWidget.setGeometry(QtCore.QRect(0, 0, 331, 411))
        self.treeWidget.setObjectName("treeWidget")
        self.treeWidget.setContextMenuPolicy(
            QtCore.Qt.ContextMenuPolicy.ActionsContextMenu
        )
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.label_3.setBuddy(self.db_name)
        self.label_5.setBuddy(self.save_path)
        self.label_4.setBuddy(self.db_path)
        self.tb_select_db.clicked.connect(self.select_db)
        self.tb_set_save_path.clicked.connect(self.set_save_path)
        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)  # type: ignore
        self.buttonBox.rejected.connect(Dialog.reject)  # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        Dialog.setTabOrder(self.db_name, self.db_path)
        Dialog.setTabOrder(self.db_path, self.save_path)
        Dialog.setTabOrder(self.save_path, self.os_apps)
        Dialog.setTabOrder(self.os_apps, self.textBrowser)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Dateispezifische Einstellungen"))
        self.textBrowser.setHtml(
            _translate(
                "Dialog",
                '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">\n'
                '<html><head><meta name="qrichtext" content="1" /><style type="text/css">\n'
                "p, li { white-space: pre-wrap; }\n"
                "</style></head><body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
                '<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Hier können Einstellungen f�r bestehende Dateiformate geändert, oder neue Dateiformate eingefügt werden</p>\n'
                '<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>',
            )
        )
        self.label_2.setText(_translate("Dialog", "Allgemeine Einstellungen"))
        self.os_apps.setToolTip(
            _translate(
                "Dialog",
                "Verwendet im Betriebssystem festgelegte Anwendungen um Dateien zu öffnen",
            )
        )
        self.os_apps.setText(_translate("Dialog", "Standard-Apps verwenden"))
        self.label_3.setToolTip(
            _translate(
                "Dialog",
                '<html><head/><body><p>Name der Datenbank, welche verwendet werden soll. <span style=" font-weight:600;">Muss</span> auf .db enden</p></body></html>',
            )
        )
        self.label_3.setText(_translate("Dialog", "Datenbankname"))
        self.label_5.setToolTip(
            _translate(
                "Dialog",
                "Pfad, an dem heruntergeladene Dateien gespeichert werden sollen",
            )
        )
        self.label_5.setText(_translate("Dialog", "Speicherpfad"))
        self.label_4.setText(_translate("Dialog", "Datenbankpfad"))
        self.db_name.setText(_translate("Dialog", "sap.db"))
        self.tb_set_save_path.setText(_translate("Dialog", "..."))
        self.tb_select_db.setText(_translate("Dialog", "..."))
        self.load_config()

    def load_config(self):
        self.db_name.setText(config.database.name)
        self.db_path.setText(config.database.path)
        self.save_path.setText(config.save_path)
        self.os_apps.setChecked(config.default_apps)
        applications = config.custom_applications
        for application in applications:
            name = application.application
            file_type = application.extensions
            display_name = application.name
            print(name, file_type, display_name)  #
            # create new item
            item = QtWidgets.QTreeWidgetItem(self.treeWidget)
            item.setText(0, display_name)

    def select_db(self):
        # open file dialog, limit to .db files
        file_dialog = QtWidgets.QFileDialog()
        file_dialog.setFileMode(QtWidgets.QFileDialog.FileMode.AnyFile)
        file_dialog.setNameFilter("Datenbank (*.db)")
        file_dialog.setViewMode(QtWidgets.QFileDialog.ViewMode.Detail)
        if file_dialog.exec():
            self.db_name.setText(file_dialog.selectedFiles()[0].split("/")[-1])
            self.db_path.setText(
                file_dialog.selectedFiles()[0].split(self.db_name.text())[0]
            )

    def set_save_path(self):
        # open file dialog, limit to .db files
        file_dialog = QtWidgets.QFileDialog()
        file_dialog.setFileMode(QtWidgets.QFileDialog.FileMode.Directory)
        file_dialog.setViewMode(QtWidgets.QFileDialog.ViewMode.Detail)
        if file_dialog.exec():
            self.save_path.setText(file_dialog.selectedFiles()[0])

    def return_data(self):
        config.database.name = self.db_name.text()
        config.database.path = self.db_path.text()
        config.save_path = self.save_path.text()
        config.default_apps = self.os_apps.isChecked()
        return config
