extends Node2D

@export var code_len : int = 2
@export var ghost_tokens : int = 2
@export var allowed_time : float = 5
@export var starting_lives : int = 3
@export var icon_container: HBoxContainer
@export var icons : Array[Texture]
@export var sounds : Dictionary[String, AudioStream]
@export var progress_bar : TextureProgressBar
@export var scroll_container : ScrollContainer
@export var score_label : Label
@export var life_container : HBoxContainer
@export var life_texture : Texture
@export var round_label : Label
@export var sound_player : AudioStreamPlayer
@export var finish_screen : Control
@export var sam_sprite : AnimatedSprite2D
var current_time : float = 5
var score : int = 0
var lives : int = 3
var code : Array[int]
var code_rects : Array[TextureRect]
var gaming : bool = false
var codepos : int = 0 #current position in the code to check against
var test_break : float = 1.5
var target_scroll : float
var finished : bool = false

# start scrolling at codepos == 11

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	#start_round()
	draw_lives()
	pass # Replace with function body.

func draw_lives():
	for child in life_container.get_children():
		child.queue_free()
	for i in range(lives):
		var new_tex = TextureRect.new()
		new_tex.texture = life_texture
		life_container.add_child(new_tex)

func lose_life():
	lives -= 1
	
	draw_lives()
	if lives <= 0:
		end_game()

func end_game():
	finished = true

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	target_scroll = (codepos*50) - 550
	scroll_container.scroll_horizontal = lerpf(scroll_container.scroll_horizontal,target_scroll,0.05)
	score_label.text = str(score)
	round_label.text = str(code_len) + " of " + str(code_len+ghost_tokens)
	finish_screen.visible = finished
	if not gaming:
		round_label.text = "Get Ready!"
	if finished:
		round_label.text = "Length " + str(code_len+ghost_tokens)
	
	if gaming:
		current_time -= delta
		progress_bar.value = (allowed_time-current_time)/allowed_time
		
	else:
		test_break -= delta
		progress_bar.value -= delta/1.5
		if test_break <= 0 and not finished:
			start_round()
	
	progress_bar.modulate = Color(1,1,1,progress_bar.value)
	
	if gaming and current_time <= 0:
		lose_round()
	
	if Input.is_action_just_pressed("up"):
		process_input(0)
	elif Input.is_action_just_pressed("right"):
		process_input(1)
	elif Input.is_action_just_pressed("down"):
		process_input(2)
	elif Input.is_action_just_pressed("left"):
		process_input(3)

func start_round():
	current_time = allowed_time
	codepos = 0
	generate_code()
	gaming = true
	sound_player.stream = sounds["reveal"]
	sound_player.play()
	sam_sprite.play("default")
	
	#scroll_container.scroll_horizontal = 0
	pass

func process_input(direction : int):
	if gaming:
		if(code[codepos] == direction):
			#correct input 
			icon_container.get_child(codepos).modulate = Color(0,1,0)
			score += codepos+1
			codepos += 1
			
			if(codepos == code_len):
				win_round()
			else:
				sound_player.stream = sounds["success"]
				sound_player.play()
		else:
			#incorrect input
			codepos = 0
			sound_player.stream = sounds["error"]
			sound_player.play()
			#scroll_container.scroll_horizontal = 0
			for i in range(code_len):
				icon_container.get_child(i).modulate = Color(1,1,1)

func win_round():
	#todo: bous for remaining time?
	sam_sprite.play("bow")
	sound_player.stream = sounds["roundSuccess"]
	sound_player.play()
	gaming = false
	codepos = 0
	test_break = 1.7 # todo: replace with gamer samurai animation stuff
	if ghost_tokens > 0:
		ghost_tokens -= 1
		code_len += 1
		
	else:
		ghost_tokens = 2
		code_len -= 1
		score += 100

func lose_round():
	gaming = false
	sam_sprite.play("tan")
	codepos = 0
	test_break = 2.5
	sound_player.stream = sounds["tant"]
	sound_player.play()
	lose_life()

func generate_code():
	code.clear()
	for child in icon_container.get_children():
		child.queue_free()
	for i in range(code_len):
		var ran_dir : int = randi_range(0,3)
		code.append(ran_dir)
		var new_texture_rect : TextureRect = TextureRect.new()
		new_texture_rect.texture = icons[ran_dir]
		icon_container.add_child(new_texture_rect)
	for i in range(ghost_tokens):
		var new_texture_rect : TextureRect = TextureRect.new()
		new_texture_rect.texture = icons[4]
		new_texture_rect.modulate = Color(1,1,1,0.25)
		icon_container.add_child(new_texture_rect)
	print(code)


func _on_menu_button_pressed() -> void:
	get_tree().change_scene_to_file("res://menu.tscn")
