/* eslint-disable no-var */
/* eslint-disable max-len */
const {MessageEmbed} = require('discord.js');
module.exports = {
  name: 'help',
  alias: ['heko', 'cmds', 'commands'],
  description: 'Shows the help',
  usage: '[command]',
  trusted: 0,
  execute: function(bot, username, usernameraw, sender, prefix, args) {
    const generalCommands = [];
    const trustedCommands = [];
    const ownerCommands = [];
    function component(command, color) {
      return {
        text: command.name + ' ',
        color,
        hoverEvent: {
          action: 'show_text',
          contents: [{
            text: 'Click here to see the information for this command',
            color: 'green',
          }],
        },
        clickEvent: {
          action: 'run_command',
          value: `${prefix}help ${command.name}`,
        },
      };
    };
    for (const command of bot.command_handler.commands) {
      if (command.trusted !== 0) continue;
      generalCommands.push(component(command, 'green'));
    }
    for (const command of bot.command_handler.commands) {
      if (command.trusted !== 1) continue;
      trustedCommands.push(component(command, 'red'));
    }
    for (const command of bot.command_handler.commands) {
      if (command.trusted !== 2) continue;
      ownerCommands.push(component(command, 'dark_red'));
    }
    if (typeof args[0] !== 'undefined') {
      for (const command of bot.command_handler.commands) {
        function run() {
          let discordSupported;
          let alias = command.name;
          if (typeof command.discordExecute === 'undefined') {
            discordSupported = 'false';
          } else {
            discordSupported = 'true';
          }
          if (command.alias.toString() !== '') {
            alias = command.alias.join(', ');
          }
          bot.tellraw('@a', [{text: prefix + command.name, color: 'gold'}, {text: ` (${alias})`, color: 'white'}, {text: ' - ', color: 'gray'}, {text: command.description, color: 'gray'}]);
          bot.tellraw('@a', [{text: 'Trust level: ', color: 'green'}, {text: command.trusted, color: 'yellow'}]);
          bot.tellraw('@a', [{text: 'Supported on Discord: ', color: 'green'}, {text: discordSupported, color: 'gold'}]);
          bot.tellraw('@a', [{text: prefix + command.name, color: 'gold'}, {text: ' ' + command.usage, color: 'aqua'}]);
        }

        if (command.name === args[0]) run();
        for (const alias of command.alias) {
          if (alias === args[0]) run();
        }
      };
    } else {
      const pre = [{text: 'Commands ', color: 'gray'}, {text: '(', color: 'dark_gray'}, {text: 'Length: ', color: 'gray'}, {text: bot.command_handler.commands.length, color: 'green'}, {text: ') ', color: 'dark_gray'}, {text: '(', color: 'dark_gray'}, {text: '⬤ Public', color: 'green'}, {text: ' ⬤ Trusted', color: 'red'}, {text: ' ⬤ Owner', color: 'dark_red'}, {text: ') - ', color: 'dark_gray'}];
      bot.tellraw('@a', [pre, generalCommands, trustedCommands, ownerCommands]);
    }
  },
  discordExecute: async function(bot, username, usernameraw, sender, prefix, args, channeldc) {
    if (typeof args[0] !== 'undefined') {
      for (const command of bot.command_handler.commands) {
        function run() {
          let discordSupported;
          let alias = command.name;
          if (typeof command.discordExecute === 'undefined') {
            discordSupported = 'false';
          } else {
            discordSupported = 'true';
          }
          if (command.alias.toString() !== '') {
            alias = command.alias.join(', ');
          }
          const Embed = new MessageEmbed()
              .setColor('#FFFF00')
              .setTitle(`${prefix + command.name} (${alias}) - ${command.description}`)
              .setDescription(`Trust level: ${command.trusted}` + '\n' +
                              `Supported: ${discordSupported}` + '\n' +
                              `${prefix + command.name} ${command.usage}`,
              );
          channeldc.send({embeds: [Embed]});
        }

        if (command.name === args[0]) run();
        for (const alias of command.alias) {
          if (alias === args[0]) run();
        }
      };
    } else {
      let supportedCommands = '';
      let unsupportedCommands = '';
      for (const command of bot.command_handler.commands) {
        if (typeof command.discordExecute === 'undefined') continue;
        supportedCommands += command.name + ' ';
      }
      for (const command of bot.command_handler.commands) {
        if (typeof command.discordExecute !== 'undefined') continue;
        unsupportedCommands += command.name + ' ';
      }
      const Embed = new MessageEmbed()
          .setColor('#FFFF00')
          .setTitle(`Commands (Length: ${bot.command_handler.commands.length})`)
          .setDescription('**Supported Commands**\n' + supportedCommands + '\n**Unsupported Commands**\n' + unsupportedCommands);
      channeldc.send({embeds: [Embed]});
    }
  },
};