extends Node2D

@onready var positions = $Positions
@onready var collision = $CollisionTest

var lastDirection = null

var type = GLOBAL.BLOCKTYPES.LIGHT
var expand = false

func getPositions():
	return positions.get_children()

func checkDirection():
	print("xfsdfdsfd")
	var leftCollision = false
	var rightCollision = false

	movePattern(GLOBAL.Direction.RIGHT)
	print("0: ", await isColliding())
	if await isColliding():
		movePattern(GLOBAL.Direction.LEFT)
		print("1: ", await isColliding())
		#if await isColliding():
			#movePattern(GLOBAL.Direction.RIGHT, 2)
			#print("2: ", await isColliding())
			#if await isColliding():
				#movePattern(GLOBAL.Direction.LEFT, 2)
				#print("3: ", await isColliding())
				#if await isColliding():
					#resetPosition()
					#print("4: ", await isColliding())
	#if await isColliding():
		#rightCollision = true 
	#resetPosition()
	#movePattern(GLOBAL.Direction.LEFT)
	#if await isColliding():
		#leftCollision = true 
	#print(" LEFT: ", leftCollision, "\n RIGHT: ", rightCollision)
	#resetPosition()
	#if await getCollidingAreas() != []:
		#rightCollision = true
	#resetPosition()
	#movePattern(GLOBAL.Direction.LEFT)
	#if await getCollidingAreas() != []:
		#leftCollision = true
	#resetPosition()
	#
	#if leftCollision:
		#movePattern(GLOBAL.Direction.RIGHT)
	#elif rightCollision:
		#movePattern(GLOBAL.Direction.LEFT)
	
	
	#else: 
		
		#resetPosition()

func moveToPosition(newPosition):
	resetPosition()
	position = newPosition

func turn(direction, newPosition):
	reset(newPosition)
	#moveToPosition(newPosition)
	rotatePattern(direction)
	
	#if await isColliding():
		#print(1)
		#movePattern(GLOBAL.Direction.RIGHT, 1)
	#if await isColliding():
		#print(2)
		#movePattern(GLOBAL.Direction.LEFT,2)
	#if await isColliding():
		#print(3)
		#movePattern(GLOBAL.Direction.RIGHT, 3)
	#if await isColliding():
		#print(4)
		#movePattern(GLOBAL.Direction.LEFT, 4)
		
	#if await isColliding():
		#print(1)
		#movePattern(GLOBAL.Direction.RIGHT, 2)
	#if await isColliding():
		#print(2)
		#movePattern(GLOBAL.Direction.LEFT,4)
		#
	if await isColliding():
		movePattern(GLOBAL.Direction.RIGHT, 1)
	if await isColliding():
		movePattern(GLOBAL.Direction.RIGHT, 1)
	if await isColliding():
		movePattern(GLOBAL.Direction.LEFT,3)
	if await isColliding():
		movePattern(GLOBAL.Direction.LEFT,1)

	#if await isColliding():
		#print(2)
		#movePattern(GLOBAL.Direction.RIGHT, 1)
	#if await isColliding():
		#print(3)
		#movePattern(GLOBAL.Direction.LEFT,3)
	#if await isColliding():
		#print(4)
		#movePattern(GLOBAL.Direction.LEFT,1)
	#if await isColliding():
		#print(3)
		#movePattern(GLOBAL.Direction.LEFT, 1)
		#movePattern(GLOBAL.Direction.RIGHT, 3)
	#if await isColliding():
		#movePattern(GLOBAL.Direction.LEFT, 2)
	if await isColliding():
		resetPosition()



func getCollidingAreas():
	collision.force_update_transform()
	#collision.monitoring = false
	#collision.monitorable = false
	#await get_tree().process_frame
	#await get_tree().process_frame
	#collision.monitoring = true
	collision.monitorable = false
	await get_tree().process_frame
	collision.monitorable = true
	collision.monitorable = false
	await get_tree().process_frame
	collision.monitorable = true
	var collidingAreas = []
	for i in collision.get_overlapping_areas():
		if i.is_in_group("Block"):
			if i.UID != GLOBAL.currentUID:
				collidingAreas.append(i)
		else:
			collidingAreas.append(i)
	return collidingAreas
	
func isColliding():
	return await getCollidingAreas() != []

func getCollidingBorder():
	await get_tree().process_frame
	await get_tree().process_frame
	await get_tree().process_frame
	await get_tree().process_frame
	var collidingAreas = []
	for i in collision.get_overlapping_areas():
		if not i.is_in_group("Block"):
			collidingAreas.append(i)
	return collidingAreas

func getCollidingBlocks():
	await get_tree().process_frame
	await get_tree().process_frame
	await get_tree().process_frame
	var collidingAreas = []
	for i in collision.get_overlapping_areas():
		if i.is_in_group("Block"):
			if i.UID != GLOBAL.currentUID:
				collidingAreas.append(i)
	return collidingAreas

#func _on_collision_test_area_entered(area: Area2D) -> void:
	#if lastDirection == null:
		#checkLeft()
	#else:
		#if lastDirection == GLOBAL.Direction.LEFT:
			#print(getCollidingAreas())
			#checkRight()
		#if lastDirection == GLOBAL.Direction.LEFT:
			#print(getCollidingAreas())
	#print(collision.get_overlapping_areas())
	#print(getCollidingAreas())
	#print(checkLeft())
	#print(checkRight())

func reset(newPosition):
	position = newPosition
	positions.position.x = 0
	collision.position.x = 0
	rotation_degrees = 0

func movePattern(direction, steps = 1):
	match direction:
		GLOBAL.Direction.LEFT:
			positions.position.x -= GLOBAL.GRID * steps
			collision.position.x -= GLOBAL.GRID * steps
			#print("Left")
			#print(positions.position)
		GLOBAL.Direction.RIGHT:
			positions.position.x += GLOBAL.GRID * steps
			collision.position.x += GLOBAL.GRID * steps
			#print("Right")
			#print(positions.position)

func resetPosition():
	#print("RESET")
	positions.position.x = 0
	collision.position.x = 0
	
func rotatePattern(direction):
	match direction:
		GLOBAL.Direction.LEFT:
			positions.rotation_degrees += 90
			collision.rotation_degrees += 90
		GLOBAL.Direction.RIGHT:
			positions.rotation_degrees -= 90
			collision.rotation_degrees -= 90
