/**
 * @file wow command
 * @author James Naylor (a.k.a euronay)
 * @license GPL-3.0
 */

const request = xrequire('request-promise-native');

const wowConstants = {
  faction: [
    'Alliance',
    'Horde'
  ],
  gender: [
    'None',
    'Male',
    'Female'
  ],
  race: [
    'None',
    'Human',
    'Orc',
    'Dwarf',
    'Night Elf',
    'Undead',
    'Tauren',
    'Gnome',
    'Troll',
    'Goblin',
    'Blood Elf',
    'Draenei',
    'Fel Orc',
    'Naga',
    'Broken',
    'Skeleton',
    'Vrykul',
    'Tuskarr',
    'Forest Troll',
    'Taunka',
    'Northrend Skeleton',
    'Ice Troll',
    'Worgen',
    'Gilnean',
    'Pandaren',
    'Pandaren',
    'Pandaren',
    'Nightborn',
    'Highmountain Tauren',
    'Void Elf',
    'Lightforged Draenei',
    'Zandalari Troll',
    'Kul Tiran',
    'Human',
    'Dark Iron Dwarf',
    'Vulpera',
    'Mag\'har Orc'
  ],
  class: [
    'None',
    'Warrior',
    'Paladin',
    'Hunter',
    'Rogue',
    'Priest',
    'Death Knight',
    'Shaman',
    'Mage',
    'Warlock',
    'Druid',
    'Demon Hunter'
  ]
};

exports.exec = async (Bastion, message, args) => {
  try {
    if (!args.character || !args.realm) {
      /**
       * The command was ran with invalid parameters.
       * @fires commandUsage
       */
      return Bastion.emit('commandUsage', message, this.help);
    }

    let locale = args.region == 'us' ? 'en-us' : 'en-gb';

    let requestUrl = `https://${args.region}.api.battle.net/wow/character/${args.realm}/${args.character}?fields=guild,items&locale=${locale}&apikey=${Bastion.credentials.battleNetApiKey}`;

    options = {
      url: requestUrl,
      json: true
    };
    response = await request(options);

    let factionIcon = `https://worldofwarcraft.akamaized.net/static/components/Logo/Logo-${response.faction == 0 ? 'alliance' : 'horde'}.png` + "";

    let avatar = `https://render-${args.region}.worldofwarcraft.com/character/${response.thumbnail}` + "";

    let stats = [{
      name: 'Character',
      value: `${response.level} ${wowConstants.race[response.race]} ${wowConstants.class[response.class]}`
    },
    {
      name: 'Achievement Points',
      value: response.achievementPoints
    },
    {
      name: 'Item Level',
      value: response.items.averageItemLevel
    }];

    if (response.guild) {
      stats.push(
        {
          name: 'Guild',
          value: response.guild.name
        }
      )
    }

    message.channel.send({
      embed: {
        author: {
          name: response.name,
          url: `https://worldofwarcraft.com/${locale}/character/${args.realm}/${args.character}`,
          icon_url: factionIcon
        },
        fields: stats,
        thumbnail: {
          url: avatar
        }
      }
    }).catch(e => {
      Bastion.log.error(e);
    });

  }
  catch (e) {
    if (e.name === 'StatusCodeError') {
      if (e.statusCode == '404') {
        return Bastion.emit('error', '', Bastion.i18n.error(message.guild.language, 'notFound', 'player'), message.channel);
      }
      return Bastion.emit('error', e.statusCode, e.error.message, message.channel);
    }
    Bastion.log.error(e);
  }
};

exports.config = {
  aliases: [],
  enabled: true,
  argsDefinitions: [
    { name: 'character', type: String, defaultOption: true },
    { name: 'realm', type: String, alias: 'r' },
    { name: 'region', type: String, alias: 'e', defaultValue: 'us' },
  ]
};

exports.help = {
  name: 'wow',
  description: 'Get stats of any World of Warcraft character from the Armory.',
  botPermission: '',
  userTextPermission: '',
  userVoicePermission: '',
  usage: 'wow <character> <realm> [-r <region>]',
  example: ['wow Fallken -r Burning-Blade -e eu ']
};

@@ -1377,16 +1377,17 @@
 egion ==
+=
  'us' ? 
@@ -1601,16 +1601,20 @@
 %60;%0A%0A    
+let 
 options 
@@ -1664,20 +1664,25 @@
 %0A    %7D;%0A
+%0A
     
+let 
 response
@@ -1819,16 +1819,17 @@
 ction ==
+=
  0 ? 'al
@@ -1851,21 +1851,16 @@
 e'%7D.png%60
- + %22%22
 ;%0A%0A    l
@@ -1958,13 +1958,8 @@
 il%7D%60
- + %22%22
 ;%0A%0A 
@@ -1974,18 +1974,27 @@
 tats = %5B
-%7B
 %0A
+      %7B%0A  
       na
@@ -2012,24 +2012,26 @@
 ter',%0A      
+  
 value: %60$%7Bre
@@ -2119,33 +2119,39 @@
 lass%5D%7D%60%0A    
+  
 %7D,%0A    
+ 
+ 
 %7B%0A
+  
       name: 
@@ -2170,32 +2170,34 @@
  Points',%0A      
+  
 value: response.
@@ -2222,17 +2222,23 @@
     
+  
 %7D,%0A    
+ 
+ 
 %7B%0A
+  
     
@@ -2255,24 +2255,26 @@
 tem Level',%0A
+  
       value:
@@ -2310,17 +2310,24 @@
 vel%0A    
-%7D
+  %7D%0A    
 %5D;%0A%0A    
@@ -2455,16 +2455,17 @@
 %0A      )
+;
 %0A    %7D%0A%0A
@@ -2920,16 +2920,17 @@
 sCode ==
+=
  '404') 
@@ -3439,17 +3439,16 @@
 : 'us' %7D
-,
 %0A  %5D%0A%7D;%0A
@@ -3696,16 +3696,21 @@
 ample: %5B
+%0A    
 'wow Fal
@@ -3738,13 +3738,16 @@
  -e eu '
+%0A  
 %5D%0A%7D;%0A

fix lint issues