package net.dirtydeeds.discordsoundboard.async;

import net.dirtydeeds.discordsoundboard.audio.AudioScheduler;
import net.dirtydeeds.discordsoundboard.audio.AudioTrackScheduler;
import net.dirtydeeds.discordsoundboard.beans.SoundFile;
import net.dirtydeeds.discordsoundboard.org.Category;
import net.dirtydeeds.discordsoundboard.service.SoundboardBot;
import net.dirtydeeds.discordsoundboard.service.SoundboardDispatcher;
import net.dirtydeeds.discordsoundboard.utils.MessageBuilder;
import net.dirtydeeds.discordsoundboard.utils.StyledEmbedMessage;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.requests.RestAction;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class PlaySoundsStaggeredJob implements SoundboardJob {

  private Random rand = new Random();
  private String[] sounds;
  private SoundboardBot bot;
  private User user;
  private String category;
  private Date timestamp;

  public PlaySoundsStaggeredJob(String[] sounds, SoundboardBot bot, User user) {
    this.sounds = sounds;
    this.bot = bot;
    this.user = user;
    this.timestamp = someTimeFromNow();
  }

  public PlaySoundsStaggeredJob(String[] sounds, SoundboardBot bot, User user,
                                String category) {
    this(sounds, bot, user);
    this.category = category;
    this.timestamp = someTimeFromNow();
  }

  private Date someTimeFromNow() {
    return new Date(System.currentTimeMillis() + 1000 + rand.nextInt(600000));
  }

  public boolean isApplicable(SoundboardDispatcher dispatcher) {
    if (timestamp != null) {
      Date now = new Date(System.currentTimeMillis());
      return now.after(timestamp);
    }
    return true;
  }

  private void tryAgain(SoundboardDispatcher dispatcher) {
    dispatcher.getAsyncService().runJob(new PlaySoundsStaggeredJob(sounds, bot, user));
  }

  private void next(SoundboardDispatcher dispatcher) {
    dispatcher.getAsyncService().runJob(new PlaySoundsStaggeredJob(Arrays.copyOfRange(sounds, 1, sounds.length), bot, user));
  }

  private void schedule(AudioTrackScheduler scheduler, String name) throws
          InterruptedException, ExecutionException, TimeoutException {
    if (name == null) return;
    SoundFile f = bot.getSoundMap().get(name);
    f.addOneToNumberOfPlays();
    bot.getDispatcher().saveSound(f);
    scheduler.load(f.getSoundFile().getPath(),
            new AudioScheduler(scheduler)).get(5, TimeUnit.SECONDS);
  }

  private VoiceChannel getVoiceChannel() {
    VoiceChannel voice = null;
    try {
      voice = bot.getUsersVoiceChannel(user);
      if (voice == null) {
        bot.sendMessageToUser(SoundboardBot.NOT_IN_VOICE_CHANNEL_MESSAGE, user);
        return null;
      }
      bot.moveToChannel(voice);
    } catch (Exception e) {
      return voice;
    }
    return voice;
  }

  public void run(SoundboardDispatcher dispatcher) {
    if (bot == null || sounds == null || sounds.length == 0) return;
    VoiceChannel voice = getVoiceChannel();
    if (voice == null) tryAgain(dispatcher);
    Guild guild = voice.getGuild();

    AudioTrackScheduler scheduler = bot.getSchedulerForGuild(guild);
    String sound = sounds[0];
    if (sound == null || sound.equals("*")) {
      if (category == null) try {
        schedule(scheduler, bot.getRandomSoundName());
        next(dispatcher);
      } catch (Exception e) {
        next(dispatcher);
      }
      else try {
        schedule(scheduler, bot.getRandomSoundNameForCategory(category));
        next(dispatcher);
      } catch (Exception e) {
        next(dispatcher);
      }
    } else {
      try {
        schedule(scheduler, sound);
        next(dispatcher);
      } catch (Exception e) {
        next(dispatcher);
      }
    }
  }

}

@@ -1295,40 +1295,8 @@
 ser,
-%0A                               
  Str
@@ -2643,25 +2643,25 @@
  (voice 
-=
+!
 = null) 
 %7B%0A      
@@ -2648,28 +2648,78 @@
 ce != null) 
+bot.moveToChannel(voice);%0A    %7D catch (Exception e) 
 %7B%0A
-  
       bot.se
@@ -2738,166 +2738,144 @@
 ser(
+%22
 So
-undboardBot.NOT_IN_VOICE_CHANNEL_MESSAGE, user);%0A        return null;
+mething went wrong when I tried to play a sound for you%22 +
 %0A      
-%7D%0A
+  
       
-bot.moveToChannel(voice);%0A    %7D catch (Exception e) %7B%0A      return voice
+((sounds%5B0%5D != null) ? %22 %60%22 + sounds%5B0%5D + %22%60.%22 : %22.%22), user)
 ;%0A  
@@ -3014,16 +3014,22 @@
 th == 0)
+%0A     
  return;
@@ -3025,24 +3025,25 @@
     return;%0A
+%0A
     VoiceCha

Fix stagger job to not bug the user anymore