/* eslint-disable no-continue */
/* eslint-disable default-case */
import { Message } from 'eris';
import { Client, Command, RichEmbed } from '../class';
import { getTotalMessageCount } from '../intervals/score';

import Inquiry_Remove from './inquiry_rm';

export default class Inquiry extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'inquiry';
    this.description = 'Retrieves information about a hard inquiry that was performed on a member.';
    this.usage = `${this.client.config.prefix}inquiry <reportID>`;
    this.aliases = ['inq', 'pulldata'];
    this.permissions = 5;
    this.guildOnly = true;
    this.enabled = true;
    this.subcmds = [Inquiry_Remove];
  }

  public async run(message: Message, args: string[]) {
    try {
      if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);

      const inquiry = await this.client.db.Inquiry.findOne({ iid: args[0] });
      if (!inquiry) return this.error(message.channel, 'Could not locate Inquiry information.');
      const currentReport = await this.client.db.Score.findOne({ userID: inquiry.userID });
      if (!currentReport) return this.error(message.channel, 'Could not find Community Report for this user.');
      const member = this.client.util.resolveMember(inquiry.userID, this.mainGuild);
      if (!member) return this.error(message.channel, 'Could not locate member.');
      const { report } = inquiry;
      // if (!report) return this.error(message.channel, 'Could not find inquiry information.');

      await this.client.report.createInquiry(member.id, 'Library of Code sp-us | Bureau of Community Reports', 1);

      const embed = new RichEmbed();
      embed.setTitle(`Hard Inquiry Information - ${inquiry.iid}`);
      embed.setAuthor(member.username, member.user.avatarURL);
      let currentScore = '0';
      if (currentReport.total < 200) currentScore = '---';
      else if (currentReport.total > 800) currentScore = '800';
      else currentScore = `${currentReport.total}`;
      embed.setDescription(`**Current Community Score:** ${currentScore}\n\n**Department/Service:** ${inquiry.name || 'N/A'}\n**Reason:** ${inquiry.reason || 'N/A'}`);

      let totalScore = '0';
      let activityScore = '0';
      let moderationScore = '0';
      let roleScore = '0';
      let cloudServicesScore = '0';
      let otherScore = '0';
      let miscScore = '0';

      if (report.total < 200) totalScore = '---';
      else if (report.total > 800) totalScore = '800';
      else totalScore = `${report.total}`;

      if (report.activity < 10) activityScore = '---';
      else if (report.activity > Math.floor((Math.log1p(getTotalMessageCount(this.client)) * 12))) activityScore = String(Math.floor((Math.log1p(getTotalMessageCount(this.client)) * 12)));
      else activityScore = `${report.activity}`;

      if (report.roles <= 0) roleScore = '---';
      else if (report.roles > 54) roleScore = '54';
      else roleScore = `${report.roles}`;

      moderationScore = `${report.moderation}`;

      if (report.other === 0) otherScore = '---';
      else otherScore = `${report.other}`;

      if (report.staff <= 0) miscScore = '---';
      else miscScore = `${report.staff}`;

      if (report.cloudServices === 0) cloudServicesScore = '---';
      else if (report.cloudServices > 10) cloudServicesScore = '10';
      else cloudServicesScore = `${report.cloudServices}`;

      let color = '🔴';
      let additionalText = 'POOR';
      embed.setColor('FF0000');
      if (report.total >= 550) { color = '🟠'; additionalText = 'FAIR'; embed.setColor('FFA500'); }
      if (report.total >= 630) { color = '🟡'; additionalText = 'GOOD'; embed.setColor('FFFF00'); }
      if (report.total >= 700) { color = '🟢'; additionalText = 'EXCELLENT'; embed.setColor('66FF66'); }
      if (report.total >= 770) { color = '✨'; additionalText = 'EXCEPTIONAL'; embed.setColor('#99FFFF'); }
      embed.addField('Total | 200 to 800', report ? `${color} ${totalScore} | ${additionalText}` : 'N/C', true);
      embed.addField(`Activity | 10 to ${Math.floor(Math.log1p(getTotalMessageCount(this.client)) * 12)}`, activityScore || 'N/C', true);
      embed.addField('Roles | 1 to N/A', roleScore || 'N/C', true);
      embed.addField('Moderation | N/A to 2' || 'N/C', moderationScore, true);
      embed.addField('Cloud Services | N/A to 10+', cloudServicesScore || 'N/C', true);
      embed.addField('Other', otherScore || 'N/C', true);
      embed.addField('Misc', miscScore || 'N/C', true);
      if (currentReport.pin?.length > 0) {
        embed.addField('PIN', currentReport.pin.join('-'), true);
      }
      embed.setTimestamp();
      embed.setFooter('Inquiry performed on', this.client.user.avatarURL);
      return message.channel.createMessage({ embed });
    } catch (err) {
      return this.client.util.handleError(err, message, this);
    }
  }
}
