extends Node2D

@export var time_limit: int
var time = 0
var score = 0
var is_end = false
var is_stop = false

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	randomize()
	reset_game()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if time <= 0:
		if !is_end:
			is_end = true
			print("end")
			$se_time_up.play()
			verify_game()
		time = 0
	elif !is_stop:
		time -= delta
		$cart.set_label()
	$time.text = "Time: %0.2f s" % time
	$score.text = "Score: %d" % score


func verify_game() -> void:
	var correct = 0
	for i in range($cart.label.size()):
		if $cart.qty[i] == int(data.q_and_a["a"][$cart.q_and_a_id[i]]):
			$cart.set_correct(i)
			correct += 1
		else:
			$cart.set_wrong(i)
	if is_stop:
		if correct == $cart.qty.size():
			$Panel/Label.text = "You bought enough mask, you can stay away the covid."
		else:
			$Panel/Label.text = "You bought wrong amount of mask, you do not have enough mask to use."
	elif is_end:
		$Panel/Label.text = "You calculate too slow, no any mask can buy."
	$Panel.visible = true


func reset_game() -> void:
	time = time_limit
	score = 0
	is_end = false
	is_stop = false
	$Stock.set_mask_position()


func reset_cart() -> void:
	$cart.reset_qty()
	$cart.set_id()


# handle buy action
func _on_buy_pressed() -> void:
	if is_end || is_stop:
		return
	print("buy pressed")
	$se_cashier.play()
	is_stop = true
	verify_game()


# handle mask pressed
func mask_pressed(type: data.MaskType, isAdd: bool) -> void:
	if is_end || is_stop:
		return
	if isAdd:
		print(data.MaskName(type) + " add")
		$se_add.play()
	else:
		print(data.MaskName(type) + " minus")
		$se_minus.play()
	$cart.set_qty(type, isAdd)


# bind each mask button
func _on_stock_adult_lv_1_pressed() -> void:
	mask_pressed(data.MaskType.Adult_1, true)

func _on_stock_adult_lv_2_pressed() -> void:
	mask_pressed(data.MaskType.Adult_2, true)

func _on_stock_adult_lv_3_pressed() -> void:
	mask_pressed(data.MaskType.Adult_3, true)

func _on_stock_child_lv_1_pressed() -> void:
	mask_pressed(data.MaskType.Child_1, true)

func _on_stock_child_lv_2_pressed() -> void:
	mask_pressed(data.MaskType.Child_2, true)

func _on_stock_child_lv_3_pressed() -> void:
	mask_pressed(data.MaskType.Child_3, true)

func _on_cart_adult_lv_1_pressed() -> void:
	mask_pressed(data.MaskType.Adult_1, false)

func _on_cart_adult_lv_2_pressed() -> void:
	mask_pressed(data.MaskType.Adult_2, false)

func _on_cart_adult_lv_3_pressed() -> void:
	mask_pressed(data.MaskType.Adult_3, false)

func _on_cart_child_lv_1_pressed() -> void:
	mask_pressed(data.MaskType.Child_1, false)

func _on_cart_child_lv_2_pressed() -> void:
	mask_pressed(data.MaskType.Child_2, false)

func _on_cart_child_lv_3_pressed() -> void:
	mask_pressed(data.MaskType.Child_3, false)


func _on_button_2_pressed() -> void:
	print("exit")
	$se_btn_click.play()
	get_tree().change_scene_to_file("res://Scene/title.tscn")


func _on_button_pressed() -> void:
	print("retry")
	$se_btn_click.play()
	$Panel.visible = false
	reset_game()
	reset_cart()
