#!/usr/bin/env python
# (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 oedepict

###############################################################
# USED TO GENERATE CODE SNIPPETS FOR THE OEDEPICT DOCUMENTATION
###############################################################


def DepictMoleculesWithLabel(smiles, ss, highlight, label, basefilename):

    mol = oechem.OEGraphMol()
    oechem.OESmilesToMol(mol, smiles)
    oedepict.OEPrepareDepiction(mol)
    opts = oedepict.OE2DMolDisplayOptions(220, 160, oedepict.OEScale_AutoScale)
    opts.SetTitleLocation(oedepict.OETitleLocation_Hidden)
    disp = oedepict.OE2DMolDisplay(mol, opts)

    unique = True
    for match in ss.Match(mol, unique):
        oedepict.OEAddHighlighting(disp, highlight, match)
        oedepict.OEAddLabel(disp, label, match)

    image = oedepict.OEImage(disp.GetWidth(), disp.GetHeight())
    oedepict.OERenderMolecule(image, disp)

    oedepict.OEWriteImage(basefilename + ".png", image)
    oedepict.OEWriteImage(basefilename + ".pdf", image)


smiles = "c1ccc2c(c1)cc(O)cn2"
ss = oechem.OESubSearch("c1ncc(O)cc1")

highlight = oedepict.OEHighlightByLasso(oechem.OEDarkRed)
highlight.SetConsiderAtomLabelBoundingBox(True)

# @ <SNIPPET-OEHIGHLIGHTLABEL-DEFAULT>
label = oedepict.OEHighlightLabel('match')
# @ </SNIPPET-OEHIGHLIGHTLABEL-DEFAULT>
DepictMoleculesWithLabel(smiles, ss, highlight, label, "OEHighlightLabel_Default")

# @ <SNIPPET-OEHIGHLIGHTLABEL-COLOR>
label = oedepict.OEHighlightLabel('match', oechem.OEDarkRed)
# @ </SNIPPET-OEHIGHLIGHTLABEL-COLOR>
DepictMoleculesWithLabel(smiles, ss, highlight, label, "OEHighlightLabel_Color")


# @ <SNIPPET-OEHIGHLIGHTLABEL-FONT>
font = oedepict.OEFont(oedepict.OEFontFamily_Courier, oedepict.OEFontStyle_Bold, 12,
                       oedepict.OEAlignment_Center, oechem.OEDarkRed)
label = oedepict.OEHighlightLabel('match', font)
# @ </SNIPPET-OEHIGHLIGHTLABEL-FONT>
DepictMoleculesWithLabel(smiles, ss, highlight, label, "OEHighlightLabel_Font")

# @ <SNIPPET-OEHIGHLIGHTLABEL-SETFONT>
label = oedepict.OEHighlightLabel('match')
font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Default, 12,
                       oedepict.OEAlignment_Center, oechem.OEDarkRed)
label.SetFont(font)
# @ </SNIPPET-OEHIGHLIGHTLABEL-SETFONT>
DepictMoleculesWithLabel(smiles, ss, highlight, label, "OEHighlightLabel_SetFont")

# @ <SNIPPET-OEHIGHLIGHTLABEL-SETBOUNDINGBOXPEN>
label = oedepict.OEHighlightLabel('match')
pen = oedepict.OEPen(oechem.OEColor(255, 220, 220), oechem.OEDarkRed, oedepict.OEFill_On, 16.0,
                     oedepict.OEStipple_ShortDash)
label.SetBoundingBoxPen(pen)
# @ </SNIPPET-OEHIGHLIGHTLABEL-SETBOUNDINGBOXPEN>
DepictMoleculesWithLabel(smiles, ss, highlight, label, "OEHighlightLabel_SetBoundingBoxPen")

# @ <SNIPPET-OEHIGHLIGHTLABEL-SETFONTSCALE>
label = oedepict.OEHighlightLabel('match')
label.SetFontScale(1.2)
# @ </SNIPPET-OEHIGHLIGHTLABEL-SETFONTSCALE>
DepictMoleculesWithLabel(smiles, ss, highlight, label, "OEHighlightLabel_SetFontScale")
