class_name WrappingManager extends Node

@export var dupFlags: int = 15

# Mirrored sprites for periodic boundary
var mirrors: Array

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	await owner.ready
	
	# add mirrors
	for _i in range(3):
		var dup = owner.collision.duplicate(dupFlags)
		mirrors.append(dup)
		owner.call_deferred("add_child", dup)
		#for i in owner.get_groups():
		#	if not str(i).begins_with("_"):
		#		m.add_to_group(i)
		if dup.has_method("duplicate_init"):
			dup.duplicate_init()
	
	_handle_wrapping()

func play_sprite(anim: String) -> void:
	for m in mirrors:
		m.get_node("Sprite").play(anim)

func _process(delta: float) -> void:
	_handle_wrapping()
	
	
# Mirroring table:
# |---|---|---|---|
# | 4 | 3 | 4 | 3 |
# |---|===|===|---|
# | 1 ǁ 2 | 1 ǁ 2 |
# |---ǁ---|---ǁ---|
# | 4 ǁ 3 | 4 ǁ 3 |
# |---|===|===|---|
# | 1 | 2 | 1 | 2 |
# |---|---|---|---|
# If less than viewport size away from an edge, mirror over that edge (for seamless boundary)
# NOTE: For this to look correctly the camera size should be smaller than half the screen port (in
# 		any one dimension. Ideally, the difference between camera size and half the screen port is 
# 		at least the size of the prey sprite)
func _handle_wrapping():	
	for m in mirrors:
		m.global_rotation = owner.collision.global_rotation

	# TODO: Assume viewport size << screen size and only draw according to GameManager.viewport_size
	# Find corresponding section of the screen
	if owner.collision.position.x < GameManager.screen_size.x/2 and owner.collision.position.y < GameManager.screen_size.y/2:
		# Right
		mirrors[0].global_position = owner.collision.global_position + Vector2(GameManager.screen_size.x, 0)
		# Diag
		mirrors[2].global_position = owner.collision.global_position + Vector2(GameManager.screen_size.x, GameManager.screen_size.y)
		# Bottom
		mirrors[1].global_position = owner.collision.global_position + Vector2(0, GameManager.screen_size.y)

		
	elif owner.collision.position.x < GameManager.screen_size.x/2:
		# Top
		mirrors[0].global_position = owner.collision.global_position +  Vector2(0, - GameManager.screen_size.y)
		# Diag
		mirrors[1].global_position = owner.collision.global_position +  Vector2(GameManager.screen_size.x, - GameManager.screen_size.y)
		# Right
		mirrors[2].global_position = owner.collision.global_position +  Vector2(GameManager.screen_size.x, 0)
		
		
	elif owner.collision.position.y < GameManager.screen_size.y/2:
		# Left 
		mirrors[0].global_position = owner.collision.global_position +  Vector2(- GameManager.screen_size.x, 0)
		# Bottom
		mirrors[1].global_position = owner.collision.global_position +  Vector2(0, GameManager.screen_size.y)
		# Diag
		mirrors[2].global_position = owner.collision.global_position +  Vector2(- GameManager.screen_size.x, GameManager.screen_size.y)

	else:
		# Left
		mirrors[0].global_position = owner.collision.global_position +  Vector2(- GameManager.screen_size.x, 0)
		# Diag
		mirrors[1].global_position = owner.collision.global_position +  Vector2(- GameManager.screen_size.x, - GameManager.screen_size.y)
		# Top
		mirrors[2].global_position = owner.collision.global_position +  Vector2(0,  - GameManager.screen_size.y)
