use super::super::framework::{NodeImpl, get_default_exec_out};
use crate::gui_builder::node_registry::{NodeSpec, ParamKind};
use crate::gui_builder::nodes::{Node, NodeCategory, NodeGraph, NodeLibrary, NodeParams, Pin, PinType};
use crate::gui_builder::interpreter::ExecutionStack;
use crate::gui_builder::runtime::RuntimeContext;

pub struct EventStartNode;

impl NodeImpl for EventStartNode {
    fn execute(&self, _graph: &NodeGraph, _ctx: &mut RuntimeContext, node: &Node) -> Result<Option<usize>, String> {
        // EventStart just passes execution to its output
        get_default_exec_out(node)
    }
}

impl super::super::framework::NodeDescription for EventStartNode {
    fn description(&self) -> &str {
        "Entry point for an event handler. Execution starts here when the event is triggered."
    }
}

fn default_params() -> NodeParams {
    NodeParams::None
}

fn pins(next_pin_id: &mut usize) -> Vec<Pin> {
    let mut pins = Vec::new();
    pins.push(Pin::new(*next_pin_id, PinType::Exec, false, ""));
    *next_pin_id += 1;
    pins
}

fn exec(graph: &NodeGraph, ctx: &mut RuntimeContext, node: &Node, _stack: &mut ExecutionStack) -> Result<Option<usize>, String> {
    EventStartNode.execute(graph, ctx, node)
}

crate::register_node!(NodeSpec {
    id: "EventStart",
    display_name: "Event Start",
    documentation: "Entry point for an event handler. Execution starts here when the event is triggered.",
    category: NodeCategory::Entry,
    library: NodeLibrary::Core,
    param_kind: ParamKind::None,
    default_params,
    pins,
    execute: Some(exec),
    evaluate: None,
});
