extends Camera2D

var horizontal_facing = 0
var vertical_facing = 0
var camera_horizontal_shift = 90
var camera_vertical_shift = 0
var time: float = 0
var tween

@onready var original_x_zoom = zoom.x
@onready var original_y_zoom = zoom.y
@onready var blobby = get_node("%Blobby")
@onready var prev_camera_pos

# Gets the camera limits from the tilemap of the level
# Requires "TileMap" to be a sibling of blobby
func _ready():
	_set_boundaries()
	self.position = blobby.global_position
	prev_camera_pos = get_screen_center_position()
	offset.x = -camera_horizontal_shift if zoom.x == original_x_zoom else 0
	tween = get_tree().create_tween()

func _physics_process(delta: float) -> void:
	if(!GlobalState.is_dead):
		time += delta
		_adapt_to_movement()
	#TODO Do this via a event or let it be to track blobbies movement better
	else:
		self.position = blobby.global_position
		_death_cam()

func _set_boundaries():
	# This is ok, because it only happens on initialization
	# But it is also quite fickle
	var tilemap = get_node("../TileMap")
	# TODO: This goes wrong when overwriting old tiles with new sprites
	# New pngs -> completely new tiles and rebuild map
	var rect = tilemap.get_used_rect()
	var cell_size = tilemap.cell_size
	limit_right = rect.end.x * cell_size.x - camera_horizontal_shift
	limit_left = rect.position.x * cell_size.x + camera_horizontal_shift
	# TODO: When vertical scrolling is fixed
	limit_top = rect.position.y * cell_size.y #+ camera_vertical_shift
	limit_bottom = rect.end.y * cell_size.y #- camera_vertical_shift
	var screen_size = get_viewport_rect()
	var h_pixels = limit_right - limit_left
	var v_pixels = limit_bottom - limit_top
	# TODO: Fix that it can zoom both?
	if screen_size.end.x * original_x_zoom - h_pixels > 0:
		zoom.x = h_pixels / screen_size.end.x
		zoom.y = zoom.x
	if screen_size.end.y * original_y_zoom - v_pixels > 0:
		zoom.y = v_pixels / screen_size.end.y
		zoom.x = zoom.y

# TODO Smoothing the camera limits in godot ruins this still?
func _adapt_to_movement():
	# TODO Adapt this to movement speed
	var target_offset: Vector2 = Vector2(0,0)
	var tween_v = false
	var tween_h = false
	# TODO Make smarter
	if(time > 0.1):
		time = 0.0
		var cam_offset = get_screen_center_position() - prev_camera_pos
		var new_h_facing = sign(cam_offset.x)
		if new_h_facing != 0 && horizontal_facing != new_h_facing:
			horizontal_facing = new_h_facing
			target_offset.x = camera_horizontal_shift * horizontal_facing
			tween_h = true
		
		# var new_v_facing = sign(cam_offset.y)
		# if new_v_facing != 0 && vertical_facing != new_v_facing:
		# 	vertical_facing = new_v_facing
		# 	target_offset.x = offset.x if !new_h_facing else target_offset.x
		# 	target_offset.y = camera_vertical_shift * vertical_facing
		# 	tween_v = true

		prev_camera_pos = get_screen_center_position()
	if ((tween_h || tween_v )):	
			#TODO Motion may be too complex
		tween.interpolate_property(
			self,
			"offset",
			offset,
			target_offset,
			1.2,
			Tween.TRANS_SINE,
			Tween.EASE_OUT
		)
		tween.start()
	position = blobby.position

func _death_cam():
	$AnimationPlayer.play("deathCam")
	#TODO Animation not always centered
