use super::super::framework::{NodeImpl, get_default_exec_out, eval_input_string};
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, RuntimeValue, ComponentContext};

pub struct ShowMessageNode;

impl NodeImpl for ShowMessageNode {
    fn execute(&self, graph: &NodeGraph, ctx: &mut RuntimeContext, node: &Node) -> Result<Option<usize>, String> {
        let msg = eval_input_string(graph, ctx, node.id, "message")?;
        ctx.show_message(&msg);
        get_default_exec_out(node)
    }
}

impl super::super::framework::NodeDescription for ShowMessageNode {
    fn description(&self) -> &str {
        "Displays a popup message box to the user."
    }
}

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, true, ""));
    *next_pin_id += 1;
    pins.push(Pin::new(*next_pin_id, PinType::Exec, false, ""));
    *next_pin_id += 1;
    pins.push(Pin::new(*next_pin_id, PinType::String, true, "message"));
    *next_pin_id += 1;
    pins
}

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

crate::register_node!(NodeSpec {
    id: "ShowMessage",
    display_name: "Show Message",
    documentation: "Displays a popup message box to the user test.",
    category: NodeCategory::Action,
    library: NodeLibrary::Core,
    param_kind: ParamKind::None,
    default_params,
    pins,
    execute: Some(execute),
    evaluate: None,
});
