#!/usr/bin/env python
# (C) 2022 Cadence Design Systems, Inc. (Cadence) 
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence 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 Cadence 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 Cadence 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 CreateReportLayout():

    rows, cols = 3, 2
    reportopts = oedepict.OEReportOptions(rows, cols)
    reportopts.SetPageWidth(100.0)
    reportopts.SetPageHeight(150.0)
    reportopts.SetPageMargins(5.0)
    reportopts.SetCellGap(5.0)
    reportopts.SetHeaderHeight(15.0)
    reportopts.SetFooterHeight(15.0)
    report = oedepict.OEReport(reportopts)

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

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

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

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

    for page in report.GetPages():
        oedepict.OEDrawBorder(page, oedepict.OEBlackPen)
    for header in report.GetHeaders():
        oedepict.OEDrawBorder(header, borderpen)
    for footer in report.GetFooters():
        oedepict.OEDrawBorder(footer, borderpen)

    return report


def HighlightCell(cell, idx):

    font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Default, 10,
                           oedepict.OEAlignment_Center, oechem.OEBlack)
    color = oechem.OEColor(oechem.OELightGrey)
    borderpen = oedepict.OEPen(color, color, oedepict.OEFill_On, 1.0)

    oedepict.OEDrawBorder(cell, borderpen)
    p = oedepict.OE2DPoint(cell.GetWidth() / 2.0, cell.GetHeight() / 2.0 + font.GetSize() / 2.0)
    cell.DrawText(p, "(%d)" % idx, font)


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)


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETCELLS-ALL>
for cell in report.GetCells():
    pass
# @ </SNIPPET-OEREPORT-GETCELLS-ALL>
    HighlightCell(cell, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-cells-all")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETCELLS-ALLPAGES-ROW-ALLCOLS>
row = 2
for cell in report.GetCells(0, row, 0):
    pass
# @ </SNIPPET-OEREPORT-GETCELLS-ALLPAGES-ROW-ALLCOLS>
    HighlightCell(cell, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-cells-allpages-row-allcols")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETCELLS-ALLPAGES-ALLROWS-COL>
col = 2
for cell in report.GetCells(0, 0, col):
    pass
# @ </SNIPPET-OEREPORT-GETCELLS-ALLPAGES-ALLROWS-COL>
    HighlightCell(cell, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-cells-allpages-allrows-col")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETCELLS-ALLPAGES-ROW-COL>
row = 2
col = 2
for cell in report.GetCells(0, row, col):
    pass
# @ </SNIPPET-OEREPORT-GETCELLS-ALLPAGES-ROW-COL>
    HighlightCell(cell, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-cells-allpages-row-col")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETCELLS-PAGE-ROW-ALLCOLS>
page = 3
row = 2
for cell in report.GetCells(page, row, 0):
    pass
# @ </SNIPPET-OEREPORT-GETCELLS-PAGE-ROW-ALLCOLS>
    HighlightCell(cell, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-cells-page-row-allcols")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETCELLS-PAGE-ALLROWS-COL>
page = 3
col = 2
for cell in report.GetCells(page, 0, col):
    pass
# @ </SNIPPET-OEREPORT-GETCELLS-PAGE-ALLROWS-COL>
    HighlightCell(cell, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-cells-page-allrows-col")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETCELLS-PAGE-ROW-COL>
page = 3
row = 2
col = 2
for cell in report.GetCells(page, row, col):
    pass
# @ </SNIPPET-OEREPORT-GETCELLS-PAGE-ROW-COL>
    HighlightCell(cell, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-cells-page-row-col")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETHEADERS>
for header in report.GetHeaders():
    pass
# @ </SNIPPET-OEREPORT-GETHEADERS>
    HighlightCell(header, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-headers")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETFOOTERS>
for footer in report.GetFooters():
    pass
# @ </SNIPPET-OEREPORT-GETFOOTERS>
    HighlightCell(footer, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-footers")


report = CreateReportLayout()
idx = 1
# @ <SNIPPET-OEREPORT-GETBODIES>
for body in report.GetBodies():
    pass
# @ </SNIPPET-OEREPORT-GETBODIES>
    HighlightCell(body, idx)
    idx += 1
WriteReportPageByPage(report, "OEReportIter-bodies")

report = CreateReportLayout()
WriteReportPageByPage(report, "OEReportIter-pages")
