extends Node2D


var cursors = [load("res://boot_icon.png"), load("res://eye_icon.png"), load("res://hand_icon.png"), load("res://speech_icon.png")]
var hourglass_cursor = load("res://hourglass_icon.png")
var previous_cursor_index: int = 0
var is_script_running: bool = false
var is_cursor_locked: bool = false  # When true, hourglass is shown and cursor can't be changed
func get_scene() -> Scene:
	return $SceneViewport/background

# Called when the node enters the scene tree for the first time.
func _ready():
#	get_scene().connect("transitioned", Callable($SceneViewport/label, "_on_transitioned"))
	get_scene().connect("transitioned", Callable(self, "_on_transitioned"))
	reset_camera(get_scene())
	ActionState.set_current_scene(get_scene())
	
	pass # Replace with function body.


func _on_transitioned(scene):
	reset_camera(scene)
	$SceneViewport/label._on_transitioned(scene)
	ActionState.set_current_scene(scene)
	camera.position = Vector2(0,0 )
	#scene.connect("transitioned", $SceneViwport/label, "_on_transitioned")
	scene.connect("transitioned", Callable(self, "_on_transitioned"))

@onready var camera:Camera2D = $SceneViewport/PlayerTracker/Camera2D	
	
func reset_camera(scene):
	#$Node2D.scale = Vector2(scene.background_scale, scene.background_scale)
	#var far_point = self.to_global(Vector2(scene.find_child("bg").texture.get_width(), scene.find_child("bg").texture.get_height() ) )
	var x:Sprite2D = scene.find_child("bg");
	var height = x.texture.get_height()
	var far_point = self.to_global(Vector2(x.texture.get_width(), x.texture.get_height() )  * x.get_global_transform())
	camera.make_current()
	camera.limit_right = far_point.x
	camera.limit_bottom=far_point.y
	get_scene().get_node("Graham/RemoteTransform2D").remote_path=$SceneViewport/PlayerTracker.get_path()
	camera.reset_smoothing()
	camera.limit_right = far_point.x 
	camera.limit_bottom = far_point.y 
	
	#$Node2D/background/Graham/Camera2D.reset_smoothing()
	
	#$Node2D/background/Graham/Camera2D.reset_smoothing()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var player = get_scene().get_node("Graham")
	$SceneViewport/PlayerTracker/Camera2D.set_target(player.facing)
	var s = get_scene().ego_scale(player)
	player.scale = Vector2(s, s)
	
	# Update cursor if script state changed
	var script = $GameScript.current_script
	if script and not is_script_running:
		set_script_cursor()
	elif not script and is_script_running:
		restore_cursor()

func set_script_cursor() -> void:
	previous_cursor_index = ActionState.current_action
	is_script_running = true
	is_cursor_locked = true  # Lock cursor to hourglass
	Input.set_custom_mouse_cursor(hourglass_cursor)

func restore_cursor() -> void:
	is_script_running = false
	is_cursor_locked = false  # Unlock cursor
	Input.set_custom_mouse_cursor(cursors[previous_cursor_index])
	
func _unhandled_input(event: InputEvent) -> void:
	$SceneViewport.push_input(event)
	
func _on_backpack_show_overlay() -> void:
	$InventoryOverlayLayer/InventoryOverlay.show_overlay()

func _on_backpack_hide_overlay() -> void:
	$InventoryOverlayLayer/InventoryOverlay.hide_overlay()

func _input(event):
	if event.is_action_released("quit"):
		get_tree().quit()
	if event.is_action_released("right_click") and not is_cursor_locked:
		var prev_action = ActionState.current_action
		ActionState.current_action = (ActionState.current_action + 1) % 5
		if ActionState.current_action == ActionState.Action.ITEM:
			if InventoryManager.selected_item:
				Input.set_custom_mouse_cursor(cursors[ActionState.current_action], Input.CursorShape.CURSOR_ARROW, Vector2(0,0))
			else:
				ActionState.current_action = (ActionState.current_action + 1) % 5
				Input.set_custom_mouse_cursor(cursors[ActionState.current_action], Input.CursorShape.CURSOR_ARROW, Vector2(0,0))
		else:
			if prev_action == ActionState.Action.ITEM:
				InventoryManager.clear_selection()
			Input.set_custom_mouse_cursor(cursors[ActionState.current_action], Input.CursorShape.CURSOR_ARROW, Vector2(0,0))
