"""
Module with the menubar
"""

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

from LayoutElements.gcodeEditor import gcodeEditor
from LayoutElements.plotErstellung import plotErstellung
from LayoutElements.setupTabmenu import setupTabmenu  #Tabmenü, in dem die Parameter eingegeben werden können

from stl import mesh
import stl.base
from mpl_toolkits import mplot3d

from utils.globalSettings import (
    # Import GUI Settings to access global settings
    GUI_SETTINGS,
    # with Language Settings language pack can be accessed
    LANGUAGE_SETTINGS
)
from LayoutElements.settingsManager import settingsManager


class menubar(QMenuBar):
    """add a customized menubar"""

    def __init__(self, mainWindow):
        """Set the structure and options for the menubar"""
        super().__init__()

        settings = mainWindow.thisSettingsManager

        #obere Menübar, die die Popup-Menüs enthält
        self: QMenuBar = mainWindow.menuBar()
        self.setObjectName("MenuBar")
        # clear to prevent mutliple menubars as it is reinit after user select
        self.clear()
        self.show()

        fileMenu = self.addMenu("&"+LANGUAGE_SETTINGS.languagePack["menubar"]["openFile"])

        settingsfileOpenAction = QAction(LANGUAGE_SETTINGS.languagePack["menubarSubmenu"]["fileSubmenu"]["settingsFileActions"]["openSettingsFile"], mainWindow, )
        settingsfileOpenAction.setShortcut('Ctrl+Alt+O')
        settingsfileOpenAction.triggered.connect(mainWindow.thisSettings.openProjectFile)
        settingsfileSaveAction = QAction(LANGUAGE_SETTINGS.languagePack["menubarSubmenu"]["fileSubmenu"]["settingsFileActions"]["saveSettingsFile"], mainWindow, )
        settingsfileSaveAction.triggered.connect(mainWindow.thisSettings.writeProjectFile)
        settingsfileSaveAction.setShortcut('Ctrl+Alt+S')


        """
        Hier werden die Aktionen definiert, die in den Popup-Menüs (G-Code, Settings) definiert werden
        """
        gcodeOpenAction = QAction(LANGUAGE_SETTINGS.languagePack["menubarSubmenu"]["fileSubmenu"]["gcodeActions"]["openGcode"], mainWindow, )
        gcodeOpenAction.triggered.connect(mainWindow.thisGcodeEditor.gcodeOpen)    #Aktion wird der Funktion gcode_open zugewiesen
        gcodeOpenAction.setShortcut('Ctrl+O')

        gcodeSaveAction = QAction(LANGUAGE_SETTINGS.languagePack["menubarSubmenu"]["fileSubmenu"]["gcodeActions"]["saveGcode"], mainWindow, )
        gcodeSaveAction.triggered.connect(mainWindow.thisGcodeEditor.gcodeSave)
        gcodeSaveAction.setShortcut('Ctrl+S')

        gcodeSubmenu = fileMenu.addMenu(LANGUAGE_SETTINGS.languagePack["menubarSubmenu"]["fileSubmenu"]["optionGcode"])
        gcodeSubmenu.addAction(gcodeOpenAction)
        gcodeSubmenu.addAction(gcodeSaveAction)

        fileMenu.addSeparator()

        gcodeSubmenu = fileMenu.addMenu(LANGUAGE_SETTINGS.languagePack["menubarSubmenu"]["fileSubmenu"]["optionSettingsFile"])
        gcodeSubmenu.addAction(settingsfileOpenAction)
        gcodeSubmenu.addAction(settingsfileSaveAction)

        settingsAction = QAction("&"+LANGUAGE_SETTINGS.languagePack["menubar"]["openSettings"], mainWindow, )
        settingsAction.triggered.connect(lambda: settings.showSettings())
        self.addAction(settingsAction)

        helpMenu = self.addMenu("&"+LANGUAGE_SETTINGS.languagePack["menubar"]["openHelp"])
        openTutorialPdf = helpMenu.addAction(LANGUAGE_SETTINGS.languagePack["menubarSubmenu"]["helpSubmenu"]["optionTutorialPDF"])
        openTutorialPdf.setShortcut('Ctrl+H')
        url = QUrl.fromLocalFile('Tutorial PDF/Tutorial.pdf')
        openTutorialPdf.triggered.connect(lambda: QDesktopServices.openUrl(url) )



        


