#!/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 ScaleReportOptions(opts, scale):

    for margin in [oedepict.OEMargin_Top, oedepict.OEMargin_Bottom, oedepict.OEMargin_Left, oedepict.OEMargin_Right]:
        opts.SetPageMargin(margin, scale * opts.GetPageMargin(margin))
    opts.SetCellGap(scale * opts.GetCellGap())
    opts.SetHeaderHeight(scale * opts.GetHeaderHeight())
    opts.SetFooterHeight(scale * opts.GetFooterHeight())
    opts.SetPageWidth(scale * opts.GetPageWidth())
    opts.SetPageHeight(scale * opts.GetPageHeight())
    return opts


def CreateScaleReportLayout(opts, scale):

    opts = ScaleReportOptions(opts, scale)
    report = oedepict.OEReport(opts)

    borderpen = oedepict.OEPen(oechem.OEGrey, oechem.OEGrey, oedepict.OEFill_Off, 1.0)

    first = report.NewBody()
    oedepict.OEDrawBorder(first, borderpen)

    nrgridpages = 1
    for p in range(0, nrgridpages):
        for c in range(0, report.NumRowsPerPage() * report.NumColsPerPage()):
            cell = report.NewCell()
            oedepict.OEDrawBorder(cell, borderpen)

    for page in report.GetPages():
        oedepict.OEDrawBorder(page, oedepict.OEBlackPen)

    if opts.GetHeaderHeight() > 0.0:
        for header in report.GetHeaders():
            oedepict.OEDrawBorder(header, borderpen)
    if opts.GetFooterHeight() > 0.0:
        for footer in report.GetFooters():
            oedepict.OEDrawBorder(footer, borderpen)

    return report


def WriteReportPageByPage(report, filenamebase):

    for pidx in range(1, report.NumPages() + 1):
        oedepict.OEWriteReport("%s-%d.pdf" % (filenamebase, pidx), report, pidx)
        oedepict.OEWriteReport("%s-%d.png" % (filenamebase, pidx), report, pidx)


scale = 0.25

# @ <SNIPPET-OEREPORTOPTIONS-DEFAULT>
opts = oedepict.OEReportOptions(3, 2)
# @ </SNIPPET-OEREPORTOPTIONS-DEFAULT>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_Default")

# @ <SNIPPET-OEREPORTOPTIONS-SETPAGESIZE>
opts = oedepict.OEReportOptions(3, 2)
opts.SetPageSize(oedepict.OEPageSize_US_Legal)
# @ </SNIPPET-OEREPORTOPTIONS-SETPAGESIZE>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetPageSize")

# @ <SNIPPET-OEREPORTOPTIONS-SETPAGEORIENTATION>
opts = oedepict.OEReportOptions(3, 2)
opts.SetPageOrientation(oedepict.OEPageOrientation_Landscape)
# @ </SNIPPET-OEREPORTOPTIONS-SETPAGEORIENTATION>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetPageOrientation")

# @ <SNIPPET-OEREPORTOPTIONS-SETPAGEWIDTH>
opts = oedepict.OEReportOptions(3, 2)
opts.SetPageWidth(opts.GetPageWidth() * 0.50)
# @ </SNIPPET-OEREPORTOPTIONS-SETPAGEWIDTH>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetPageWidth")

# @ <SNIPPET-OEREPORTOPTIONS-SETPAGEHEIGHT>
opts = oedepict.OEReportOptions(3, 2)
opts.SetPageHeight(opts.GetPageHeight() * 0.50)
# @ </SNIPPET-OEREPORTOPTIONS-SETPAGEHEIGHT>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetPageHeight")

# @ <SNIPPET-OEREPORTOPTIONS-SETPAGEMARGIN>
opts = oedepict.OEReportOptions(3, 2)
opts.SetPageMargin(oedepict.OEMargin_Left, 100)
opts.SetPageMargin(oedepict.OEMargin_Right, 100)
# @ </SNIPPET-OEREPORTOPTIONS-SETPAGEMARGIN>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetPageMargin")

# @ <SNIPPET-OEREPORTOPTIONS-SETPAGEMARGINS>
opts = oedepict.OEReportOptions(3, 2)
opts.SetPageMargins(100)
# @ </SNIPPET-OEREPORTOPTIONS-SETPAGEMARGINS>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetPageMargins")

# @ <SNIPPET-OEREPORTOPTIONS-SETCELLGAP>
opts = oedepict.OEReportOptions(3, 2)
opts.SetCellGap(100)
# @ </SNIPPET-OEREPORTOPTIONS-SETCELLGAP>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetCellGap")

# @ <SNIPPET-OEREPORTOPTIONS-SETHEADERHEIGHT>
opts = oedepict.OEReportOptions(3, 2)
opts.SetHeaderHeight(100)
# @ </SNIPPET-OEREPORTOPTIONS-SETHEADERHEIGHT>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetHeaderHeight")

# @ <SNIPPET-OEREPORTOPTIONS-SETFOOTERHEIGHT>
opts = oedepict.OEReportOptions(3, 2)
opts.SetFooterHeight(100)
# @ </SNIPPET-OEREPORTOPTIONS-SETFOOTERHEIGHT>
report = CreateScaleReportLayout(opts, scale)
WriteReportPageByPage(report, "OEReportOptions_SetFooterHeight")
