#!/usr/bin/env python3
# (C) 2017 OpenEye Scientific Software Inc. All rights reserved.
#
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of OpenEye products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. OpenEye claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable OpenEye offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be
# liable for any damages or liability in connection with the Sample Code
# or its use.
########################################################################

from openeye import oechem
from openeye import oemolprop
from openeye import oedepict
from openeye import oegrapheme


def SetAtomProperties(mol, tag, minvalue=float("inf"), maxvalue=float("-inf"), SAndP=True):

    avals = oechem.OEFloatArray(mol.GetMaxAtomIdx())
    psa = oemolprop.OEGet2dPSA(mol, avals, SAndP)

    mol.SetTitle(mol.GetTitle() + "Topological Polar Surface Area = %.2f" % psa)

    for atom in mol.GetAtoms():
        val = avals[atom.GetIdx()]
        atom.SetData(tag, val)
        minvalue = min(minvalue, val)
        maxvalue = max(maxvalue, val)

    return minvalue, maxvalue


class PSAArcFxn(oegrapheme.OESurfaceArcFxnBase):
    def __init__(self, colorg, tag, pen):
        oegrapheme.OESurfaceArcFxnBase.__init__(self)
        self.colorg = colorg
        self.tag = tag
        self.pen = pen

    def __call__(self, image, arc):

        adisp = arc.GetAtomDisplay()
        if adisp is None or not adisp.IsVisible():
            return False

        atom = adisp.GetAtom()
        atompsa = atom.GetData(self.tag)
        if atompsa == 0.0:
            return True

        pen = oedepict.OEPen(self.pen)
        color = self.colorg.GetColorAt(atompsa)
        pen.SetForeColor(color)

        center = arc.GetCenter()
        bgnAngle = arc.GetBgnAngle()
        endAngle = arc.GetEndAngle()
        radius = arc.GetRadius()

        edgeAngle = 10.0
        patternDirection = oegrapheme.OEPatternDirection_Outside
        patternAngle = 10.0
        minPatternWidthRatio = 0.05
        maxPatternWidthRatio = 0.70
        actPatternWidthRatio = min(maxPatternWidthRatio, atompsa * (maxPatternWidthRatio / 40.0))
        oegrapheme.OEDrawEyelashSurfaceArc(image, center, bgnAngle, endAngle, radius, pen,
                                           edgeAngle, patternDirection, patternAngle,
                                           minPatternWidthRatio, actPatternWidthRatio)
        return True

    def CreateCopy(self):
        return PSAArcFxn(self.colorg, self.tag, self.pen).__disown__()
