# 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 BookMenu
extends Menu

const MARGIN: int = 32

var current_page := 0

@onready var first := $Margin/HBoxContainer/First/PanelContainer/MarginContainer
@onready var second := $Margin/HBoxContainer/Second/PanelContainer/MarginContainer

static var TOOLS_WITH_COUNTER = ["cutting-board", "rolling-board", "deep-fryer", "sink", "book"]

@export var title_font: Font
@export var default_font: Font

var pages: Array

class BookData:
	var game: Game
	var data: Dictionary

	func _init(game_, data_) -> void:
		game = game_
		data = data_

var book_data: BookData

func _ready():
	super()
	book_data = data
	pages = book_data.data["pages"]
	# Flip book for rtl languages
	$Margin/HBoxContainer/Previous.flip_h = not is_layout_rtl()
	$Margin/HBoxContainer/Next.flip_h = is_layout_rtl()
	build_page()

func build_page() -> void:
	$Margin/HBoxContainer/Previous.disabled = current_page == 0
	$Margin/HBoxContainer/Next.disabled = current_page == pages.size() - 1
	for c: Node in first.get_children():
		c.queue_free()
	for c: Node in second.get_children():
		c.queue_free()

	var p: Dictionary = pages[current_page]
	match p.page_type:
		"contents":
			var title := build_title(MessageParser.new(p["title"], book_data.game).result)
			var vbox := VBoxContainer.new()
			vbox.add_child(title)
			for i: Array in p.table:
				var m := MessageParser.new(i[0], book_data.game)
				vbox.add_child(build_contents_entry(m.result, i[1]))
			second.add_child(vbox)
		"recipe":
			var title := build_title(MessageParser.new(p["title"], book_data.game).result)
			var par := build_paragraph(MessageParser.new(p["description"], book_data.game).result)
			var vbox := VBoxContainer.new()
			vbox.add_child(title)
			vbox.add_child(par)
			var dia := Diagram.new(p["diagram"], book_data.game)
			first.add_child(vbox)
			second.add_child(dia)
		_: push_error("%s not known" % p.page_type)
	return Control.new()

func build_title(m: String) -> Label:
	var label := Label.new()
	label.add_theme_font_override("font", title_font)
	label.add_theme_font_size_override("font_size", 42)
	label.add_theme_color_override("font_color", Color.BLACK)
	label.text = m
	return label

func build_paragraph(m: String) -> Label:
	var label := Label.new()
	label.add_theme_font_override("font", default_font)
	label.add_theme_font_size_override("font_size", 32)
	label.add_theme_color_override("font_color", Color.BLACK)
	label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	label.text = m
	return label

func build_contents_entry(m: String, ref: int) -> Control:
	var b := LinkButton.new()
	b.text = m
	b.pressed.connect(func():
		current_page = ref
		build_page()
	)
	b.add_theme_font_override("font", default_font)
	b.add_theme_font_size_override("font_size", 32)
	b.add_theme_color_override("font_color", Color.BLACK)
	return b

func _on_previous_pressed() -> void:
	current_page = clampi(current_page - 1, 0, pages.size() - 1)
	build_page()

func _on_next_pressed() -> void:
	current_page = clampi(current_page + 1, 0, pages.size() - 1)
	build_page()
