const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { RIOT_KEY } = process.env;
const buttons = ['Q', 'W', 'E', 'R'];

module.exports = class LeagueOfLegendsChampionCommand extends Command {
	constructor(client) {
		super(client, {
			name: 'league-of-legends-champion',
			aliases: ['lol-champion', 'league-of-legends-champ', 'lol-champ'],
			group: 'search',
			memberName: 'league-of-legends-champion',
			description: 'Responds with information on a League of Legends champion.',
			clientPermissions: ['EMBED_LINKS'],
			args: [
				{
					key: 'champion',
					prompt: 'What champion would you like to get information on?',
					type: 'string',
					parse: champion => champion.toLowerCase()
				}
			]
		});

		this.version = null;
		this.champions = null;
	}

	async run(msg, { champion }) {
		if (champion === 'satan') champion = 'teemo';
		try {
			if (!this.version) await this.fetchVersion();
			const data = await this.fetchChampion(champion);
			if (!data) return msg.say('Could not find any results.');
			const tips = [].concat(data.allytips, data.enemytips);
			const embed = new MessageEmbed()
				.setColor(0x002366)
				.setAuthor('League of Legends', 'https://i.imgur.com/2JL4Rko.png', 'https://leagueoflegends.com/')
				.setTitle(`${data.name} ${data.title}`)
				.setDescription(data.blurb)
				.setThumbnail(`https://ddragon.leagueoflegends.com/cdn/${this.version}/img/champion/${data.image.full}`)
				.addField('❯ Attack',
					data.info.attack, true)
				.addField('❯ Defense',
					data.info.defense, true)
				.addField('❯ Magic',
					data.info.magic, true)
				.addField('❯ Difficulty',
					data.info.difficulty, true)
				.addField('❯ HP',
					`${data.stats.hp} (${data.stats.hpperlevel}/level)`, true)
				.addField('❯ HP Regen',
					`${data.stats.hpregen} (${data.stats.hpregenperlevel}/level)`, true)
				.addField('❯ MP',
					`${data.stats.mp} (${data.stats.mpperlevel}/level)`, true)
				.addField('❯ MP Regen',
					`${data.stats.mpregen} (${data.stats.mpregenperlevel}/level)`, true)
				.addField('❯ Resource',
					data.partype, true)
				.addField('❯ Armor',
					`${data.stats.armor} (${data.stats.armorperlevel}/level)`, true)
				.addField('❯ Attack Damage',
					`${data.stats.attackdamage} (${data.stats.attackdamageperlevel}/level)`, true)
				.addField('❯ Attack Range',
					data.stats.attackrange, true)
				.addField('❯ Attack Speed Offset',
					`${data.stats.attackspeedoffset} (${data.stats.attackspeedperlevel}/level)`, true)
				.addField('❯ Crit',
					`${data.stats.crit} (${data.stats.critperlevel}/level)`, true)
				.addField('❯ Move Speed',
					data.stats.movespeed, true)
				.addField('❯ Spell Block',
					`${data.stats.spellblock} (${data.stats.spellblockperlevel}/level)`, true)
				.addField('❯ Passive',
					data.passive.name, true)
				.addField('❯ Spells',
					data.spells.map((spell, i) => `${spell.name} (${buttons[i]})`).join('\n'), true);
			return msg.say(`Tip: ${tips[Math.floor(Math.random() * tips.length)]}`, { embed });
		} catch (err) {
			return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
		}
	}

	async fetchVersion() {
		const { body } = await snekfetch
			.get('https://na1.api.riotgames.com/lol/static-data/v3/versions')
			.query({ api_key: RIOT_KEY });
		[this.version] = body;
		setTimeout(() => { this.version = null; }, 3600000);
		return body;
	}

	async fetchChampions() {
		if (this.champions && this.champions.version === this.version) return this.champions;
		const { body } = await snekfetch
			.get(`https://ddragon.leagueoflegends.com/cdn/${this.version}/data/en_US/champion.json`);
		this.champions = body;
		return body;
	}

	async fetchChampion(champion) {
		const champions = await this.fetchChampions();
		const name = Object.keys(champions.data).find(key => key.toLowerCase() === champion);
		if (!name) return null;
		const { id } = champions.data[name];
		const { body } = await snekfetch
			.get(`https://ddragon.leagueoflegends.com/cdn/${this.version}/data/en_US/champion/${id}.json`);
		return body.data[id];
	}
};
