extends Camera2D
signal camera_moved

var SPEED = 4000
var DRAG_SPEED = 4.0
var ZOOM_SPEED = 12.0

var BASE_MAX_ZOOM = 1.0
var BASE_MIN_ZOOM = 0.15
var max_zoom = 1.0
var min_zoom = 0.15
var previous_mouse_position = Vector2(-1, -1)
@onready var previous_position: Vector2 = get_screen_center_position()
@onready var current_zoom = get_zoom().x

func _ready():
    update_sound_players()
    update_zoom_values()
    get_tree().get_root().get_child(0).connect("item_rect_changed", update_zoom_values)

func _input(event: InputEvent):
    if event is InputEventMouseMotion and Input.is_action_pressed("screen_drag"):
#        if previous_mouse_position == Vector2(-1, -1):
#            previous_mouse_position = get_viewport().get_mouse_position()
#        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
        #set_screen_center_position(center - event.relative * DRAG_SPEED)
        var pos = get_target_position() - event.relative * DRAG_SPEED
        set_position(pos)
        fix_borders()
        #move_local_x(-event.relative.x * DRAG_SPEED)
        #move_local_y(-event.relative.y * DRAG_SPEED)
#    elif previous_mouse_position != Vector2(-1, -1):
#        Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
#        get_viewport().warp_mouse(previous_mouse_position)
#        previous_mouse_position = Vector2(-1, -1)

func _process(delta):
    #print(get_screen_center_position(), " ", get_position(), " ", get_target_position())
    var pos = get_tree().get_root().get_mouse_position()
    if pos.x < 20:
        #move_local_x(delta * -SPEED)
        pass
    
    if Input.is_action_just_released("zoom_in"):
        current_zoom *= (1 + delta * ZOOM_SPEED)
        current_zoom = min(max_zoom, current_zoom)
        set_zoom(Vector2(current_zoom, current_zoom))
        update_sound_players()
        fix_borders()
    if Input.is_action_just_released("zoom_out"):
        current_zoom *= (1 - delta * ZOOM_SPEED)
        current_zoom = max(min_zoom, current_zoom)
        set_zoom(Vector2(current_zoom, current_zoom))
        update_sound_players()
        fix_borders()

    var new_position = get_screen_center_position()
    if previous_position != new_position:
        emit_signal("camera_moved")
    previous_position = get_screen_center_position()
    
func update_sound_players():
    var players = get_tree().get_nodes_in_group("sound_player")
    var dist = Vector2(DisplayServer.window_get_size()) / zoom
    for sound_player in players:
        sound_player.set_max_distance(dist.x)

func update_zoom_values():
    var oversize = DisplayServer.window_get_size().x / 1920.0
    if oversize > 1.0:
        min_zoom = BASE_MIN_ZOOM * oversize
        if current_zoom < min_zoom:
            current_zoom = min_zoom
            set_zoom(Vector2(current_zoom, current_zoom))
    else:
        min_zoom = BASE_MIN_ZOOM

# Fix the camera lock being 🌠🌠 fucking stupid 🌠🌠
func fix_borders():
    var pos = get_position()
    var rect = Vector2(DisplayServer.window_get_size()) / zoom
    pos.x = max(pos.x, limit_left + rect.x / 2.0)
    pos.x = min(pos.x, limit_right - rect.x / 2.0)
    pos.y = max(pos.y, limit_top + rect.y / 2.0)
    pos.y = min(pos.y, limit_bottom - rect.y / 2.0)
    set_position(pos)
