extends CanvasLayer

# Debug Overlay - Shows network and player information

var debug_label: Label
var info_label: Label
var network_manager

var visible_debug = false

func _ready():
	network_manager = get_node_or_null("/root/NetworkManager")
	
	# Create debug label (toggleable)
	debug_label = Label.new()
	debug_label.name = "DebugLabel"
	debug_label.position = Vector2(10, 10)
	debug_label.add_theme_color_override("font_color", Color.YELLOW)
	add_child(debug_label)
	debug_label.visible = false
	
	# Create info label (always visible in top right)
	info_label = Label.new()
	info_label.name = "InfoLabel"
	info_label.add_theme_color_override("font_color", Color.WHITE)
	info_label.add_theme_font_size_override("font_size", 20)
	info_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT  # Text grows from right to left
	
	# Load standard_font.png as bitmap font
	var standard_font_resource = null
	if ResourceLoader.exists("res://assets/fonts/standard_font.png"):
		standard_font_resource = load("res://assets/fonts/standard_font.png")
		if standard_font_resource:
			info_label.add_theme_font_override("font", standard_font_resource)
	
	add_child(info_label)
	
	# Anchor to top right corner - text grows from right to left
	# set_anchors_preset automatically sets layout_mode to anchors
	info_label.set_anchors_preset(Control.PRESET_TOP_RIGHT)
	info_label.offset_left = -400  # Width of label (grows leftward) - increased for longer text like "CLIENT" and large IDs
	info_label.offset_right = -10   # Right edge at 10px from screen edge
	info_label.offset_top = 10      # Top edge at 10px from screen top
	info_label.offset_bottom = 150  # Height of label - increased for multiple lines

func _process(_delta):
	# Toggle debug with F3
	if Input.is_action_just_pressed("ui_cancel"):
		visible_debug = !visible_debug
		debug_label.visible = visible_debug
	
	if visible_debug:
		_update_debug_info()
	
	# Always update info label
	_update_info_label()

func _update_info_label():
	var info = []
	
	if multiplayer.has_multiplayer_peer():
		# Show role
		if multiplayer.is_server():
			info.append("HOST")
		else:
			info.append("CLIENT")
		
		# Show peer ID
		info.append("ID: %d" % multiplayer.get_unique_id())
	else:
		info.append("OFFLINE")
	
	# Show local player position
	var game_world = get_node_or_null("../GameWorld")
	if not game_world:
		game_world = get_node_or_null("/root/Main/GameWorld")
	if game_world:
		var player_manager = game_world.get_node_or_null("PlayerManager")
		if player_manager:
			var local_players = player_manager.get_local_players()
			if local_players.size() > 0:
				var player = local_players[0]
				info.append("Pos: (%.1f, %.1f)" % [player.global_position.x, player.global_position.y])
	
	info_label.text = "\n".join(info)

func _update_debug_info():
	var info = []
	
	# Network info
	if multiplayer.has_multiplayer_peer():
		info.append("Network: Connected")
		info.append("Peer ID: %d" % multiplayer.get_unique_id())
		info.append("Is Server: %s" % multiplayer.is_server())
		if network_manager:
			info.append("Connected Peers: %d" % network_manager.players_info.size())
	else:
		info.append("Network: Offline")
	
	# Player info
	if network_manager:
		info.append("\nPlayers:")
		for peer_id in network_manager.players_info.keys():
			var player_info = network_manager.players_info[peer_id]
			info.append("  Peer %d: %d local players" % [peer_id, player_info.local_player_count])
	
	# Performance
	info.append("\nFPS: %d" % Engine.get_frames_per_second())
	
	debug_label.text = "\n".join(info)
