import { Message } from 'eris';
import { Client, Command, RichEmbed } from '../class';

export default class DelItem extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'delitem';
    this.description = 'Removes information to your whois embed.';
    this.usage = 'delitem [code]';
    this.permissions = 0;
    this.enabled = true;
  }

  public async run(message: Message, args: string[]) {
    try {
      if (args.length < 1) {
        const embed = new RichEmbed();
        embed.setTitle('Whois Data Codes');
        embed.addField('Languages', '**Assembly Language:** lang-asm\n**C/C++:** lang-cfam\n**C#:** lang-csharp\n**Go:** lang-go\n**Java:** lang-java\n**JavaScript:** lang-js\n**Kotlin:** lang-kt\n**Python:** lang-py\n**Ruby:** lang-rb\n**Rust:** lang-rs\n**Swift:** lang-swift\n**TypeScript:** lang-ts');
        embed.addField('Operating Systems', '**Arch:** os-arch\n**Debian:** os-deb\n**CentOS:** os-cent\n**Fedora:** os-fedora\n**macOS:** os-mdarwin\n**Manjaro:** os-manjaro\n**RedHat:** os-redhat\n**Ubuntu:** os-ubuntu\n**Windows:** os-win');
        embed.setFooter(this.client.user.username, this.client.user.avatarURL);
        embed.setTimestamp();
        return message.channel.createMessage({ embed });
      }
      if (args[0].split('-')[0] === 'os' && ['arch', 'deb', 'cent', 'fedora', 'manjaro', 'mdarwin', 'redhat', 'ubuntu', 'win'].includes(args[0].split('-')[1])) {
        const account = await this.client.db.Member.findOne({ userID: message.member.id });
        if (account?.additional.operatingSystems.length < 1) {
          return message.channel.createMessage(`***${this.client.util.emojis.ERROR} You don't have any operating systems to remove.***`);
        }
        await account.updateOne({ $pull: { 'additional.operatingSystems': args[0].split('-')[1] } });
        return message.channel.createMessage(`***${this.client.util.emojis.SUCCESS} Removed OS code ${args[0]} from profile.***`);
      }
      if (args[0].split('-')[0] === 'lang' && ['js', 'py', 'rb', 'ts', 'rs', 'go', 'cfam', 'csharp', 'swift', 'java', 'kt', 'asm'].includes(args[0].split('-')[1])) {
        const account = await this.client.db.Member.findOne({ userID: message.member.id });
        if (account?.additional.langs.length < 1) {
          return message.channel.createMessage(`***${this.client.util.emojis.ERROR} You don't have any languages to remove.***`);
        }
        await account.updateOne({ $pull: { 'additional.langs': args[0].split('-')[1] } });
        return message.channel.createMessage(`***${this.client.util.emojis.SUCCESS} Removed language code ${args[0]} from profile.***`);
      }
      return message.channel.createMessage(`***${this.client.util.emojis.ERROR} Invalid data code.***`);
    } catch (err) {
      return this.client.util.handleError(err, message, this);
    }
  }
}
