import FreeCAD

if FreeCAD.GuiUp:
    import FreeCADGui
    from PySide import QtGui, QtCore
    import os
else:
    # \cond
    def translate(ctxt, txt):
        return txt

    def QT_TRANSLATE_NOOP(ctxt, txt):
        return txt
    # \endcond

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

from PVPlantResources import DirIcons as DirIcons


class ProjectSetupDialog(QtGui.QWidget):
    '''The editmode TaskPanel to select what you want to export'''

    def __init__(self):
        QtGui.QWidget.__init__(self)

        import os
        # self.form:
        self.form = FreeCADGui.PySideUic.loadUi(
            os.path.join(os.path.dirname(os.path.realpath(__file__)), "ProjectSetup.ui"))
        #self.form.setWindowIcon(QtGui.QIcon(os.path.join(PVPlantResources.DirIcons, "convert.svg")))

        self.bcnf = FreeCADGui.UiLoader().createWidget("Gui::ColorButton")
        self.bcnf.setProperty('color', QtGui.QColor(255, 126, 126))
        self.bcsf = FreeCADGui.UiLoader().createWidget("Gui::ColorButton")
        self.bcsf.setProperty('color', QtGui.QColor(255, 126, 0))
        gridLayout = self.form.tab1.layout()
        gridLayout.addWidget(self.bcnf, 2, 1)
        gridLayout.addWidget(self.bcsf, 2, 2)
        self.setdefaultvalues()

        self.layout = QtGui.QHBoxLayout(self)
        self.layout.setContentsMargins(4, 4, 4, 4)
        self.layout.addWidget(self.form)

        self.form.buttonAccept.clicked.connect(self.onAcceptClick)
        self.form.buttonCancel.clicked.connect(self.onCancelClick)

    def setdefaultvalues(self):
        doc = FreeCAD.ActiveDocument
        pl = doc.PropertiesList
        if "MaximumTiltPositive" in pl:
            self.form.editNFTL.setValue(doc.MaximumTiltPositive)

        if "MaximumTiltPositiveColor" in pl:
            col = QtGui.QColor()
            col.setRgbF(doc.MaximumTiltPositiveColor[0], doc.MaximumTiltPositiveColor[1],
                        doc.MaximumTiltPositiveColor[2])
            self.bcsf.setProperty('color', col)

        if "MaximumTiltNegative" in pl:
            self.form.editSFTL.setValue(-doc.MaximumTiltNegative)

        if "MaximumTiltNegativeColor" in pl:
            col = QtGui.QColor()
            col.setRgbF(doc.MaximumTiltNegativeColor[0], doc.MaximumTiltNegativeColor[1],
                        doc.MaximumTiltNegativeColor[2])
            self.bcnf.setProperty('color', col)

        if "MaximumWestEastSlope" in pl:
            self.form.editWETL.setValue(doc.MaximumWestEastSlope)


    def onAcceptClick(self):
        doc = FreeCAD.ActiveDocument
        pl = doc.PropertiesList
        if not ("FramesChecking" in pl):
            doc.addProperty("App::PropertyBool",
                            "FramesChecking",
                            "PVPlantProject",
                            "All the frames inside this area."
                            ).FramesChecking = False
            doc.setEditorMode("FramesChecking", 1)  # no editable
            doc.setEditorMode("FramesChecking", 2)  # no visible

        if not ("MaximumTiltPositive" in pl):
            doc.addProperty("App::PropertyAngle",
                            "MaximumTiltPositive",
                            "PVPlantProject",
                            "All the frames inside this area."
                            )

        if not ("MaximumTiltPositiveColor" in pl):
            doc.addProperty("App::PropertyColor",
                            "MaximumTiltPositiveColor",
                            "PVPlantProject",
                            "All the frames inside this area."
                            )

        if not ("MaximumTiltNegative" in pl):
            doc.addProperty("App::PropertyAngle",
                            "MaximumTiltNegative",
                            "PVPlantProject",
                            "All the frames inside this area."
                            )

        if not ("MaximumTiltNegativeColor" in pl):
            doc.addProperty("App::PropertyColor",
                            "MaximumTiltNegativeColor",
                            "PVPlantProject",
                            "All the frames inside this area."
                            )

        if not ("MaximumWestEastSlope" in pl):
            doc.addProperty("App::PropertyAngle",
                            "MaximumWestEastSlope",
                            "PVPlantProject",
                            "All the frames inside this area."
                            )

        doc.MaximumTiltPositive = self.form.editNFTL.value()
        col = self.bcsf.property('color')
        doc.MaximumTiltPositiveColor = (col.redF(), col.greenF(), col.blueF())

        doc.MaximumTiltNegative = -self.form.editSFTL.value()
        col = self.bcnf.property('color')
        doc.MaximumTiltNegativeColor = (col.redF(), col.greenF(), col.blueF())

        doc.MaximumWestEastSlope = self.form.editWETL.value()

        self.closeForm()

    def onCancelClick(self):
        self.closeForm()

    def closeForm(self):
        self.close()

class CommandProjectSetup:
    def GetResources(self):
        return {'Pixmap': str(os.path.join(DirIcons, "flash.svg")),
                'Accel': "P, S",
                'MenuText': "Project Setup",
                'ToolTip': "Setup all the variable for this project"}

    def Activated(self):
        taskd = ProjectSetupDialog()
        taskd.setParent(FreeCADGui.getMainWindow())
        taskd.setWindowFlags(QtCore.Qt.Window)
        taskd.show()

    def IsActive(self):
        if FreeCAD.ActiveDocument:
            return True
        else:
            return False


if FreeCAD.GuiUp:
    FreeCADGui.addCommand('ProjectSetup', CommandProjectSetup())

