@tool
extends Node3D
class_name RelayBase

signal orchid_saved

@export var base_name : String = "" : set = set_base_name
@export var base_number : String = "" : set = set_base_number
@onready var orchid_saver : OrchidSaver = %OrchidSaver
@onready var player : Player3D = %Player3D
var exit_scene : Scene = null : 
    set(s):
        exit_scene = s
        if is_node_ready():
            %Exit.interactable = exit_scene != null
var dead_orchid : Node3D


func _ready():
    if not Engine.is_editor_hint():
        Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

    %Exit.interactable = exit_scene != null

    set_base_name()
    set_base_number()

func set_base_name(bn := base_name):
    base_name = bn
    if is_node_ready():
        %BaseNameLabel.text = base_name

func set_base_number(bn := base_number):
    base_number = bn
    if is_node_ready():
        %BaseNumberLabel.text = base_number
        %Sun.rotation.y = hash(base_number) % 360

func get_incubators() -> Array[Incubator]:
    var incubators = [] as Array[Incubator]

    for c in %Incubators.get_children():
        if c is Incubator:
            incubators.append(c)
    
    return incubators

func _on_exit_clicked():
    %Exit.interactable = false
    if dead_orchid:
        GameInfo.game_data.dead_orchid_position = dead_orchid.position
        GameInfo.game_data.dead_orchid_rotation = dead_orchid.rotation

    if exit_scene:
        SceneManager.change_to_scene(exit_scene)

func _on_orchid_saver_clicked():
    orchid_saved.emit()
    %OrchidSaver.interactable = false
