@tool
extends Node3D

enum State {ARRIVAL,COMMS_UP,LATER}

const DIALOG_BOREA_ARRIVAL = "res://dialogs/timelines/1_waking_up/9_borea_arrival.dtl"
const DIALOG_COMMS_UP = "res://dialogs/timelines/1_waking_up/91_borea_comms_up.dtl"
const DIALOG_BOREA_LATER = "res://dialogs/timelines/1_waking_up/92_borea_later.dtl"

@export var communication_lever_animation_player : AnimationPlayer

@export var show_plants : bool = false : set = set_show_plants

var state := State.ARRIVAL : set = set_state

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

func set_show_plants(v : bool = show_plants):
    show_plants = v
    if is_node_ready():
        %Plant.visible = show_plants
        %FallingPlant.visible = show_plants

func set_state(v := state):
    state = v
    if is_node_ready():
        %AiReceptacle.screen_turned_on = true
        %AiReceptacle.interactable = true

func _on_ai_receptacle_clicked():
    SteamConnection.unlock_achievement(SteamConnection.ACH_MEET_DEMETER)

    %AiReceptacle.screen_turned_on = false
    %AiReceptacle.interactable = false

    match state:
        State.ARRIVAL:
            Dialogic.start(DIALOG_BOREA_ARRIVAL)
            %CommunicationLever.interactable = true
        State.COMMS_UP:
            Dialogic.start(DIALOG_COMMS_UP)
            
            await Dialogic.timeline_ended
            await get_tree().create_timer(3.).timeout

            %Player3D.controlling_player = false
            %PassDayAnimationPlayer.play("PassDays")
            
            await %PassDayAnimationPlayer.animation_finished
            
            %Player3D.controlling_player = true

            state = State.LATER
        State.LATER:
            Dialogic.start(DIALOG_BOREA_LATER)

            await Dialogic.timeline_ended

            %Exit3dDoor.to_scene = RelayBaseScene.new(
                "BOREA_BASE",
                "004",
            )
            %Exit3dDoor.interactable = true

            AudioManager.play_sfx("DoorOpen")

func _on_communication_lever_clicked():
    %CommunicationLever.interactable = false
    %CommunicationLever.play_audio()
    communication_lever_animation_player.play("Activate")

    state = State.COMMS_UP
