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

if FreeCAD.GuiUp:
    import FreeCADGui

else:
    # \cond
    def translate(ctxt, txt):
        return txt

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

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

import os
from PVPlantResources import DirIcons as DirIcons


def checkSingleTracker(frame, val):
    doc = FreeCAD.ActiveDocument
    if val:
        if frame.AngleY < doc.MaximumTiltNegative.Value:
            frame.ViewObject.ShapeColor = doc.MaximumTiltNegativeColor
        elif frame.AngleY > doc.MaximumTiltPositive.Value:
            frame.ViewObject.ShapeColor = doc.MaximumTiltPositiveColor
        else:
            frame.ViewObject.ShapeColor = frame.Setup.ViewObject.ShapeColor
    else:
        frame.ViewObject.ShapeColor = frame.Setup.ViewObject.ShapeColor


def checkTrackers(val):
    from Utils.PVPlantUtils import findObjects
    tlist = findObjects("Tracker")
    for obj in tlist:
        checkSingleTracker(obj, val)


class CommandRackCheck:

    def GetResources(self):
        checked = False
        if hasattr(FreeCAD.ActiveDocument, "FramesChecking"):
            checked = FreeCAD.ActiveDocument.FramesChecking
        return {'Pixmap': str(os.path.join(DirIcons, "checked.png")),
                'MenuText': "Tracker slope checker",
                'Accel': "R, C",
                'ToolTip': "Tracker slope checker.",
                'CmdType': "NoTransaction",
                'Checkable': checked}

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

    def Activated(self, val):
        val = bool(val)
        if not hasattr(FreeCAD.ActiveDocument, "FramesChecking"):
            ''' Abrir dialogo '''
        else:
            FreeCAD.ActiveDocument.FramesChecking = val
        checkTrackers(val)
        return


if FreeCAD.GuiUp:
    FreeCADGui.addCommand('PVPlantRackCheck', CommandRackCheck())

