from typing import Any, cast
import random
random.seed(0)
import numpy as np
np.random.seed(0)
import tensorflow as tf
import onnx2tf.gs as gs
from onnx2tf.utils.enums import ONNX_DTYPES_TO_TF_DTYPES
from onnx2tf.utils.common_functions import (
    get_constant_or_variable,
    print_node_info,
    inverted_operation_enable_disable,
    make_tf_node_info,
)


@print_node_info
@inverted_operation_enable_disable
def make_node(
    *,
    graph_node: gs.Node,
    tf_layers_dict: dict,
    **kwargs: Any,
):
    """RandomNormalLike

    Parameters
    ----------
    graph_node: gs.Node
        graph_surgeon Node

    tf_layers_dict: dict
        optype, shape, dtype, tensorflow graph
    """
    before_op_output_shape_trans_1 = \
        tf_layers_dict.get(graph_node.inputs[0].name, {}).get('before_op_output_shape_trans', True)
    before_op_output_shape_trans = \
        before_op_output_shape_trans_1

    graph_node_input = get_constant_or_variable(
        graph_node.inputs[0],
        before_op_output_shape_trans,
    )
    input_tensor = tf_layers_dict[graph_node_input.name]['tf_node'] \
        if isinstance(graph_node_input, gs.Variable) else graph_node_input
    graph_node_output: gs.Variable = graph_node.outputs[0]

    shape = graph_node_output.shape
    dtype = graph_node_output.dtype

    rdtype = graph_node.attrs.get('dtype', 1)
    rmean = graph_node.attrs.get('mean', 0.0)
    rscale = graph_node.attrs.get('scale', 1.0)
    rseed = graph_node.attrs.get('seed', 0)
    rshape = graph_node_input.shape \
        if input_tensor is None else input_tensor.shape
    if isinstance(rshape, tf.TensorShape):
        try:
            rshape = rshape.as_list()
        except ValueError:
            rshape = None
    if isinstance(rshape, tuple):
        rshape = list(rshape)
    if rshape is None:
        rshape = tf.shape(input_tensor) if input_tensor is not None else graph_node_output.shape
    if isinstance(rshape, list):
        rshape = [
            s if not isinstance(s, str) else None for s in rshape
        ]
        if input_tensor is not None and any(s is None for s in rshape):
            # Stateful random ops do not accept None in shape lists.
            rshape = tf.shape(input_tensor)

    # Preserving Graph Structure (Dict)
    tf_layers_dict[graph_node_output.name] = {
        'optype': graph_node.op,
        'shape': shape,
        'dtype': dtype,
    }

    # Generation of TF OP
    tf_layers_dict[graph_node_output.name]['tf_node'] = \
        tf.random.normal(
            shape=cast(Any, rshape),
            mean=rmean,
            stddev=rscale,
            dtype=ONNX_DTYPES_TO_TF_DTYPES[rdtype],
            seed=rseed,
            name=graph_node.name,
        )

    # Generation of Debug Info
    tf_layers_dict[graph_node_output.name]['tf_node_info'] = \
        make_tf_node_info(
            node_info={
                'tf_op_type': tf.random.normal,
                'tf_inputs': {
                    'shape': shape,
                    'mean': rmean,
                    'stddev': rscale,
                    'dtype': ONNX_DTYPES_TO_TF_DTYPES[rdtype],
                    'seed': rseed,
                },
                'tf_outputs': {
                    'output': tf_layers_dict[graph_node_output.name]['tf_node'],
                },
            }
        )
