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

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

  public async run(ctx: CmdContext) {
    if (ctx.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 ctx.uniCreateMessage({ embed });
    }
    if (ctx.args[0].split('-')[0] === 'os' && ['arch', 'deb', 'cent', 'fedora', 'manjaro', 'mdarwin', 'redhat', 'ubuntu', 'win'].includes(ctx.args[0].split('-')[1])) {
      const account = await this.client.db.mongo.Member.findOne({ userID: ctx.message.member.id });
      if (!account) {
        const newAccount = new this.client.db.mongo.Member({
          userID: ctx.message.member.id,
          additional: {
            operatingSystems: [ctx.args[0].split('-')[1]],
          },
        });
        await newAccount.save();
      } else {
        await account.updateOne({ $addToSet: { 'additional.operatingSystems': ctx.args[0].split('-')[1] } });
      }
      return ctx.uniCreateMessage(`***${this.client.util.emojis.SUCCESS} Added OS code ${ctx.args[0]} to profile.***`);
    }
    if (ctx.args[0].split('-')[0] === 'lang' && ['js', 'py', 'rb', 'ts', 'rs', 'go', 'cfam', 'csharp', 'swift', 'java', 'kt', 'asm'].includes(ctx.args[0].split('-')[1])) {
      const account = await this.client.db.mongo.Member.findOne({ userID: ctx.message.member.id });
      if (!account) {
        const newAccount = new this.client.db.mongo.Member({
          userID: ctx.message.member.id,
          additional: {
            langs: [ctx.args[0].split('-')[1]],
          },
        });
        await newAccount.save();
      } else {
        await account.updateOne({ $addToSet: { 'additional.langs': ctx.args[0].split('-')[1] } });
      }
      return ctx.uniCreateMessage(`***${this.client.util.emojis.SUCCESS} Added language code ${ctx.args[0]} to profile.***`);
    }
    return ctx.uniCreateMessage(`***${this.client.util.emojis.ERROR} Invalid data code.***`);
  }
}
