const commandBase = require('../modules/commandBase')

module.exports = class help extends commandBase {
	constructor() {
		super()
		this.aliases     = ['?']
		this.description = 'Shows a list of help channels'
		this.usage.args  = '[command]'
		this.usage.text  = 'If command is not specified, returns a list of commands. Otherwise, shows usage for the ' +
			'given command.'
	}

	execute(msg, args) {
		let commands = msg.client.commands

		if(!args[0]) {
			let lines  = []
			let cmds   = []
			let maxLen = [0, 0]

			for (let [key, command] of commands) {
				if(key != command.constructor.name) continue
				if(!command.description) continue

				let name = command.constructor.name
				let desc = command.description

				cmds.push([name, desc])
				if(name.length > maxLen[0]) maxLen[0] = name.length
				if(desc.length > maxLen[1]) maxLen[1] = desc.length
			}

			cmds.forEach((arr) => {
				let paddedName = rightPad(arr[0], maxLen[0])
				let paddedDesc = rightPad(arr[1], maxLen[1])
				lines.push(`[${paddedName}](${paddedDesc})`)
			})

			lines.push('', '# Type help <command> for usage on a command.')

			let text = lines.join("\n")

			msg.author.send(text, {code: 'md'}).then(() => {
				if(msg.channel.type !== 'dm') msg.reply('Check your DMs!')
			}).catch(() => {
				if(msg.channel.type !== 'dm') msg.reply('Unable to send you a DM, is your DMs open?')
			})
		} else {
			let usage = this._returnUsage(args[0], commands)

			if(!usage) msg.reply('Command not found.')
			else msg.channel.send(usage, {code: true})
		}
	}

	_returnUsage(cname, commands) {
		if(!commands.get(cname)) return false

		let command = commands.get(cname)
		let aliases = [command.constructor.name, ...command.aliases]
		let text = []

		text[0] = aliases.length > 1 ? `{${aliases.join(",")}}` : aliases[0]
		if(command.usage.args)  text.push(` ${command.usage.args}`)
		if(command.description) text.push(` - ${command.description}`)
		if(command.usage.text)  text.push(`\n\n${command.usage.text}`)

		return text.join('')
	}
}
		
function rightPad(str, len) {
	var i = -1
	len = len - str.length
	while (++i < len) str += ' ';
	return str
}
@@ -1427,25 +1427,27 @@
 age(
-args%5B0%5D, commands
+msg.client, args%5B0%5D
 )%0A%0A%09
@@ -1507,20 +1507,13 @@
 msg.
-channel.send
+reply
 (usa
@@ -1557,22 +1557,19 @@
 ge(c
-name, commands
+lient, name
 ) %7B%0A
@@ -1570,24 +1570,31 @@
 me) %7B%0A%09%09if(!
+client.
 commands.get
@@ -1590,25 +1590,24 @@
 ommands.get(
-c
 name)) retur
@@ -1631,16 +1631,23 @@
 mmand = 
+client.
 commands
@@ -1651,17 +1651,16 @@
 nds.get(
-c
 name)%0A%09%09

rework _returnUsage slightly to be use client and not client.commands