# Hurry Curry! - a game about cooking
# Copyright (C) 2025 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/>.
#
extends Menu

@onready var anim = $AnimationPlayer
@onready var options = $Side/Margin/Options
@onready var game: Game = $"../Game"
@onready var abort_button: Button = $Side/Margin/Options/Abort
@onready var leave_button: Button = $Side/Margin/Options/Leave

var opened
func _ready():
	opened = Time.get_ticks_msec()
	game.join_state_updated.connect(_on_game_join_state_changed)
	_on_game_join_state_changed(game.join_state)
	update_abort_button()
	super()

func update_abort_button():
	abort_button.disabled = game.in_lobby or game.join_state == Game.JoinState.SPECTATING
	if game.in_lobby: abort_button.tooltip_text = tr("c.error.cannot_cancel_no_game")
	elif game.join_state != Game.JoinState.JOINED: abort_button.tooltip_text = tr("c.error.must_join_to_cancel")
	else: abort_button.tooltip_text = ""

func anim_setup(): pass
func _menu_open():
	anim.play("activate")
	await anim.animation_finished
	game.mp.send_idle(true)

func _menu_exit():
	game.mp.send_idle(false)
	anim.play_backwards("activate")
	await anim.animation_finished

func _menu_cover(state: bool):
	if Settings.read("ui.scale_mode") == "disabled": return # TODO perhaps check for overlap than scale_mode
	if state: anim.play_backwards("activate")
	else: anim.play("activate")

func _on_resume_pressed():
	exit()

func _on_main_menu_pressed():
	parent_menu.replace_menu("res://gui/menus/main/main.tscn")

func _on_settings_pressed():
	submenu("res://gui/menus/settings/settings.tscn")

func _on_reconnect_pressed():
	parent_menu.replace_menu("res://gui/menus/game.tscn", parent_menu.data)

func _on_quit_pressed():
	quit()

func _on_lobby_pressed():
	game.mp.send_chat(game.my_player_id, "/end")
	exit()

func _on_leave_pressed():
	game.toggle_join()

func _on_game_join_state_changed(state: Game.JoinState):
	match state:
		Game.JoinState.JOINED:
			leave_button.disabled = false
			leave_button.text = tr("c.menu.ingame.leave")
		Game.JoinState.SPECTATING:
			leave_button.disabled = false
			leave_button.text = tr("c.menu.ingame.join")
		Game.JoinState.WAITING:
			leave_button.disabled = true
