import os

import FreeCADGui as Gui
import FreeCAD as App

import ahb_locator

global ahb_icon
ahb_icon = os.path.join(os.path.dirname(ahb_locator.__file__), "resources/assembly-handbook.svg")

class AssemblyHandbookWorkbench(Gui.Workbench):
    """
    class which gets initiated at startup of the gui
    """

    MenuText = "Assembly handbook"
    ToolTip = "A workbench for automating creation of an assembly handbook"
    Icon = ahb_icon

    dev = True # indicates development mode (enables additional tools, log, debug checks, etc.)

    context = None

    def GetClassName(self):
        return "Gui::PythonWorkbench"

    def Initialize(self):
        """
        This function is called at the first activation of the workbench.
        """
        self.registerCommands()

    def Activated(self):
        """
        code which should be computed when a user switch to this workbench
        """
        if self.context is None:
            self.initializeContext()

    def Deactivated(self):
        """
        code which should be computed when this workbench is deactivated
        """
        pass

    def initializeContext(self):
        import ahb_context
        self.context = ahb_context.AHB_Context()

    def registerCommands(self):
        toolbox = []

        self.importModule('ahb_cmd_export_csv')
        toolbox.append("AHB_exportCsv")

        self.importModule('ahb_cmd_set_active_stage')
        toolbox.append("AHB_setActiveStage")

        self.importModule('ahb_cmd_set_part_stage')
        toolbox.append("AHB_setPartStage")

        self.importModule('ahb_cmd_switch_visibility_mode')
        toolbox.append("AHB_switchVisibilityMode")

        self.importModule('ahb_cmd_render')
        toolbox.append("AHB_render")

        self.importModule('ahb_cmd_animate')
        toolbox.append("AHB_animate")

        if self.dev:
            self.importModule('ahb_cmd_parse_step')
            toolbox.append("AHB_parse_step")

            self.importModule('ahb_cmd_reload')
            toolbox.append("AHB_reload")

        self.appendToolbar("Assembly Handbook", toolbox)
        self.appendMenu("Assembly Handbook", toolbox)

    def reload(self):
        # Used for development only
        self.removeToolbar("Assembly Handbook")
        self.removeMenu("Assembly Handbook")

        self.context.setAnimationActive(False)

        import importlib
        import ahb_context
        importlib.reload(ahb_context)

        self.initializeContext()
        self.registerCommands()
        pass

    def importModule(self, moduleName):
        import importlib
        module = importlib.import_module(moduleName)
        if self.dev:
            importlib.reload(module)

Gui.addWorkbench(AssemblyHandbookWorkbench())