using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
using static ld48.Extensions;

public class MessageArea : Node2D {
    [Export] private String message;
    [Export] private bool flash;
    [Export] private AudioStreamSample sample;
    
    [Export] private String[] messagesAfter;

    private List<String> nextMessages;

    private AudioStreamPlayer2D player;

    private bool seen;

    private PackedScene lastMessageScene;

    private static List<AudioStreamSample> sounds;
    private int soundI;

    private bool goToCredits;
    private float creditsDelay = 5f;

    public override void _Ready() {
        base._Ready();
        
        lastMessageScene = ResourceLoader.Load<PackedScene>($"res://scene/{nameof(SilentMessage)}.tscn");

        if (sounds == null) {
            sounds = new List<AudioStreamSample> {
                ResourceLoader.Load<AudioStreamSample>($"res://resources/dialogue1.wav"),
                ResourceLoader.Load<AudioStreamSample>($"res://resources/dialogue2.wav"),
                ResourceLoader.Load<AudioStreamSample>($"res://resources/dialogue3.wav"),
                ResourceLoader.Load<AudioStreamSample>($"res://resources/dialogue4.wav"),
                ResourceLoader.Load<AudioStreamSample>($"res://resources/dialogue4.wav"),
                ResourceLoader.Load<AudioStreamSample>($"res://resources/dialogue5.wav"),
                ResourceLoader.Load<AudioStreamSample>($"res://resources/dialogue6.wav"),
            };
        }

        player = GetNode<AudioStreamPlayer2D>("Player");
        player.Stream = sample;
        if (messagesAfter != null) {
            nextMessages = messagesAfter.ToList();
        } else {
            nextMessages = new List<string>();
        }
    }

    public override void _Process(float delta) {
        base._Process(delta);

        if (goToCredits) {
            if (creditsDelay > 0f) {
                creditsDelay -= delta;
                if (creditsDelay <= 0f) {
                    var message = lastMessageScene.Instance<SilentMessage>();
                    message.message = "Thanks for playing.";
                    GetTree().Root.GetNode("Game").GetNode("UI").AddChild(message);
                }
            }
        }
    }

    public void Entered(Node body) {
        if (body is Hero hero) {
            if (!seen && Hero.hp > 0f) {
                seen = true;
                Hero.instance.Toast(message, flash, this);
                player.Play();
            }
        }
    }
    
    public void Exited(Node body) {
        if (body is Hero hero) {
            
        }
    }

    public void Next() {
        if (nextMessages.Any()) {
            Hero.instance.falling = true;
            Hero.instance.Toast(nextMessages.First(), flash, this);
            Hero.instance.player.Stream = sounds[soundI];
            Hero.instance.currentRope.Retract();
            soundI += 1;
            soundI %= sounds.Count;
            Hero.instance.player.PitchScale -= 0.03f;
            Hero.instance.player.VolumeDb -= 0.5f;
            nextMessages.RemoveAt(0);
        } else if (messagesAfter != null && messagesAfter.Any()) {
            // Hero.instance.done = true;

            goToCredits = true;
        }
    }
}

