/**
 * @file shuffle command
 * @author Sankarsan Kampa (a.k.a k3rn31p4nic)
 * @license GPL-3.0
 */

exports.exec = (Bastion, message) => {
  if (!message.guild.music.enabled) {
    if (Bastion.user.id === '267035345537728512') {
      return Bastion.emit('error', '', Bastion.i18n.error(message.guild.language, 'musicDisabledPublic'), message.channel);
    }
    return Bastion.emit('error', '', Bastion.i18n.error(message.guild.language, 'musicDisabled'), message.channel);
  }

  if (message.guild.music.textChannelID && message.channel.id !== message.guild.music.textChannelID) return Bastion.log.info('Music channels have been set, so music commands will only work in the music text channel.');

  if (!message.guild.music.songs || !message.guild.music.songs.length) {
    /**
     * Error condition is encountered.
     * @fires error
     */
    return Bastion.emit('error', '', Bastion.i18n.error(message.guild.language, 'notPlaying'), message.channel);
  }

  let nowPlaying = message.guild.music.songs.shift();
  message.guild.music.songs = shuffle(message.guild.music.songs);
  message.guild.music.songs.unshift(nowPlaying);
  // message.guild.music.songs.shuffle();

  message.guild.music.textChannel.send({
    embed: {
      color: Bastion.colors.GREEN,
      description: 'Shuffled the queue.'
    }
  }).catch(e => {
    Bastion.log.error(e);
  });
};

exports.config = {
  aliases: [],
  enabled: true,
  musicMasterOnly: true
};

exports.help = {
  name: 'shuffle',
  botPermission: '',
  userTextPermission: '',
  userVoicePermission: '',
  usage: 'shuffle',
  example: []
};

/**
 * Shuffles an array.
 * @function shuffle
 * @param {array} array The array to shuffle.
 * @returns {array} The shuffled array.
 */
function shuffle(array) {
  let i = array.length;
  while (i) {
    let j = Math.floor(Math.random() * i);
    let t = array[--i];
    array[i] = array[j];
    array[j] = t;
  }
  return array;
}

@@ -529,18 +529,33 @@
 age.
-c
+guild.music.textC
 hannel
-.id
+ID
  !==
@@ -567,34 +567,25 @@
 age.
-guild.music.textChannelID)
+channel.id) %7B%0A   
  ret
@@ -680,20 +680,20 @@
 the 
-m
+M
 usic 
-t
+T
 ext 
-c
+C
 hann
@@ -698,16 +698,20 @@
 nnel.');
+%0A  %7D
 %0A%0A  if (
@@ -781,83 +781,8 @@
 ) %7B%0A
-    /**%0A     * Error condition is encountered.%0A     * @fires error%0A     */%0A
     
@@ -1067,50 +1067,8 @@
 ng);
-%0A  // message.guild.music.songs.shuffle();
 %0A%0A  

refactor shuffle command