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


"""Generates image of soot ball from the Miyazaki's film Spirited Away."""


import os
import sys

from openeye import oechem, oedepict, oegrapheme


def main() -> int:
    """Depict sooth ball."""
    image_width, image_height = 200, 200
    image = oedepict.OEImage(image_width, image_height)

    center = oedepict.OE2DPoint(image_width / 2.0, image_height / 2.0)
    radius = min(image_width / 3.0, image_height / 3.0)

    black_pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_On, 6.0)
    oegrapheme.OEDrawEyelashCircle(image, center, radius, black_pen)

    white_pen = oedepict.OEPen(oechem.OEWhite, oechem.OEWhite, oedepict.OEFill_On, 1.0)

    _draw_ellipse(image, center + oedepict.OE2DPoint(+30, 0), 60, 50, white_pen)
    image.DrawCircle(center + oedepict.OE2DPoint(+20, 0), 3.0, black_pen)
    _draw_ellipse(image, center + oedepict.OE2DPoint(-30, 0), 60, 50, white_pen)
    image.DrawCircle(center + oedepict.OE2DPoint(-20, 0), 3.0, black_pen)

    # add OEDepict TK watermark

    font = oedepict.OEFont(
        oedepict.OEFontFamily_Default,
        oedepict.OEFontStyle_Default,
        10,
        oedepict.OEAlignment_Right,
        oechem.OELightGrey,
    )
    text = "Generated by OEDepict TK"
    image.DrawText(
        oedepict.OE2DPoint(image_width - 5.0, image_height - 5.0), text, font
    )

    # write image

    oedepict.OEWriteImage("soot.svg", image)

    return os.EX_OK


def _draw_ellipse(
    image: oedepict.OEImageBase,
    center: oedepict.OE2DPoint,
    width: float,
    height: float,
    pen: oedepict.OEPen,
) -> None:
    image.DrawCubicBezier(
        center + oedepict.OE2DPoint(0.0, -height / 2.0),
        center + oedepict.OE2DPoint(+width / 2.0, -height / 2.0),
        center + oedepict.OE2DPoint(+width / 2.0, +height / 2.0),
        center + oedepict.OE2DPoint(0.0, +height / 2.0),
        pen,
    )

    image.DrawCubicBezier(
        center + oedepict.OE2DPoint(0.0, +height / 2.0),
        center + oedepict.OE2DPoint(-width / 2.0, +height / 2.0),
        center + oedepict.OE2DPoint(-width / 2.0, -height / 2.0),
        center + oedepict.OE2DPoint(0.0, -height / 2.0),
        pen,
    )


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