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

const MARGIN: int = 75
const PAGE_WIDTH: float = 500.
const ASPECT_RATIO: float = sqrt(2)

var labels := {}

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
	build_book(book_data.data)

func build_book(d: Dictionary) -> void:
	var pages: Array = d.pages
	for p: Dictionary in pages:
		var page := Page.new()
		match p.page_type:
			"contents":
				pass
			"recipe":
				var title_string: String = MessageParser.new(p["title"]).parse(book_data.game)
				var title = Label.new()
				title.text = title_string
				page.left.add_child(title)
				var dia := Diagram.new(p["diagram"], book_data.game)
				dia.scale(Rect2(Vector2(64., 64.), Vector2(PAGE_WIDTH - 128., PAGE_WIDTH * ASPECT_RATIO - 128.)))
				page.right.add_child(dia)
			_: push_error("%s not known" % p.page_type)
		$ScrollContainer/VBoxContainer.add_child(page)
	return Control.new()

class Page:
	extends HBoxContainer
	var left: Control
	var right: Control
	var title: bool = false:
		set(val):
			left.visible = !val

	func single_page() -> Control:
		var node = PanelContainer.new()
		node.name = "Page"
		node.add_theme_stylebox_override("panel", preload("res://gui/resources/style/paper_panel_style.tres"))
		node.set_custom_minimum_size(Vector2(PAGE_WIDTH, PAGE_WIDTH * ASPECT_RATIO))
		return node

	func _init():
		left = single_page()
		right = single_page()
		add_child(left)
		add_child(right)
