extends Node3D

@onready var camera: Camera3D = $Camera3D

# BOUTONS
@onready var start: Button = $HBoxContainer/Start
@onready var quit: Button = $HBoxContainer/Quit
@onready var credit: Button = $HBoxContainer/Credit

@onready var fr: TextureButton = $HBoxContainer/fr
@onready var en: TextureButton = $HBoxContainer/en
@onready var screen: Button = $HBoxContainer2/Screen

@onready var credit_label: Label = $Sprite3D/SubViewport/credit_label

var but_credit_exist : bool = true

var cible_rot_y
var cible_rot_x

func _ready() -> void:
	Sauvegarde.is_in_menu = true
	
	load_lang(Sauvegarde.language)
	if Sauvegarde.language == "en":
		en.texture_normal = preload("uid://bdcehsogmfiv2")
		fr.texture_normal = preload("uid://dusujvur4igj4")
	screen.text = Sauvegarde.get_text("window_screen")
	
	cible_rot_y = deg_to_rad( 147.8 )
	cible_rot_x = deg_to_rad( -6.1 )


func _on_start_pressed() -> void:
	get_tree().change_scene_to_file( "res://scenes/main.scn" )
	Sauvegarde.is_in_menu = false

func _on_quit_pressed() -> void:
	get_tree().quit()

func _physics_process(delta: float) -> void:
	camera.rotation.y = lerp_angle(camera.rotation.y, cible_rot_y, 3 * delta)
	camera.rotation.x = lerp_angle(camera.rotation.x, cible_rot_x, 3 * delta)

func _on_credit_pressed() -> void:
	cible_rot_y = deg_to_rad(100)
	cible_rot_x = deg_to_rad(16)
	credit.queue_free()
	but_credit_exist = false
	

func _on_fr_pressed() -> void:
	fr.texture_normal = preload("uid://cdnkgbmh886ry")
	en.texture_normal = preload("uid://mpo1px8jnlyq")
	
	load_lang("fr")

func _on_en_pressed() -> void:
	en.texture_normal = preload("uid://bdcehsogmfiv2")
	fr.texture_normal = preload("uid://dusujvur4igj4")
	
	load_lang("en")

func _on_screen_pressed() -> void:
	if get_window().mode == Window.MODE_FULLSCREEN:
		get_window().mode = Window.MODE_WINDOWED
		screen.text = Sauvegarde.get_text("full_screen")
	else:
		get_window().mode = Window.MODE_FULLSCREEN
		screen.text = Sauvegarde.get_text("window_screen")

func load_lang(l):
	Sauvegarde.language = l
	start.text = Sauvegarde.get_text("start")
	quit.text = Sauvegarde.get_text("quit")
	if but_credit_exist :
		credit.text = Sauvegarde.get_text("credit")
	if get_window().mode == Window.MODE_FULLSCREEN:
		screen.text = Sauvegarde.get_text("window_screen")
	else:
		screen.text = Sauvegarde.get_text("full_screen")
		
	credit_label.text = Sauvegarde.get_text("credit_detail")
