extends Control

@onready
var qr_panel: Panel = $QRPanel

@onready
var qr_code_rect: QRCodeRect = $QRPanel/QRCodeRect

@onready
var url_label: Label = $QRPanel/URLLabel


func _ready() -> void:
	qr_code_rect.light_module_color = Color.WHITE
	qr_code_rect.dark_module_color = Color.BLACK
	qr_code_rect.quiet_zone_size = 4
	
	# Generate QR code for the search URL
	var url: String = Settings.default_path + "/search"
	qr_code_rect.data = url
	url_label.text = qr_code_rect.data
	print("QR code generated for: ", url)

func show_window() -> void:
	visible = true
	# Regenerate QR code in case settings changed
	var url: String = Settings.default_path + "/search"
	qr_code_rect.data = url
	url_label.text = qr_code_rect.data


func _input(event: InputEvent) -> void:
	if self.visible == true:
		if event is InputEventMouseButton && event.is_pressed():
			var evLocal: InputEvent = make_input_local(event)
			if !Rect2(Vector2(0, 0), qr_panel.size).has_point(evLocal.position):
				self.visible = false


func hide_window() -> void:
	visible = false
