package com.skcraft.plume.util.concurrent;

import com.google.common.util.concurrent.*;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.skcraft.plume.util.Messages;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;

import java.util.concurrent.*;

import static com.skcraft.plume.common.util.SharedLocale.tr;

@Singleton
@Log
public class BackgroundExecutor {

    private static final long MESSAGE_DELAY = 500;

    private final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Plume Background Executor #%d").setDaemon(true).build();
    @Getter
    private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(threadFactory));
    @Inject
    private TickExecutorService tickExecutorService;
    private final ScheduledExecutorService timer = new ScheduledThreadPoolExecutor(1);

    public void notifyOnDelay(ListenableFuture<?> future, ICommandSender sender) {
        ChatComponentText message = new ChatComponentText(tr("task.pleaseWaitProcessing"));
        message.getChatStyle().setColor(EnumChatFormatting.GRAY);
        Future<?> messageFuture = timer.schedule(new MessageTask(sender, message), MESSAGE_DELAY, TimeUnit.MILLISECONDS);
        future.addListener(() -> messageFuture.cancel(false), MoreExecutors.newDirectExecutorService());
    }

    public void addCallbacks(ListenableFuture<?> future, ICommandSender sender) {
        notifyOnDelay(future, sender);

        Futures.addCallback(future, new FutureCallback<Object>() {
            @Override
            public void onSuccess(Object result) {
            }

            @Override
            public void onFailure(Throwable t) {
                sender.addChatMessage(Messages.exception(t));
            }
        });
    }

    @RequiredArgsConstructor
    private class MessageTask implements Runnable {
        private final ICommandSender sender;
        private final ChatComponentText message;

        @Override
        public void run() {
            tickExecutorService.execute(() -> sender.addChatMessage(message));
        }
    }

}

@@ -78,17 +78,32 @@
 current.
-*
+ListenableFuture
 ;%0Aimport
@@ -110,24 +110,210 @@
  com.google.
+common.util.concurrent.ListeningExecutorService;%0Aimport com.google.common.util.concurrent.MoreExecutors;%0Aimport com.google.common.util.concurrent.ThreadFactoryBuilder;%0Aimport com.google.
 inject.Injec
@@ -353,48 +353,8 @@
 eton
-;%0Aimport com.skcraft.plume.util.Messages
 ;%0Aim
@@ -1706,451 +1706,8 @@
  %7D%0A%0A
-    public void addCallbacks(ListenableFuture%3C?%3E future, ICommandSender sender) %7B%0A        notifyOnDelay(future, sender);%0A%0A        Futures.addCallback(future, new FutureCallback%3CObject%3E() %7B%0A            @Override%0A            public void onSuccess(Object result) %7B%0A            %7D%0A%0A            @Override%0A            public void onFailure(Throwable t) %7B%0A                sender.addChatMessage(Messages.exception(t));%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     

Remove legacy BackgroundExecutor.addCallbacks().