extends HBoxContainer

var base = AtlasTexture.new()
var yellow = AtlasTexture.new()
var orange = AtlasTexture.new()
var red = AtlasTexture.new()
var purple = AtlasTexture.new()
var slots: Array[TextureRect] = []
var colors: Array[AtlasTexture] = []
var burning_colors: Array[AtlasTexture] = []
var slot_states = [0, 0, 0]  
var progres_index = 0
var is_brewing 
var brew_explosion = true
@onready var witch = get_parent()  
var explosion_scene = preload("res://scenes/explosion.tscn")# Called when the node enters the scene tree for the first time.

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	print(witch)
	base.atlas = preload("res://assets/Cauldron's Brew/Equiptment.png")
	base.region = Rect2(96, 96, 96, 96)
	yellow.atlas = preload("res://assets/Cauldron's Brew/Cauldron and Powder.png")
	yellow.region = Rect2(96, 96, 96, 96)
	orange.atlas = preload("res://assets/Cauldron's Brew/Cauldron and Powder.png")
	orange.region = Rect2(192, 96, 96, 96)
	red.atlas = preload("res://assets/Cauldron's Brew/Cauldron and Powder.png")
	red.region = Rect2(288, 96, 96, 96)
	purple.atlas = preload("res://assets/Cauldron's Brew/Cauldron and Powder.png")
	purple.region = Rect2(480, 96, 96, 96)
	slots = [$Empty1, $Empty2, $Empty3]
	colors = [base, yellow, red, orange, purple]
	enrich_burning_colors()
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass
	
func change_texture(slot_index: int, color_index: int):
	slots[slot_index].texture = colors[color_index]
	slot_states[slot_index] = color_index
	pass

func reset_texture():
	for slot in slots:
		slot.texture = colors[0]
	slot_states = [0,0,0]
		
func ignite_cauldrons():
	for i in slots.size():
		slots[i].texture = burning_colors[slot_states[i]]
		await get_tree().create_timer(0.4).timeout
	if brew_explosion:
		var boom = explosion_scene.instantiate()
		get_parent().add_child(boom) 
		boom.scale = Vector2(3,3)
		boom.global_position = witch.global_position

	pass
	
func enrich_burning_colors():
	for color in colors:
		var burning = AtlasTexture.new()
		burning.atlas = color.atlas
		burning.region = Rect2(color.region.position.x, color.region.position.y + 96, 96, 96)
		burning_colors.append(burning)
		
func progres_bar(fruit):
	if is_brewing:
		return
	if fruit is Apple: 
		change_texture(progres_index,2)
	if fruit is Grape:
		change_texture(progres_index, 4)
	progres_index += 1
	if progres_index == 3:
		brew(slot_states)
		progres_index = 0
	pass

func brew(fruits):
	is_brewing = true
	var unique = get_unique_fruits()
	await ignite_cauldrons()
	if fruits == [2, 2, 4]:
		witch.shoot_fire_swirl()
	elif unique.has(2) and unique.size() == 1:
		witch.shoot_fireballs()
	elif unique.has(4) and unique.size() == 1:
		witch.shoot_shuriken()
	elif unique.has(2) and unique.has(4) and unique.size() == 2:
		witch.shoot_fireballs()
		witch.shoot_shuriken()
	reset_texture()
	is_brewing = false
	
func get_unique_fruits() -> Array:
	var unique = []
	for fruit in slot_states:
		if fruit != null and not fruit in unique:
			unique.append(fruit)
	return unique
