# -*- coding: utf-8 -*-
from typing import List

import logging
from ${library} import ${function}

from nodedge.blocks.block import Block
from nodedge.blocks.block_exception import EvaluationError
from nodedge.blocks.block_config import BLOCKS_ICONS_PATH, registerNode
from nodedge.connector import SocketType

_LOG = logging.getLogger(__name__)

try:
    from nodedge.blocks.op_node import ${op_block_string}
except NameError:
    _LOG.warning(f"Not registered block: {__name__}")
    op_block_string = -1


@registerNode(${op_block_string})
class ${block_name}Block(Block):
    icon = f"{BLOCKS_ICONS_PATH}/${icon_filename}.png"
    operationCode = ${op_block_string}
    operationTitle = "${operation_title}"
    contentLabel = "${operation_symbol}"
    contentLabelObjectName = "BlockBackground"
    evalString = "${function}"
    library = "${library}"
    inputSocketTypes: List[SocketType] = ${input_socket_types}
    outputSocketTypes: List[SocketType] = ${output_socket_types}

    def evalImplementation(self):
        inputs = []
        for i in range(len(self.inputSockets)):
            inputs.append(self.inputNodeAt(i))

        try:
            evaluatedInputs = [str(currentInput.eval()) for currentInput in inputs]
            operation = f"{${block_name}Block.evalString}({', '.join(evaluatedInputs)})"
            result = eval(operation)
        except TypeError as e:
            raise EvaluationError(e)

        self.value = result

        return self.value
