# 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

var authors := ["metamuffin", "nokoe", "tpart"]
var contributors := ["sofviic", "BigBrotherNii", "Miner34", "jnms"]
const cc_by_4 := "CC-BY 4.0"
const cc_by_3 := "CC-BY 3.0"
const cc_by_sa_4 := "CC-BY-SA 4.0"
const cc0 := "CC0"

const LICENSE_LINKS = {
	cc_by_4: "https://creativecommons.org/licenses/by/4.0/",
	cc_by_3: "https://creativecommons.org/licenses/by/3.0/",
	cc0: "https://creativecommons.org/publicdomain/zero/1.0/",
}

const AGPL_NOTICE := """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/>."""

const SOURCE_CODE := "https://codeberg.org/hurrycurry/hurrycurry"
const REPORT_ISSUE := "https://codeberg.org/hurrycurry/hurrycurry/issues"

func _ready() -> void:
	super ()
	$side/margin/options/first/source.tooltip_text = SOURCE_CODE
	$side/margin/options/first/report_issue.tooltip_text = REPORT_ISSUE

var translators = {
	tr("c.settings.ui.language.ar"): ["sofviic"],
	tr("c.settings.ui.language.bar"): ["Ember"],
	tr("c.settings.ui.language.el"): ["n0toose"],
	tr("c.settings.ui.language.eu"): ["josuigoa"],
	tr("c.settings.ui.language.fi"): ["Ricky-Tigg"],
	tr("c.settings.ui.language.fr"): ["fnetX", "lejun", "macbook-girl"],
	tr("c.settings.ui.language.he"): ["RustyStriker"],
	tr("c.settings.ui.language.it"): ["Miner34", "solemden"],
	tr("c.settings.ui.language.ja"): ["BigBrotherNii"],
	tr("c.settings.ui.language.nl"): ["Vistaus"],
	tr("c.settings.ui.language.pl"): ["tranzystorekk"],
	tr("c.settings.ui.language.ru"): ["0ko"],
	tr("c.settings.ui.language.sv"): ["Mahatma_G"],
	tr("c.settings.ui.language.tr"): ["furkanunsalan", "tekrei"],
	tr("c.settings.ui.language.vi"): ["hthienloc"],
	tr("c.settings.ui.language.zh_Hans"): ["Outbreak2096"],
	tr("c.settings.ui.language.zh_Hant"): ["hugoalh"],
}

var credits := [
	[tr("c.credits.models"), [
		["kenney.nl", "Various Models", cc0],
		["Kay Lousberg", "Kitchen tiles", cc0],
		["Poly by Google", "Strawberry", cc_by_3],
		["Poly by Google", "Fish", cc_by_3],
		["Poly by Google", "Rolling pin", cc_by_3],
		["jeremy", "Propeller hat", cc_by_3],
		["jeremy", "Donut", cc_by_3]
	]],
	[tr("c.credits.sounds"), [
		["Dryoma", "Footstep sounds", cc_by_4],
		["Koops", "Page_Turn_24.wav", cc_by_4],
		["InspectorJ", "Pencil, Writing, Close, A.wav", cc_by_4],
		["Dillon Becker", "Super Dialogue Audio Pack", cc_by_4],
		["Ellr", "Universal UI/Menu Soundpack", cc_by_4],
		["toyoto", "Woosh Fleuret Escrime B.wav", cc_by_4],
		["deleted_user_877451", "Game_over.wav", cc_by_3],
		["Quaternius", "Someting, dont remember...", cc0],
		["Dillon Becker", "Super Dialogue Audio Pack V1", cc_by_4],
		["Ekrcoaster", "Water steaming on hot surface #2", cc0]
	]],
	[tr("c.credits.music"), [
		["Giorgi Latso, Asuas", "Debussy: Images I. Reflets dans l'eau (adapted: bars 1-35)", cc_by_sa_4],
	]],
	[tr("c.credits.other"), [
		["Ray Trace", "Nintendo_Switch_Pro_Controller.svg", cc_by_4],
		["Amousey", "Curved solid arrow.svg", cc0]
	]],
	[tr("c.credits.translations"), translators]
]

func _menu_cover(state):
	$side.visible = not state

func credits_text() -> String:
	var text = "[center]"
	authors.shuffle()
	contributors.shuffle()
	
	text += "\n\n\n[b]%s[/b]\n\n%s\n\n[b]%s[/b]\n\n%s\n\n[b]%s[/b]\n\n\n" % [
		tr("c.credits.title"),
		tr("c.credits.developed_by"),
		"\n".join(authors),
		tr("c.credits.contributors"),
		",  ".join(contributors),
	]

	for section in credits:
		text += "[b]%s[/b]\n\n" % section[0]
		if typeof(section[1]) == TYPE_DICTIONARY:
			text += "[table=2]"
			for key in section[1]:
				text += "[cell][right]%s[/right][/cell]" % key
				text += "[cell][left]%s[/left][/cell]" % ", ".join(section[1][key])
			text += "[/table]"
		else:
			text += "[table=3]"
			for entry in section[1]:
				text += "[cell][right]%s[/right][/cell]" % entry[0]
				text += "[cell][left]%s[/left][/cell]" % entry[1]
				text += "[cell][left]%s[/left][/cell]" % entry[2]
			text += "[/table]"
		text += "\n\n\n"
	
	text += "[b]%s[/b]\n\n[table=2]" % tr("c.credits.licenses")
	for key in LICENSE_LINKS:
		text += "[cell][right]%s[/right][/cell]" % key
		text += "[cell][left][url=%s]%s[/url][/left][/cell]" % [LICENSE_LINKS[key], tr("c.credits.licenses.link_label")]
	text += "[/table]\n\n\n"

	text += "\n[b]%s[/b]\n\n\n[/center]" % tr("c.credits.thanks")
	return text

func legal_text() -> String:
	var all: Array[String] = []
	var translators_list: Array[String] = []
	for c in translators.values(): translators_list.append_array(c)
	translators_list.shuffle()
	authors.shuffle()
	contributors.shuffle()
	all.append_array(authors)
	all.append_array(contributors)
	all.append_array(translators_list)

	var text := "Hurry Curry! - a game about cooking\n"
	text += "[code]Copyright 2024, 2025, 2026 %s\n\n" % ", ".join(dedup_array(all))
	text += "%s[/code]\n\n" % AGPL_NOTICE
	text += tr("c.legal.using_godot")
	text += "\n\n[code]%s[/code]" % Engine.get_license_text()
	return text

func version_text() -> String:
	var text := "[center][b]Hurry Curry![/b]\n\n"
	OS.get_version()
	text += "[table=2]"
	var row = "[cell][right]%s[/right][/cell][cell][left]%s[/left][/cell]"
	text += row % [tr("c.version.game"), Global.VERSION]
	text += row % [tr("c.version.protocol"), "%s.%s" % [Multiplayer.VERSION_MAJOR, Multiplayer.VERSION_MINOR]]
	text += row % [tr("c.version.godot"), Engine.get_version_info().string]
	text += row % [tr("c.version.os"), OS.get_name()]
	text += row % [tr("c.version.arch"), Engine.get_architecture_name()]
	text += row % [tr("c.version.distribution"), Global.DISTRIBUTION]

	text += "[/table]"
	text += "[/center]"
	return text

func _on_credits_pressed() -> void:
	submenu("res://gui/menus/popup_large.tscn", credits_text())

func _on_legal_pressed() -> void:
	submenu("res://gui/menus/popup_large.tscn", legal_text())

func _on_version_pressed() -> void:
	submenu("res://gui/menus/popup_large.tscn", version_text())

func _on_report_issue_pressed() -> void:
	OS.shell_open(REPORT_ISSUE)

func _on_source_pressed() -> void:
	OS.shell_open(SOURCE_CODE)

func _on_back_pressed() -> void:
	exit()

func dedup_array(a: Array) -> Array:
	var b = []
	for x in a: if not b.has(x): b.append(x)
	return b
