package eu.mhsl.minenet.minigames.message;

import net.kyori.adventure.audience.Audience;
import net.minestom.server.MinecraftServer;
import net.minestom.server.timer.TaskSchedule;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

public class Countdown {
    public Countdown(Class<? extends TranslatableMessage> messageType) {
    }
    public CompletableFuture<Void> countdown(Audience targets, int from, Consumer<CountdownModifier> modifier) {
        CompletableFuture<Void> future = new CompletableFuture<>();
        AtomicInteger count = new AtomicInteger(from);
        MinecraftServer.getSchedulerManager().submitTask(() -> {
            if(count.get() <= 0) {
                future.complete(null);
                return TaskSchedule.stop();
            }
            try {
                CountdownModifier countdownModifier = new CountdownModifier(count.getAndDecrement());
                modifier.accept(countdownModifier);
                countdownModifier.message.send(targets);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return TaskSchedule.seconds(1);
        });
        return future;
    }


    public static class CountdownModifier {
        public TranslatableMessage message;
        public final int timeLeft;
        public CountdownModifier(int timeLeft) {
            this.timeLeft = timeLeft;
        }
    }
}


