# /**********************************************************************
# *                                                                     *
# * 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                                                                 *
# *                                                                     *
# ***********************************************************************

import FreeCAD
import ArchComponent
import Part
import os
import zipfile
import re

if FreeCAD.GuiUp:
    import FreeCADGui
    from DraftTools import translate
else:
    # \cond
    def translate(ctxt,txt):
        return txt
    def QT_TRANSLATE_NOOP(ctxt,txt):
        return txt
    # \endcond

import os
from PVPlantResources import DirIcons as DirIcons

__title__ = "PVPlant Areas"
__author__ = "Javier Braña"
__url__ = "http://www.sogos-solar.com"

import PVPlantResources
from PVPlantResources import DirIcons as DirIcons
Dir3dObjects = os.path.join(PVPlantResources.DirResources, "3dObjects")

vector = ["Y", "YN", "Z", "ZN", "D"]

def makePCS():
    obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "PowerConversionSystem")
    PowerConverter(obj)
    ViewProviderPowerConverter(obj.ViewObject)

    try:
        folder = FreeCAD.ActiveDocument.PowerConversionSystemGroup
    except:
        folder = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", 'PowerConversionSystemGroup')
        folder.Label = "PowerConversionSystemGroup"
    folder.addObject(obj)
    return obj


class PowerConverter(ArchComponent.Component):
    def __init__(self, obj):
        ''' Initialize the Area object '''
        ArchComponent.Component.__init__(self, obj)
        self.Type = None
        self.obj = None
        self.setProperties(obj)

    def setProperties(self, obj):
        pl = obj.PropertiesList

        # Transformer properties
        if not "Technology" in pl:
            obj.addProperty("App::PropertyEnumeration",
                            "Technology",
                            "Transformer",
                            "Number of phases and type of transformer"
                            ).Technology = ["Single Phase Transformer", "Three Phase Transformer"]

        if not "PowerPrimary" in pl:
            obj.addProperty("App::PropertyPower",
                            "PowerPrimary",
                            "Transformer",
                            "The base file this component is built upon").PowerPrimary = 6000000000

        if not "PowerSecundary1" in pl:
            obj.addProperty("App::PropertyPower",
                            "PowerSecundary1",
                            "Transformer",
                            "The base file this component is built upon").PowerSecundary1 = 3000000000

        if not "PowerSecundary2" in pl:
            obj.addProperty("App::PropertyPower",
                            "PowerSecundary2",
                            "Transformer",
                            "The base file this component is built upon").PowerSecundary2 = 3000000000

        if not "VoltagePrimary" in pl:
            obj.addProperty("App::PropertyElectricPotential",
                            "VoltagePrimary",
                            "Transformer",
                            "The base file this component is built upon").VoltagePrimary = 33000000000

        if not "VoltageSecundary1" in pl:
            obj.addProperty("App::PropertyElectricPotential",
                            "VoltageSecundary1",
                            "Transformer",
                            "The base file this component is built upon").VoltageSecundary1 = 11000000000

        if not "VoltageSecundary2" in pl:
            obj.addProperty("App::PropertyElectricPotential",
                            "VoltageSecundary2",
                            "Transformer",
                            "The base file this component is built upon").VoltageSecundary2 = 11000000000

        if not "VectorPrimary" in pl:
            obj.addProperty("App::PropertyEnumeration",
                            "VectorPrimary",
                            "Transformer",
                            "The base file this component is built upon").VectorPrimary = vector

        if not "VectorSecundary1" in pl:
            obj.addProperty("App::PropertyEnumeration",
                            "VectorSecundary1",
                            "Transformer",
                            "The base file this component is built upon").VectorSecundary1 = vector

        if not "VectorSecundary2" in pl:
            obj.addProperty("App::PropertyEnumeration",
                            "VectorSecundary2",
                            "Transformer",
                            "The base file this component is built upon").VectorSecundary2 = vector

        self.Type = "PowerConverter"
        obj.Proxy = self

    def onDocumentRestored(self, obj):
        """ Method run when the document is restored """
        self.setProperties(obj)

    def onBeforeChange(self, obj, prop):
        ''' '''
        # This method is called before a property is changed.
        # It can be used to validate the property value or to update other properties.
        # If the property is not valid, you can raise an exception.
        # If you want to prevent the change, you can return False.
        # Otherwise, return True to allow the change.
        return True

    def onChanged(self, obj, prop):
        ''' '''


    def execute(self, obj):
        ''' '''
        # obj.Shape: compound
        # |- body: compound
        # |- transformer: solid
        # |- primary switchgear: compound
        # |- secundary 1 switchgear: compound
        # |- secundary 2 switchgear: compound

        pl = obj.Placement
        obj.Shape = Part.makeBox(6058, 2438, 2591)  # Placeholder for the shape
        obj.Placement = pl



class ViewProviderPowerConverter(ArchComponent.ViewProviderComponent):
    def __init__(self, vobj):
        ArchComponent.ViewProviderComponent.__init__(self, vobj)

    def getIcon(self):
        return str(os.path.join(PVPlantResources.DirIcons, "Inverter.svg"))

class CommandPowerConverter:

    def GetResources(self):
        return {'Pixmap': str(os.path.join(PVPlantResources.DirIcons, "Inverter.svg")),
                'Accel': "E, P",
                'MenuText': "Power Converter",
                'ToolTip': "Power Converter",}

    def Activated(self):
        sinverter = makePCS()

    def IsActive(self):
        active = not (FreeCAD.ActiveDocument is None)
        return active

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('PowerConverter', CommandPowerConverter())

