extends Area3D

#needed to work as "tile"
@export var template_name = ""
var current_rotation = 0


enum SCREWTYPE {YELLOW, BLUE, RED}
@export var screw_type: SCREWTYPE = SCREWTYPE.YELLOW

var detected_tool = null
var is_removed = false

static var removed_count := 0

static var screws_amount := 0
static var screws_remaining := 0

signal removed
signal all_screws_removed



# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	
	if detected_tool != null and overlaps_body(detected_tool) and is_removed == false and detected_tool.is_driven:
		if (detected_tool.grid_distance_x < 0.35 and detected_tool.grid_distance_z < 0.35):
			emit_signal("removed")
			
			removed_count += 1
			screws_remaining = screws_amount - removed_count
			print (screws_remaining)
			print (screws_amount)
			print (removed_count)
			is_removed = true
			detected_tool.wait(0.18)
			call_deferred("queue_free")
			
	if removed_count == screws_amount and screws_amount > 0:
		emit_signal("all_screws_removed")
	


func _on_body_entered(body: Node3D) -> void:
	if body.is_in_group("tool"):
		if body.tool_type == screw_type and body.is_driven:
			detected_tool = body
			#if (body.grid_distance_x < 0.1 and body.grid_distance_z < 0.1):
	
#needed to work as "tile"		
func get_dict():
	var element_rotation = self.get_rotation_degrees()

	return {
		"name" : self.template_name,
		"rotation" : 0,
	}

static func reset_screws():
	removed_count = 0
	screws_amount = 0
	screws_remaining = 0
