import PhoneNumber from 'awesome-phonenumber';
import axios from 'axios';
import { Message, TextChannel } from 'eris';
import { Client, Command, RichEmbed } from '../class';

export default class Callback extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'callback';
    this.description = 'Requests a Callback from a Technican.\nPlease use `-` to separate the number if needed. E.x. 202-750-2585.\nDo note, we are unable to dial international numbers outside of the US and Canada.\n\n*We recommend you run this command in your DMs for privacy.*';
    this.usage = 'callback <the number you want us to call>';
    this.aliases = ['cb'];
    this.permissions = 0;
    this.guildOnly = false;
    this.enabled = true;
  }

  public async run(message: Message, args: string[]) {
    try {
      if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
      if (message.channel.type === 0) await message.delete();
      const member = this.mainGuild.members.get(message.author.id);
      if (!member) return this.error(message.channel, 'Unable to fetch member.');
      const phone = new PhoneNumber(args.join(' '), 'US');
      if (!phone.isValid()) return this.error(message.channel, 'The number you have entered is invalid.');
      const embed = new RichEmbed();
      embed.setTitle('Callback Request');
      embed.setDescription('Please dial `9` first to reach an the external trunk. For example, to dial 202-750-2585 you would dial 92027502585 on your device.\n\n*Please react with <:modSuccess:578750988907970567> on this message if you are taking the call.*');
      embed.addField('Member', `${member.user.username}#${member.user.discriminator} | <@${member.user.id}>`, true);
      embed.addField('Phone Number', phone.getNumber('national'), true);
      embed.addField('Phone Number Type', phone.getType(), true);
      const communityReport = await this.client.db.Score.findOne({ userID: message.author.id }).lean().exec();
      if (communityReport) {
        await this.client.report.createInquiry(member.user.id, 'Library of Code sp-us | VOIP/PBX Member Support SVCS', 1);
        embed.addField('PIN', `${communityReport.pin[0]}-${communityReport.pin[1]}-${communityReport.pin[2]}`, true);
      }
      try {
        const d = await axios.get(`https://api.cloud.libraryofcode.org/wh/info/?id=${message.author.id}&authorization=${this.client.config.internalKey}`);
        embed.addField('Email Address', d.data.emailAddress, true);
        embed.addField('Support Key', d.data.supportKey, true);
      } catch {
        this.client.util.signale.warn('No CS Account found for user.');
      }
      const chan = <TextChannel> this.mainGuild.channels.get('780513128240382002');
      const msg = await chan.createMessage({ content: '<@&780519428873781298>', embed });
      await msg.addReaction('modSuccess:578750988907970567');
      return message.channel.createMessage('__**Callback Request**__\nYour callback request has been sent to our agents, you should receive a callback within 1-24 hours from the date of this request. The number you will be called from is listed below.\n\n**Callback Number:** +1 202-750-2585\n**Callback Region:** Washington, D.C., United States\n\n\n*Your number is never stored on our systems at any time, as soon as your call is taken by an agent your number is deleted from notification channels.*');
    } catch (err) {
      return this.client.util.handleError(err, message, this);
    }
  }
}
