"""
This is the implementation of the OGN node defined in OgnDrawDebugCurve.ogn
"""
import carb
import numpy as np
import omni.debugdraw as dd
import omni.graph.core as og


class OgnDrawDebugCurve:
    """
    Draws a curve using omni.debugdraw
    """

    @staticmethod
    def compute(db) -> bool:
        """Compute the outputs from the current input"""

        curvepoints = db.inputs.curvepoints
        color = db.inputs.color
        closed = db.inputs.closed
        try:
            ddi = dd._debugDraw.acquire_debug_draw_interface()  # noqa: PLW0212
            rgb_bytes = (np.clip(color, 0, 1.0) * 255).astype("uint8").tobytes()
            argb_bytes = b"\xff" + rgb_bytes
            argb = int.from_bytes(argb_bytes, byteorder="big")
            carbpoints = [carb.Float3(p.tolist()) for p in curvepoints]
            # carb.log_warn(f'{color} = {argb_bytes} = {argb}, {carbpoints[0]}, {carbpoints[1]}')
            for i in range(len(carbpoints) - 1):
                ddi.draw_line(carbpoints[i], argb, carbpoints[i + 1], argb)
            if closed:
                ddi.draw_line(carbpoints[-1], argb, carbpoints[0], argb)

        except Exception as error:  # noqa: PLW0703
            import traceback

            raise RuntimeError(traceback.format_exc()) from error
        db.outputs.execOut = og.ExecutionAttributeState.ENABLED
        return True
