"""
Module contains the OmniGraph node implementation of omni.graph.fmod
"""
import carb
import numpy as np
import omni.graph.core as og


class OgnFMod:
    """Node to find floating point remainder"""

    @staticmethod
    def compute(db) -> bool:
        try:
            db.outputs.result.value = np.fmod(db.inputs.a.value, db.inputs.b.value)
        except TypeError as error:
            db.log_error(f"Remainder could not be performed: {error}")
            return False

        return True

    @staticmethod
    def on_connection_type_resolve(node) -> None:
        atype = node.get_attribute("inputs:a").get_resolved_type()
        btype = node.get_attribute("inputs:b").get_resolved_type()
        resultattr = node.get_attribute("outputs:result")
        resulttype = resultattr.get_resolved_type()

        # we can only infer the output given both inputs are resolved and they are the same.
        if (
            atype.base_type != og.BaseDataType.UNKNOWN
            and btype.base_type != og.BaseDataType.UNKNOWN
            and resulttype.base_type == og.BaseDataType.UNKNOWN
        ):
            if atype.base_type == btype.base_type:
                sum_type = og.Type(
                    atype.base_type,
                    max(atype.tuple_count, btype.tuple_count),
                    max(atype.array_depth, btype.array_depth),
                )
                resultattr.set_resolved_type(sum_type)
            else:
                carb.log_warn(f"Can not compute remainder of types {atype} and {btype}")
