#!/usr/bin/env python3
# (C) 2026 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.

"""Depict molecules with polar surface area in a multi-page report."""

import argparse
import os
import sys
from pathlib import Path

from openeye import oechem, oedepict, oegrapheme, oemolprop
from rich_argparse import HelpPreviewAction, RichHelpFormatter

__SCRIPT_NAME__ = Path(__file__).absolute().stem
__SCRIPT_DESC__ = "Depict molecules with polar surface area in a multi-page report."
__SCRIPT_TOOLKITS__ = ["oechem", "oedepict", "oegrapheme", "oemolprop"]
__SCRIPT_CATEGORIES__ = ["depiction"]


def parse_args() -> argparse.Namespace:
    """Parse command-line arguments."""
    parser = argparse.ArgumentParser(
        add_help=True,
        formatter_class=RichHelpFormatter,
        description="[yellow]" + __SCRIPT_DESC__ + "[/yellow]",
    )

    input_group = parser.add_argument_group("Input options")
    input_group.add_argument(
        "--mol",
        type=str,
        required=True,
        metavar="MOL-FILE",
        help="input molecule file",
    )

    report_group = parser.add_argument_group("Report options")
    report_group.add_argument(
        "--report",
        type=str,
        required=True,
        metavar="REPORT-FILE",
        help="output report file (PDF)",
    )
    report_group.add_argument(
        "--rows",
        type=int,
        default=3,
        choices=range(2, 6),
        metavar="N",
        help="number of rows per page (default: %(default)s)",
    )
    report_group.add_argument(
        "--cols",
        type=int,
        default=2,
        choices=range(1, 3),
        metavar="N",
        help="number of columns per page (default: %(default)s)",
    )
    report_group.add_argument(
        "--page-by-page",
        action="store_true",
        help="write pages of report to separate numbered image files",
    )

    parser.add_argument("--help-image", action=HelpPreviewAction)
    return parser.parse_args()


def main() -> int:
    """Depict molecules with polar surface area in a multi-page report."""
    args = parse_args()

    _check_report_file(args)

    # check input file
    ifs = oechem.oemolistream()
    if not ifs.open(args.mol):
        oechem.OEThrow.Fatal("Cannot open input file!")

    # initialize multi-page report
    report_options = oedepict.OEReportOptions(args.rows, args.cols)
    report = oedepict.OEReport(report_options)

    # setup depiction options
    width, height = report.GetCellWidth(), report.GetCellHeight()
    opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale)

    # read molecules and prepare them for depiction
    mol_list = []
    for mol in ifs.GetOEGraphMols():
        oedepict.OEPrepareDepiction(mol)
        mol_list.append(oechem.OEGraphMol(mol))

    # depict molecules with PSA
    depict_molecules_with_psa(report, mol_list, opts)

    if args.page_by_page:
        oedepict.OEWriteReportPageByPage(args.report, report)
    else:
        oedepict.OEWriteReport(args.report, report)

    return os.EX_OK


def depict_molecules_with_psa(
    report: oedepict.OEReport,
    mol_list: list[oechem.OEGraphMol],
    opts: oedepict.OE2DMolDisplayOptions,
) -> None:
    """
    Depict molecules with polar surface area visualization in a report.

    Calculates a uniform scale across all molecules, computes per-atom
    PSA values, and renders eyelash-style surface arcs colored by PSA
    contribution into report cells.

    Args:
        report: Multi-page report to render into.
        mol_list: List of molecules to depict.
        opts: Display options for 2D molecule depiction.

    """
    mol_scale = float("inf")
    for mol in mol_list:
        mol_scale = min(mol_scale, oegrapheme.OEGetMoleculeSurfaceScale(mol, opts))
    opts.SetScale(mol_scale)

    tag = oechem.OEGetTag("PSA")

    min_value = float("inf")
    max_value = float("-inf")
    for mol in mol_list:
        min_value, max_value = set_atom_properties(
            mol, tag, min_value, max_value, s_and_p=True
        )

    negative_color = oechem.OEColorStop(min_value, oechem.OEWhite)
    positive_color = oechem.OEColorStop(max_value, oechem.OEDarkBlue)
    color_gradient = oechem.OELinearColorGradient(negative_color, positive_color)

    arc_fxn = PSAArcFxn(color_gradient, tag, opts.GetDefaultBondPen())

    for mol in mol_list:

        for atom in mol.GetAtoms():
            oegrapheme.OESetSurfaceArcFxn(mol, atom, arc_fxn)

        disp = oedepict.OE2DMolDisplay(mol, opts)
        oegrapheme.OEDraw2DSurface(disp)

        cell = report.NewCell()
        oedepict.OERenderMolecule(cell, disp)


def set_atom_properties(
    mol: oechem.OEMolBase,
    tag: int,
    min_value: float = float("inf"),
    max_value: float = float("-inf"),
    s_and_p: bool = True,  # noqa: FBT002
) -> tuple[float, float]:
    """
    Calculate per-atom PSA values and store them as generic data.

    Computes the topological polar surface area contribution for each
    atom and stores it under the given tag. Updates and returns the
    running min/max values across calls.
    """
    atom_values = oechem.OEFloatArray(mol.GetMaxAtomIdx())
    psa = oemolprop.OEGet2dPSA(mol, atom_values, s_and_p)

    mol.SetTitle(f"{mol.GetTitle()}Topological Polar Surface Area = {psa:.2f}")

    for atom in mol.GetAtoms():
        val = atom_values[atom.GetIdx()]
        atom.SetData(tag, val)
        min_value = min(min_value, val)
        max_value = max(max_value, val)

    return min_value, max_value


class PSAArcFxn(oegrapheme.OESurfaceArcFxnBase):
    """Surface arc function for rendering PSA eyelash arcs."""

    def __init__(
        self,
        color_gradient: oechem.OELinearColorGradient,
        tag: int,
        pen: oedepict.OEPen,
    ) -> None:
        """Initialize arc function."""
        super().__init__()
        self._color_gradient = color_gradient
        self._tag = tag
        self._pen = pen

    def __call__(
        self,
        image: oedepict.OEImageBase,
        arc: oegrapheme.OESurfaceArc,
    ) -> bool:
        """Draw arc."""
        atom_disp = arc.GetAtomDisplay()
        if atom_disp is None or not atom_disp.IsVisible():
            return False

        atom = atom_disp.GetAtom()
        atom_psa = atom.GetData(self._tag)
        if atom_psa == 0.0:
            return True

        pen = oedepict.OEPen(self._pen)
        color = self._color_gradient.GetColorAt(atom_psa)
        pen.SetForeColor(color)

        center = arc.GetCenter()
        bgn_angle = arc.GetBgnAngle()
        end_angle = arc.GetEndAngle()
        radius = arc.GetRadius()

        edge_angle = 10.0
        pattern_direction = oegrapheme.OEPatternDirection_Outside
        pattern_angle = 10.0
        min_pattern_width_ratio = 0.05
        max_pattern_width_ratio = 0.70
        act_pattern_width_ratio = min(
            max_pattern_width_ratio, atom_psa * (max_pattern_width_ratio / 40.0)
        )
        oegrapheme.OEDrawEyelashSurfaceArc(
            image,
            center,
            bgn_angle,
            end_angle,
            radius,
            pen,
            edge_angle,
            pattern_direction,
            pattern_angle,
            min_pattern_width_ratio,
            act_pattern_width_ratio,
        )
        return True

    def CreateCopy(self):  # noqa: ANN201, N802
        """Copy constructor."""
        return PSAArcFxn(self._color_gradient, self._tag, self._pen).__disown__()


def _check_report_file(args: argparse.Namespace) -> None:
    """Validate report output file extension."""
    ext = Path(args.report).suffix[1:]
    if not oedepict.OEIsRegisteredImageFile(ext):
        oechem.OEThrow.Fatal("Unknown image output type!")

    if not args.page_by_page and not oedepict.OEIsRegisteredMultiPageImageFile(ext):
        oechem.OEThrow.Warning("Report will be generated into separate pages!")
        args.page_by_page = True


setattr(main, "__SCRIPT_NAME__", __SCRIPT_NAME__)
setattr(main, "__SCRIPT_DESC__", __SCRIPT_DESC__)
setattr(main, "__SCRIPT_TOOLKITS__", __SCRIPT_TOOLKITS__)
setattr(main, "__SCRIPT_CATEGORIES__", __SCRIPT_CATEGORIES__)

if __name__ == "__main__":
    sys.exit(main())
