package eu.mhsl.minenet.minigames.message.type;

import eu.mhsl.minenet.minigames.message.TranslatableMessage;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import net.kyori.adventure.title.TitlePart;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

import java.time.Duration;
import java.util.function.Consumer;

public class TitleMessage extends TranslatableMessage {
    private Title.Times times;
    private SubtitleMessage subtitle = new SubtitleMessage();

    public TitleMessage() {
        times = Title.Times.times(Duration.ZERO, Duration.ofSeconds(1), Duration.ZERO);
    }
    public TitleMessage(Duration stay) {
        times = Title.Times.times(Duration.ZERO, stay, Duration.ZERO);
    }
    public TitleMessage(Duration stay, Duration fade) {
        times = Title.Times.times(fade, stay, fade);
    }

    public void setTimes(Title.Times times) {
        this.times = times;
    }

    public TranslatableMessage subtitle(Consumer<SubtitleMessage> callback) {
        this.subtitle = new SubtitleMessage();
        callback.accept(subtitle);
        return this;
    }

    @Override
    public void send(Player p) {
        Audience.audience(p).showTitle(new Title() {
            @Override
            public @NotNull Component title() {
                return build(p);
            }

            @Override
            public @NotNull Component subtitle() {
                return subtitle.build(p);
            }

            @Override
            public @Nullable Times times() {
                return times;
            }

            @Override
            public <T> @UnknownNullability T part(@NotNull TitlePart<T> part) {
                return null;
            }
        });
    }
}
