/* eslint-disable max-len */
bot.cloops = [];
function add(command, interval, bot) {
  const id = setInterval(() => bot.core.run(command), interval);

  bot.cloops.push({id, interval, command});
}
function remove(item) {
  clearInterval(bot.cloops[item].id);

  bot.cloops.splice(item, 1);
}
function clear() {
  for (const interval of bot.cloops) clearInterval(interval.id);

  bot.cloops = [];
}
function list(bot) {
  const message = [];

  message.push({text: 'Cloops:', color: 'green'});
  message.push('\n');

  for (const [id, cloop] of Object.entries(bot.cloops)) {
    message.push({text: id, color: 'aqua'});
    message.push({text: ' > ', color: 'gold'});
    message.push({text: cloop.command, color: 'green'});
    message.push({text: ' - ', color: 'gold'});
    message.push({text: cloop.interval, color: 'green'});
    message.push('\n');
  }

  message.pop();

  bot.core.run('minecraft:tellraw @a ' + JSON.stringify(message));
}
module.exports = {
  name: 'cloop',
  alias: [],
  description: 'Loop commands',
  usage: '<hash> <add|remove|removeall|list> <interval|index> <command>',
  trusted: 1,
  execute: function(bot, username, usernameraw, sender, prefix, args) {
    if (args[1]==='add') {
      if (args[0]===bot.hash) {
        if (typeof args[3]!=='undefined') {
          // bot.cloopnumbers++;
          // bot.clooparr.push(args.slice(3).join(' '));
          // eval(`bot.command${bot.cloopnumbers} = args.slice(3).join(' ')`);
          // eval(`bot.interval${bot.cloopnumbers} = args[2]`);
          // eval(`bot.cloop${bot.cloopnumbers} = setInterval(() => { bot.core.run(bot.command${bot.cloopnumbers}) }, bot.interval${bot.cloopnumbers})`);
          // eval(`bot.core.run('minecraft:tellraw @a ["",{"text":"Added command ","color":"white"},{"text":"' + bot.command${bot.cloopnumbers} + '","color":"aqua"},{"text":" with interval ","color":"white"},{"text":"' + bot.interval${bot.cloopnumbers} + '","color":"green"},{"text":" to the cloops","color":"white"}]')`);
          add(args.slice(3).join(' '), args[2], bot);
          bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Added command ', color: 'white'}, {text: `${args.slice(3).join(' ')}`, color: 'aqua'}, {text: ' with interval ', color: 'white'}, {text: `${args[2]}`, color: 'green'}, {text: ' to the cloops', color: 'white'}]));
        }
      } else {
        throw new Error('Invalid hash');
      }
      return;
    }
    if (args[1]==='list') {
      if (args[0]===bot.hash) {
        // bot.core.run('minecraft:tellraw @a ' + JSON.stringify(['', {text: 'Cloops:'}]));
        // bot.clooparr.forEach((item, index) => {
        //   bot.core.run('minecraft:tellraw @a ' + JSON.stringify(['', {text: `${index}`, color: 'aqua'}, {text: ' > ', color: 'gold'}, {text: `${item}`, color: 'green'}]));
        // });
        list(bot);
      } else {
        throw new Error('Invalid hash');
      }
      return;
    }
    if (args[1]==='remove') {
      if (args[0]===bot.hash) {
        // eval(`if (typeof bot.cloop${Number(args[2])}==='undefined'){ throw new Error('Invalid index') }`);
        // bot.clooparr.shift(Number(args[2]));
        // eval(`clearInterval(bot.cloop${Number(args[2])})`);
        // bot.cloopnumbers - 1;
        remove(args[2]);
        bot.core.run('minecraft:tellraw @a ' + JSON.stringify(['', {text: 'Removed cloop '}, {text: args[2], color: 'aqua'}]));
      } else {
        throw new Error('Invalid hash');
      }
      return;
    }
    if (args[1]==='removeall') {
      if (args[0]===bot.hash) {
        // for (let i = 0; i <= bot.cloopnumbers; i++) {
        //   bot.clooparr.shift(i);
        //   eval(`clearInterval(bot.cloop${i})`);
        // }
        // bot.cloopnumbers = -1;
        clear();
        bot.core.run('minecraft:tellraw @a ' + JSON.stringify(['', {text: 'Removed all looped commands', color: 'white'}]));
      } else {
        throw new Error('Invalid hash');
      }
      return;
    }
  },
};