#!/usr/bin/env python

"""
Copyright (C) 2026 xanderhopp; xanderhopp@gmail.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""



import os
import sys

if getattr(sys, 'frozen', False):
    base_path = os.path.dirname(sys.executable)
else:
    base_path = os.path.dirname(os.path.abspath(__file__))

os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.path.join(base_path, "platforms")
os.environ["QT_PLUGIN_PATH"] = base_path




import sys
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QPushButton,
    QFileDialog,
    QDialog
    )


from MainWindow import Ui_MainWindow
from HelpWindow import Ui_HelpWindow
from aboutWindow import Ui_aboutWindow

import main_funktions


class HelpWindow(QDialog):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.ui = Ui_HelpWindow()
        self.ui.setupUi(self)


class aboutWindow(QDialog):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.ui = Ui_aboutWindow()
        self.ui.setupUi(self)


class MainWindow(QMainWindow, QFileDialog, QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.originFile = None
        self.setIcon()
        
        # Slots
        self.ui.fileChooseButton.clicked.connect(self.chooseFile)
        self.ui.createButton.clicked.connect(self.createFile)
        
        # Events
        self.ui.action_anleitung.triggered.connect(self.openHelpWindow)
        self.ui.action_ueber.triggered.connect(self.openaboutWindow)
    
    def setIcon(self):
        appIcon = QIcon('icon.svg')
        self.setWindowIcon(appIcon)
        
    def give_feedback(self, feedback):
         print(feedback)
         self.ui.feedbackTextEdit.append(feedback)
        
        # choose a File
    def chooseFile(self):
            fileName, _ = QFileDialog.getOpenFileName(self, "qet-Datei öffnen", "","QET Files (*.qet);;All Files (*)")
            if fileName:
                print(fileName)
                originFile = fileName
                self.ui.choosenFileLineEdit.setText(fileName)
            
        # start the Main funktions and get the feedback to the textEdit
    def createFile(self):
        main_funktions.work_out_the_terminal_diagram(self.ui.choosenFileLineEdit.text(), self.give_feedback)

    def openHelpWindow(self):
        self.hw = HelpWindow()
        self.hw.show()

    def openaboutWindow(self):
        self.aw = aboutWindow()
        self.aw.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)

    Window = MainWindow()
    Window.show()
    sys.exit(app.exec())