extends Node

const FULL_SCREEN_MODES: Array[DisplayServer.WindowMode] = [
	DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN,
	DisplayServer.WINDOW_MODE_FULLSCREEN,
]

var visible_vision: bool = false

var dragging_disabled: bool = false
var dragged_object: Node2D = null
var drop_spot: Node2D = null
var original_z: int = 0

func is_dragging() -> bool:
	return dragged_object != null

func start_dragging(object: Node2D):
	assert(object != null)
	stop_dragging()
	
	if dragging_disabled:
		return
	
	dragged_object = object
	original_z = dragged_object.z_index
	dragged_object.z_index = 1000

func stop_dragging():
	if dragged_object != null:
		if drop_spot != null:
			drop_spot.drop(dragged_object)
		dragged_object.position = Vector2.ZERO
		dragged_object.rotation = 0.0
		dragged_object.z_index = original_z
		dragged_object = null

func set_drop_spot(spot: Node2D):
	assert(spot == null || spot.has_method("drop"))
	drop_spot = spot
	
func toggle_fullscreen():
	var current_mode = DisplayServer.window_get_mode()
	if current_mode in FULL_SCREEN_MODES:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	else:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)

func _input(event: InputEvent):
	if event.is_action_pressed("toggle_fullscreen"):
		toggle_fullscreen()
	elif event.is_action_pressed("toddle_vision"):
		visible_vision = !visible_vision
	elif event is InputEventMouseMotion && is_dragging():
		dragged_object.global_position = event.global_position
