# 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/>.
#
extends Menu
class_name MapDetailsSelector

const MAX_BOT_COUNT_PER_TYPE: int = 3

var two_handed: bool
var default_two_handed: bool
var bots_enabled := false
var bot_counts: Dictionary[String, int] = {}

var bot_reset_buttons: Dictionary[String, Button] = {}
var bot_inc_buttons: Dictionary[String, Button] = {}
var bot_dec_buttons: Dictionary[String, Button] = {}

@onready var game: Game = $"../../Game" # TODO

@onready var title_label: Label = $OuterMargin/Panel/InnerMargin/VBoxContainer/Title
@onready var bots_container: VBoxContainer = $OuterMargin/Panel/InnerMargin/VBoxContainer/VBoxContainer/Bots
@onready var bot_settings: Control = $OuterMargin/Panel/InnerMargin/VBoxContainer/VBoxContainer/Bots/ScrollContainerCustom/BotSettings
@onready var bot_settings_container: Control = $OuterMargin/Panel/InnerMargin/VBoxContainer/VBoxContainer/Bots/ScrollContainerCustom
@onready var start_button: Button = $OuterMargin/Panel/InnerMargin/VBoxContainer/HBoxContainer/Start
@onready var two_handed_button: CheckButton = $OuterMargin/Panel/InnerMargin/VBoxContainer/VBoxContainer/TwoHanded
@onready var custom_game_warning: Label = $OuterMargin/Panel/InnerMargin/VBoxContainer/CustomGameWarning

func _ready() -> void:
	title_label.text = data[0]["display_name"]
	default_two_handed = false if data[0]["hand_count"] == 1. else true
	two_handed = default_two_handed
	two_handed_button.button_pressed = two_handed
	
	var bot_algos: Array = data[1]
	for algo: String in bot_algos:
		bot_counts[algo] = 0
		
		var h := HBoxContainer.new()
		h.name = algo
		var add := Button.new()
		add.text = "+"
		var reset := Button.new()
		reset.size_flags_horizontal = SIZE_EXPAND_FILL
		var remove := Button.new()
		remove.text = "-"
		bot_reset_buttons[algo] = reset
		bot_inc_buttons[algo] = add
		bot_dec_buttons[algo] = remove
		update_bot_reset_text(algo)
		add.pressed.connect(increase_bot_count.bind(algo))
		reset.pressed.connect(reset_bot_count.bind(algo))
		remove.pressed.connect(decrease_bot_count.bind(algo))
		h.add_child(remove)
		h.add_child(reset)
		h.add_child(add)
		bot_settings.add_child(h)
	
	super()
	start_button.grab_focus()

func increase_bot_count(algo_id: String):
	bot_counts[algo_id] += 1
	update_bot_reset_text(algo_id)

func decrease_bot_count(algo_id: String):
	bot_counts[algo_id] -= 1
	update_bot_reset_text(algo_id)

func reset_bot_count(algo_id: String):
	if bot_counts[algo_id] == 0: bot_counts[algo_id] = 1
	else: bot_counts[algo_id] = 0
	update_bot_reset_text(algo_id)

func update_bot_reset_text(algo_id: String):
	var display_name: String = tr("s.bot.%s" % algo_id)
	bot_reset_buttons[algo_id].text = "%s (%d)" % [display_name, bot_counts[algo_id]]
	set_disabled(bot_reset_buttons[algo_id], false)
	set_disabled(bot_inc_buttons[algo_id], not bot_counts[algo_id] < MAX_BOT_COUNT_PER_TYPE)
	set_disabled(bot_dec_buttons[algo_id], not bot_counts[algo_id] > 0)

func _on_enable_bots_toggled(toggled_on: bool) -> void:
	bots_enabled = toggled_on
	bot_settings_container.visible = toggled_on
	update_warning()

func _on_two_handed_toggled(toggled_on: bool) -> void:
	two_handed = toggled_on
	update_warning()

func _on_back_pressed() -> void:
	exit(false)

func _on_start_pressed() -> void:
	var selected_map_name: String = data[0]["name"]
	var hand_count: int = 2 if two_handed else 1
	
	var selected_bots: Array[String] = []
	if bots_enabled:
		for k in bot_counts.keys():
			for i in range(bot_counts[k]):
				selected_bots.append(k)
	
	@warning_ignore("incompatible_ternary")
	game.mp.send_start_game_vote(game.my_player_id, selected_map_name, hand_count if two_handed != default_two_handed else null, selected_bots if bots_enabled else null)
	exit(true)

func update_warning():
	custom_game_warning.visible = two_handed != default_two_handed or bots_enabled
