/**************************************************************************/
/*  studio_bridge.h                                                       */
/**************************************************************************/
/*  Bridge between AeThex Studio (web UI) and Engine (C++ runtime)       */
/**************************************************************************/

#ifndef STUDIO_BRIDGE_H
#define STUDIO_BRIDGE_H

#include "core/object/ref_counted.h"
#include "core/io/stream_peer_tcp.h"
#include "core/io/tcp_server.h"
#include "core/io/json.h"
#include "scene/main/node.h"
#include "modules/websocket/websocket_peer.h"

class StudioBridge : public Object {
	GDCLASS(StudioBridge, Object);

private:
	static StudioBridge *singleton;
	
	Ref<TCPServer> tcp_server;
	Vector<Ref<StreamPeerTCP>> pending_clients;
	Vector<Ref<WebSocketPeer>> websocket_clients;
	int server_port = 6007;
	bool is_running = false;
	
	// Current editor state
	Node *edited_scene_root = nullptr;
	Node *selected_node = nullptr;
	
	// HTTP handling
	String _parse_http_request(const String &p_request, Dictionary &r_headers);
	String _build_http_response(const String &p_body, const String &p_content_type = "application/json");
	void _process_server();
	
	// WebSocket handling
	bool _is_websocket_upgrade(const Dictionary &p_headers);
	String _build_websocket_accept_key(const String &p_key);
	String _build_websocket_handshake_response(const String &p_accept_key);
	void _process_websocket_clients();
	void _broadcast_event(const String &p_event_type, const Dictionary &p_data);
	
	// RPC method handlers - Basic
	Dictionary _handle_load_scene(const Dictionary &params);
	Dictionary _handle_save_scene(const Dictionary &params);
	Dictionary _handle_create_node(const Dictionary &params);
	Dictionary _handle_delete_node(const Dictionary &params);
	Dictionary _handle_set_property(const Dictionary &params);
	Dictionary _handle_get_property(const Dictionary &params);
	Dictionary _handle_get_scene_tree(const Dictionary &params);
	Dictionary _handle_select_node(const Dictionary &params);
	Dictionary _handle_run_game(const Dictionary &params);
	Dictionary _handle_stop_game(const Dictionary &params);
	
	// RPC method handlers - Advanced
	Dictionary _handle_move_node(const Dictionary &params);
	Dictionary _handle_duplicate_node(const Dictionary &params);
	Dictionary _handle_rename_node(const Dictionary &params);
	Dictionary _handle_reparent_node(const Dictionary &params);
	Dictionary _handle_get_all_properties(const Dictionary &params);
	Dictionary _handle_get_property_info(const Dictionary &params);
	Dictionary _handle_list_directory(const Dictionary &params);
	Dictionary _handle_get_recent_files(const Dictionary &params);
	Dictionary _handle_attach_script(const Dictionary &params);
	Dictionary _handle_detach_script(const Dictionary &params);
	Dictionary _handle_get_node_script(const Dictionary &params);
	Dictionary _handle_save_script(const Dictionary &params);
	Dictionary _handle_get_node_groups(const Dictionary &params);
	Dictionary _handle_add_to_group(const Dictionary &params);
	Dictionary _handle_remove_from_group(const Dictionary &params);
	Dictionary _handle_get_all_node_types(const Dictionary &params);
	
	// Helper methods
	Dictionary _node_to_dict(Node *p_node);
	Dictionary _error_response(const String &p_message);
	Dictionary _success_response(const Variant &p_result = Variant());

protected:
	static void _bind_methods();

public:
	static StudioBridge *get_singleton();
	
	// Server control
	Error start_server(int p_port = 6007);
	void stop_server();
	bool is_server_running() const { return is_running; }
	void process(); // Call this regularly to handle incoming requests
	
	// RPC handling
	Dictionary handle_rpc_call(const String &p_method, const Dictionary &p_params);
	
	// Event emission (Engine → Studio)
	void emit_scene_changed();
	void emit_node_selected(Node *p_node);
	void emit_property_changed(Node *p_node, const String &p_property);
	void emit_console_output(const String &p_message, const String &p_type = "info");
	
	// Scene management
	void set_edited_scene_root(Node *p_node);
	Node *get_edited_scene_root() const { return edited_scene_root; }
	
	void set_selected_node(Node *p_node);
	Node *get_selected_node() const { return selected_node; }
	
	StudioBridge();
	~StudioBridge();
};

#endif // STUDIO_BRIDGE_H
