extends PanelContainer


var ip_label: Label
var data_instance: Node

var sensor_ip: String = "127.0.0.1"
var data_labels = ["PNT", "Accel", "Gyro", "Magnet"]

var provided_data: Array = [0, 0, 0, 0]
var data_columns: Array

@export var data_column_scene: PackedScene


func _setup_data_columns():
	var container = get_node('HBoxContainer')
	for i in range(provided_data.size()):
		var spacer: Control = container.add_spacer(false)
		spacer.size = Vector2(2, 0)
		var column_instance = data_column_scene.instantiate()
		column_instance.get_node('DataLabel').text = data_labels[i]
		data_columns.append(column_instance)
		if provided_data[i] == 0:
			column_instance.get_node('DataValue').text = "False"
		else:
			column_instance.get_node('DataValue').text = "True"
		container.add_child(column_instance)

func set_provided_data(incoming_sensor_ip: String, incoming_provided_data: Array):
	sensor_ip = incoming_sensor_ip
	ip_label = get_node('HBoxContainer/Label')
	ip_label.text = sensor_ip
	provided_data = incoming_provided_data
	if data_columns.size() == 0:
		_setup_data_columns()
	else:
		for idx in range(provided_data.size()):
			if provided_data[idx] == 0:
				data_columns[idx].get_node('DataValue').text = "False"
			else:
				data_columns[idx].get_node('DataValue').text = "True"

func _on_approve_pressed() -> void:
	ServerSignals.sensor_approved.emit(sensor_ip)

func _on_decline_pressed() -> void:
	ServerSignals.sensor_declined.emit(sensor_ip)
