# /**********************************************************************
# *                                                                     *
# * Copyright (c) 2021 Javier Braña <javier.branagutierrez@gmail.com>  *
# *                                                                     *
# * This program is free software; you can redistribute it and/or modify*
# * it under the terms of the GNU Lesser General Public License (LGPL)  *
# * as published by the Free Software Foundation; either version 2 of   *
# * the License, or (at your option) any later version.                 *
# * for detail see the LICENCE text file.                               *
# *                                                                     *
# * 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.  See the       *
# * GNU Library General Public License for more details.                *
# *                                                                     *
# * You should have received a copy of the GNU Library General Public   *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307*
# * USA                                                                 *
# *                                                                     *
# ***********************************************************************

__title__ = "FreeCAD Fotovoltaic Power Plant Toolkit"
__author__ = "Javier Braña"
__url__ = "sn"

from pathlib import Path
import sys
import os
import FreeCADGui
import FreeCAD

FreeCADGui.updateLocale()


class PVPlantWorkbench(Workbench):
    from PVPlantResources import DirIcons as DirIcons
    MenuText = "PVPlant"
    ToolTip = "Workbench for PV design"
    Icon = str(os.path.join(DirIcons, "icon.svg"))

    def Initialize(self):

        #sys.path.append(r"C:\Users\javie\AppData\Roaming\FreeCAD\Mod")
        sys.path.append(os.path.join(FreeCAD.getUserAppDataDir(), 'Mod'))
        import PVPlantTools, reload

        self.projectlist = PVPlantTools.projectlist
        self.projectlist.insert(0, 'Reload')
        self.projectlist.insert(1, 'Separator')
        self.framelist = PVPlantTools.pv_mechanical

        from Export import ExporterCommands
        self.inportExportlist = ExporterCommands.Exportlist

        self.objectlist = PVPlantTools.objectlist
        '''    [
            "PVPlantTree",
            "PVPlantBuilding",
            "PVPlantFenceGroup",
        ]'''
        self.electricalList = ["PVPlantStringBox",
                               "PVPlantCable",
                               "PVPlanElectricalLine",
                               "Conduit",
                               "Stringing",
                               "Separator",
                               "StringInverter",
                               ]

        self.roads = ["PVPlantRoad",
                      ]

        self.pads = ["PVPlantPad",
                     "Separator"
                     ]

        # Toolbar
        self.appendToolbar("Civil", self.projectlist)  # creates a new toolbar with your commands
        self.appendToolbar("Mechanical", self.framelist)  # creates a new toolbar with your commands
        self.appendToolbar("Shadow", self.objectlist)  # creates a new toolbar with your commands
        self.appendToolbar("Outputs", self.inportExportlist)  # creates a new toolbar with your commands
        self.appendToolbar("Electrical", self.electricalList)  # creates a new toolbar with your commands

        # Menu
        self.appendMenu("&Civil", self.projectlist)  # creates a new menu
        self.appendMenu("&Mechanical", self.framelist)  # creates a new menu
        self.appendMenu("&Shadow", self.objectlist)  # creates a new menu
        self.appendMenu("&Outputs", self.inportExportlist)  # creates a new menu
        self.appendMenu("&Electrical", self.electricalList)  # creates a new menu

        # Draft tools
        from DraftTools import translate
        self.drafttools = ["Draft_Line", "Draft_Wire", "Draft_Circle", "Draft_Arc", "Draft_Ellipse",
                           "Draft_Polygon", "Draft_Rectangle", "Draft_Text",
                           "Draft_Dimension", "Draft_BSpline", "Draft_Point",
                           "Draft_Facebinder", "Draft_BezCurve", "Draft_Label"]
        self.draftmodtools = ["Draft_Move", "Draft_Rotate", "Draft_Offset",
                              "Draft_Trimex", "Draft_Upgrade", "Draft_Downgrade", "Draft_Scale",
                              "Draft_Shape2DView", "Draft_Draft2Sketch", "Draft_Array",
                              "Draft_Clone"]
        self.draftextratools = ["Draft_WireToBSpline", "Draft_ShapeString",
                                "Draft_PathArray", "Draft_Mirror", "Draft_Stretch"]
        self.draftcontexttools = ["Draft_ApplyStyle", "Draft_ToggleDisplayMode", "Draft_AddToGroup", "Draft_AutoGroup",
                                  "Draft_SelectGroup", "Draft_SelectPlane",
                                  "Draft_ShowSnapBar", "Draft_ToggleGrid", ]
        self.draftutils = ["Draft_Heal", "Draft_FlipDimension",
                           "Draft_ToggleConstructionMode", "Draft_ToggleContinueMode", "Draft_Edit",
                           "Draft_Slope", "Draft_AddConstruction"]
        self.snapList = ['Draft_Snap_Lock', 'Draft_Snap_Midpoint', 'Draft_Snap_Perpendicular',
                         'Draft_Snap_Grid', 'Draft_Snap_Intersection', 'Draft_Snap_Parallel',
                         'Draft_Snap_Endpoint', 'Draft_Snap_Angle', 'Draft_Snap_Center',
                         'Draft_Snap_Extension', 'Draft_Snap_Near', 'Draft_Snap_Ortho', 'Draft_Snap_Special',
                         'Draft_Snap_Dimensions', 'Draft_Snap_WorkingPlane']

        def QT_TRANSLATE_NOOP(scope, text): return text

        self.appendToolbar(QT_TRANSLATE_NOOP("Workbench", "Draft tools"), self.drafttools)
        self.appendToolbar(QT_TRANSLATE_NOOP("Workbench", "Draft mod tools"), self.draftmodtools)
        self.appendMenu(QT_TRANSLATE_NOOP("arch", "&Draft"),
                        self.drafttools + self.draftmodtools + self.draftextratools)
        self.appendMenu([QT_TRANSLATE_NOOP("arch", "&Draft"), QT_TRANSLATE_NOOP("arch", "Utilities")],
                        self.draftutils + self.draftcontexttools)
        self.appendMenu([QT_TRANSLATE_NOOP("arch", "&Draft"), QT_TRANSLATE_NOOP("arch", "Snapping")], self.snapList)

        import Part
        self.measureTools = ["Part_Measure_Linear",
                             "Part_Measure_Angular",
                             "Separator",
                             "Part_Measure_Refresh",
                             "Part_Measure_Clear_All",
                             "Part_Measure_Toggle_All",
                             "Part_Measure_Toggle_3D",
                             "Part_Measure_Toggle_Delta"
                             ]
        self.appendToolbar("Medir", self.measureTools)
        self.appendMenu("&Medir", self.measureTools)

        self.observer = None

        from widgets import CountSelection

    def Activated(self):
        "This function is executed when the workbench is activated"
        import SelectionObserver
        import FreeCADGui

        #self.observer = SelectionObserver.SelObserver()
        #FreeCADGui.Selection.addObserver(self.observer)  # installe la fonction en mode resident
        return

    def Deactivated(self):
        "This function is executed when the workbench is deactivated"

        #FreeCADGui.Selection.removeObserver(self.observer)
        return

    def ContextMenu(self, recipient):
        "This is executed whenever the user right-clicks on screen"
        # "recipient" will be either "view" or "tree"

        # if FreeCAD.activeDraftCommand is None:
        if recipient.lower() == "view":
            print("Menus en la 'View'")
            # if FreeCAD.activeDraftCommand is None:
            presel = FreeCADGui.Selection.getPreselection()
            print(presel.SubElementNames, " - ", presel.PickedPoints)
            if not presel is None:
                if presel.Object.Proxy.Type == "Road":
                    self.appendContextMenu("Road", self.roads)
                elif presel.Object.Proxy.Type == "Pad":
                    self.appendContextMenu("Pad", self.pads)

        '''
        self.contextMenu = QtGui.QMenu()
        menu_item_remove_selected = self.contextMenu.addAction("Remove selected geometry")
        menu_item_remove_all = self.contextMenu.addAction("Clear list")
        if not self.references:
            menu_item_remove_selected.setDisabled(True)
            menu_item_remove_all.setDisabled(True)
        self.connect(
            menu_item_remove_selected,
            QtCore.SIGNAL("triggered()"),
            self.remove_selected_reference
        )
        self.connect(
            menu_item_remove_all,
            QtCore.SIGNAL("triggered()"),
            self.remove_all_references
        )
        parentPosition = self.list_References.mapToGlobal(QtCore.QPoint(0, 0))
        self.contextMenu.move(parentPosition + QPos)
        self.contextMenu.show()
        '''

    def GetClassName(self):
        # this function is mandatory if this is a full python workbench
        return "Gui::PythonWorkbench"


Gui.addWorkbench(PVPlantWorkbench())
