@tool
extends Node3D
class_name Dashboard

const TOPOLOGY_SPEED = 5000.

signal main_screen_activated
signal right_screen_activated
signal left_screen_activated

@export var run_data : RunData
@export var in_space : bool

@export var main_screen_state : DashboardMainScreen.State : set = set_main_screen_state
@export var main_screen_run_point : RunPoint : set = set_main_screen_run_point

@export var status_text : String : set = set_status_text

@export var destination_text : String = "" : set = set_destination_text

@export var left_stop : RunPoint = null : set = set_left_stop
@export var right_stop : RunPoint = null : set = set_right_stop

func _ready():
    %MainScreen.clicked.connect(func(): main_screen_activated.emit())
    %RightScreen.clicked.connect(func(): right_screen_activated.emit())
    %LeftScreen.clicked.connect(func(): left_screen_activated.emit())

    set_main_screen_state()
    set_main_screen_run_point()
    set_status_text()
    set_destination_text()
    set_left_stop()
    set_right_stop()

    if not Engine.is_editor_hint() and GameInfo.game_data.player_data:
        %Inventory3D.update(GameInfo.game_data.player_data.inventory)
        %EnergyInfo3d.energy = GameInfo.game_data.player_data.energy
        %EnergyInfo3d.max_energy = GameInfo.game_data.player_data.max_energy

func update_travel_screen(with_animation = true, recreate_map = false):
    if is_node_ready():
        var current_position = run_data.level + (0.5 if in_space else 0.)

        %TravelScreenContent.story_step = run_data.story_step
        %TravelScreenContent.run_data = run_data
        %TravelScreenContent.current_position = current_position

        %TravelScreenContent.update(with_animation, recreate_map)

        %TopologyContent.texture.noise.seed = run_data.run_seed

        if with_animation:
            var topology_tween = create_tween()
            topology_tween.set_trans(Tween.TRANS_EXPO)
            topology_tween.tween_property(%TopologyContent.texture.noise,"offset:y",-current_position*TOPOLOGY_SPEED, 5.)
        else:
            %TopologyContent.texture.noise.offset.y = -current_position*TOPOLOGY_SPEED


func set_main_screen_state(s := main_screen_state):
    main_screen_state = s
    if is_node_ready():
        %MainScreen.state = s

func set_main_screen_run_point(rp := main_screen_run_point):
    main_screen_run_point = rp
    if is_node_ready():
        %MainScreen.run_point = rp

func set_status_text(v := status_text):
    status_text = v
    if is_node_ready():
        %StatusLabel.text = v

func set_destination_text(v := destination_text):
    destination_text = v
    if is_node_ready():
        %DestinationScreen.visible = v != ""
        %DestinationLabel.text = v

func set_left_stop(v := left_stop):
    left_stop = v
    if is_node_ready():
        %LeftScreen.run_point = left_stop

func set_right_stop(v := right_stop):
    right_stop = v
    if is_node_ready():
        %RightScreen.run_point = right_stop
        
