extends Node

@onready var camera2D: Camera2D = $Camera2D


var transitioning: bool = false

func _ready() -> void:
	#camera2D.current = false
	
	pass

func switch_camera(from, to) -> void:
	#from.current = false
	to.make_current()
func get_effective_position(center_pos: Vector2):
	var viewport_rect = get_viewport().get_visible_rect()
	var zoom = camera2D.zoom
	var half_viewport_width = viewport_rect.size.x * 0.5 * zoom.x
	var half_viewport_height = viewport_rect.size.y * 0.5 * zoom.y

	var min_x = camera2D.limit_left + half_viewport_width
	var max_x = camera2D.limit_right - half_viewport_width
	var min_y = camera2D.limit_top + half_viewport_height
	var max_y = camera2D.limit_bottom - half_viewport_height
	var clamped_x = clamp(float(center_pos.x), min_x, max_x)
	var clamped_y = clamp(float(center_pos.y), min_y, max_y)

	var effective_pos = Vector2(clamped_x, clamped_y)
	return effective_pos


func transition_camera2D(from: Camera2D, to: Camera2D, duration: float = 1.0) -> void:
	if transitioning: return
	# Copy the parameters of the first camera
	camera2D.custom_viewport = get_tree().root.get_node("Node2D/SceneViewport")
	camera2D.zoom = from.zoom
	camera2D.offset = from.offset
	camera2D.light_mask = from.light_mask
	camera2D.set_limit(SIDE_BOTTOM, from.limit_bottom)
	camera2D.set_limit(SIDE_RIGHT, from.limit_right)
	camera2D.set_limit(SIDE_LEFT, from.limit_left)
	camera2D.set_limit(SIDE_TOP, from.limit_top)
	camera2D.limit_left = from.limit_left
	camera2D.limit_right = from.limit_right
	camera2D.limit_top = from.limit_top
	camera2D.limit_bottom = from.limit_bottom
	to.limit_left = from.limit_left
	to.limit_right = from.limit_right
	to.limit_top = from.limit_top
	to.limit_bottom = from.limit_bottom
	#camera2D.
	# Move our transition camera to the first camera position
	var gt = from.get_screen_center_position()
	
	camera2D.global_transform.origin = gt
	#camera2D.global_transform.y = gt.y
	#camera2D.set_screen
	#camera2D.get_screen_center_position()
	
	# Make our transition camera current
	#camera2D.enabled=true
	#to.enabled=true
	camera2D.make_current()
	
	camera2D.reset_smoothing()
	camera2D.align()
	transitioning = true
	var tween = get_tree().create_tween()
	tween.set_parallel(true)
	tween.set_trans(Tween.TRANS_CUBIC)
	tween.set_ease(Tween.EASE_IN_OUT)
	var dest = Transform2D(to.global_transform) 
	get_effective_position(dest.origin)
	
	print("TOOOO")
	print(to)
	to.get_screen_center_position()
	var gz = to.get_screen_center_position()
	var ox = to.get_target_position()
	var gl = to.global_transform
	print()

	tween.tween_property(camera2D, "global_transform", dest, 2.0).from_current()

	tween.tween_property(camera2D, "offset", to.offset, 2.0).from_current()
	tween.tween_property(camera2D, "zoom", to.zoom, 2.0).from_current()
	#tween.tween_property(camera2D, "zoom", to.zoom, 3.0)

	tween.play()
	
	# Wait for the tween to complete
	await tween.finished
	
	# Make the second camera current
	to.make_current()
	transitioning = false
	#from.enabled=false 
	#camera2D.enabled=false
