# Hurry Curry! - a game about cooking
# Copyright (C) 2026 Hurry Curry! Contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
class_name ServerListItem
extends PanelContainer

var error_style_focus: StyleBoxFlat = preload("res://gui/resources/style/error_focus_style.tres")
var error_style_normal: StyleBoxFlat = preload("res://gui/resources/style/error_normal_style.tres")

@onready var title: Label = $MarginContainer/VBoxContainer/Title
@onready var online: Label = $MarginContainer/VBoxContainer/HBoxContainer/Online
@onready var address: Label = $MarginContainer/VBoxContainer/HBoxContainer/Address
@onready var button: Button = $Button

func setup(name_: String, online_players: int, version: Array, addresses: Array[String]):
	title.text = name_
	if not Global.is_protocol_version_compatible(version):
		button.disabled = true
		button.add_theme_stylebox_override("disabled", error_style_normal)
		button.add_theme_stylebox_override("focus", error_style_focus)
		online.text = tr("c.menu.play.server_version_mismatch")
		online.add_theme_color_override("font_color", Color("ff2222"))
		address.visible = false
		return
	online.text = tr("c.menu.play.server_players").format([online_players])
	
	if addresses.size() > 1 or not addresses[0].begins_with("wss://"):
		address.visible = false
	else:
		address.text = addresses[0].trim_suffix(":443").trim_prefix("wss://").trim_prefix("hurrycurry.")
	
	# This node is in group not no_click_sound, so sounds won't be automatically connected
	# by menu system. Reason: These nodes are deleted and re-created every few seconds
	# in server list, and signals are only connected on ready.
	button.pressed.connect(Sound.play_click)
	button.mouse_entered.connect(Sound.play_hover)
