class_name WrappingManager extends Node

@export var sprite: AnimatedSprite2D 

# Mirrored sprites for periodic boundary
var mirrorSprite1: Node2D
var mirrorSprite2: Node2D
var mirrorSprite3: Node2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	await owner.ready
	mirrorSprite1 = sprite.duplicate()
	mirrorSprite2 = sprite.duplicate()
	mirrorSprite3 = sprite.duplicate()

	owner.add_child(mirrorSprite1)
	owner.add_child(mirrorSprite2)
	owner.add_child(mirrorSprite3)
	
	_handle_wrapping()

func play_sprite(anim: String) -> void:
	mirrorSprite1.play(anim)
	mirrorSprite2.play(anim)
	mirrorSprite3.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():
	# TODO: Assume viewport size << screen size and only draw according to GameManager.viewport_size
	# Find corresponding section of the screen
	if owner.position.x < GameManager.screen_size.x/2 and owner.position.y < GameManager.screen_size.y/2:
		# Right
		mirrorSprite1.global_position = owner.global_position + Vector2(GameManager.screen_size.x, 0)
		# Diag
		mirrorSprite3.global_position = owner.global_position + Vector2(GameManager.screen_size.x, GameManager.screen_size.y)
		# Bottom
		mirrorSprite2.global_position = owner.global_position + Vector2(0, GameManager.screen_size.y)

		
	elif owner.position.x < GameManager.screen_size.x/2:
		# Top
		mirrorSprite1.global_position = owner.global_position +  Vector2(0, - GameManager.screen_size.y)
		# Diag
		mirrorSprite2.global_position = owner.global_position +  Vector2(GameManager.screen_size.x, - GameManager.screen_size.y)
		# Right
		mirrorSprite3.global_position = owner.global_position +  Vector2(GameManager.screen_size.x, 0)
		
		
	elif owner.position.y < GameManager.screen_size.y/2:
		# Left 
		mirrorSprite1.global_position = owner.global_position +  Vector2(- GameManager.screen_size.x, 0)
		# Bottom
		mirrorSprite2.global_position = owner.global_position +  Vector2(0, GameManager.screen_size.y)
		# Diag
		mirrorSprite3.global_position = owner.global_position +  Vector2(- GameManager.screen_size.x, GameManager.screen_size.y)

	else:
		# Left
		mirrorSprite1.global_position = owner.global_position +  Vector2(- GameManager.screen_size.x, 0)
		# Diag
		mirrorSprite2.global_position = owner.global_position +  Vector2(- GameManager.screen_size.x, - GameManager.screen_size.y)
		# Top
		mirrorSprite3.global_position = owner.global_position +  Vector2(0,  - GameManager.screen_size.y)
