using System;
using Godot;
using static ld48.Extensions;

public class Message : Node2D {
    private float intensity = 1f;

    private ColorRect color;
    private Label label;

    public String message;
    public bool simple;

    private float initialDelay;
    public MessageArea receiver;

    public override void _Ready() {
        base._Ready();

        var canvas = GetNode("Canvas");
        color = canvas.GetNode<ColorRect>("ColorRect");
        if (simple) color.QueueFree();
    }

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

        intensity -= delta*0.25f;
        color.Color = C(intensity).Alpha(simple ? 0f : intensity);
        
        if (intensity <= 0f) {
            receiver?.Next();
            QueueFree();
        }
        
        Update();
    }

    public override void _Draw() {
        base._Draw();

        var size = Game.alert.GetStringSize(message);
        DrawString(Game.alert, GetViewportRect().Size/2f+GetViewportRect().Position - size/2f + V2(0f, Game.horror.GetAscent()), message, C((1f-intensity*intensity).Clamp(0f, 1f)).Alpha(intensity));
    }
}

