module.exports =  {
	'help': {
		desc: `Print the allowed commands.`,
		async function(from){
			let intro = [
				'I am a bot owned and operated by',
				'wmantly <wmantly@gmail.com>',
				'You have access to the following commands:'
			]
			await this.whisper(from, ...intro, ...this.__reduceCommands(from).map(command =>
				`${command} -- ${this.commands[command].desc || ''}`
			));
		}
	},
	'say': {
		desc: `Make the bot say stuff in chat`,
		allowed: ['wmantly', 'useless666', 'tux4242',],
		ignoreLock: true,
		async function(from, ...messages){
			await this.say((messages || []).join(' '));
		}

	},
	'plugins': {
		desc: 'List the plugins',
		allowed: ['wmantly', 'useless666', 'tux4242',],
		ignoreLock: true,
		async function(from, botName){
			if(botName){
				if(botName in this.constructor.bots){
					this.whisper(from, `${Object.keys(this.constructor.bots[botName].plunginsLoaded)}`)
				}
			}
			
		}
	},
	'unload': {
		desc: `Make bot unload plugin`,
		allowed: ['wmantly', 'useless666', 'tux4242',],
		ignoreLock: true,
		async function(from, botName, plugin) {
			this.whisper(from, `Unloading ${plugin}`);
			if(botName in this.constructor.bots){
				let bot = this.constructor.bots[botName];
				let status = await bot.pluginUnload(plugin);
				return this.whisper(from, `plugin status ${status}`);
			}

			this.whisper(from, '?')
		}
	},
	'load': {
		desc: `Make bot load/unload plugin`,
		allowed: ['wmantly', 'useless666', 'tux4242',],
		ignoreLock: true,
		async function(from, botName, plugin) {
			this.whisper(from, `Loading ${plugin}`);
			if(botName in this.constructor.bots){
				let bot = this.constructor.bots[botName];
				let status = await bot.pluginLoad(plugin);
				return this.whisper(from, `plugin status ${status}`);
			}

			this.whisper(from, '?')
		}
	},
	'guardian': {
		desc:'',
		allowed: ['wmantly', 'useless666', 'tux4242',],
		ignoreLock: true,
		async function(from, botName, action) {
			this.whisper(from, `Loading ${action}`);
			if(botName in this.constructor.bots){
				let bot = this.constructor.bots[botName];
				let status = await bot.pluginLoad(action);
				return this.whisper(from, `plugin status ${status}`);
			}

			this.whisper(from, '?')
		}
	},
	'ai': {
		desc: `Make bot load/unload plugin`,
		allowed: ['wmantly', 'useless666', 'tux4242',],
		ignoreLock: true,
		async function(from, botName, personality, ...custom) {
			if(botName in this.constructor.bots ){
				let bot = this.constructor.bots[botName];
				if(bot.isReady){
					let status = await bot.pluginLoad('Ai', {
						promptName: personality,
						prompCustom: custom,
					});
					
					return this.whisper(from, `plugin status ${status}`);
				}

			}

			this.whisper(from, '?')
		}
	},
	'come': {
		desc: `make bot come to you`,
		allowed: ['wmantly', 'useless666', 'tux4242'],
		async function(from, playerName){
			const player = this.bot.players[playerName || from];

			if (!player || !player.entity) {
				this.whisper(from, `I can't see ${player}.`);

				return;
			}

			this.whisper(from, `Going to ${player}`);
			this.goTo({where: player.entity.position});

		}
	},

	'logon': {
		desc: `Have bot log on for 10 seconds'`,
		allowed: ['wmantly', 'useless666', 'tux4242', 'pi_chef', '1_cut',],
		ignoreLock: true,
		async function(from, botName, time){
			this.__unLockCommand();

			if(botName in this.constructor.bots){
				let bot = this.constructor.bots[botName];

				if(!bot.isReady){
					try{
						await bot.connect();
						var clear = setTimeout(()=> bot.quit(), time ? parseInt(time)*1000 : 10000);
						bot.whisper(from, 'I am ready')
					}catch(error){
						console.log('inv error connecting to bot');
						this.whisper(from, 'Bot is not available right now, try again in 30 seconds.');
					}
				}else{
					await this.whisper(from, `Bot ${bot.bot.entity.username} Already online`);
				}
			}
		}
	},
	'summon': {
		desc: `Summon a bot online indefinitely`,
		allowed: ['wmantly', 'useless666', 'tux4242'],
		ignoreLock: true,
		async function(from, botName){
			if(botName in this.constructor.bots){
				let bot = this.constructor.bots[botName];
				
				if (!bot.isReady){
					try{
						await bot.connect();
						this.whisper(from, `${botName} is now online`);
					}catch(error){
						this.whisper(from, `Failed to summon ${botName}. Try again later.`);
					}
				} else {
					this.whisper(from, `${botName} is already online`);
				}
			} else {
				this.whisper(from, `Unknown bot: ${botName}`);
			}
		}
	},
	'resupply': {
		desc: 'Trigger farm resupply manually',
		allowed: ['wmantly', 'useless666', 'tux4242'],
		ignoreLock: true,
		async function(from, botName) {
			const target = botName ? this.constructor.bots[botName] : this;
			if (!target) return this.whisper(from, 'Unknown bot');
			const fs = target.plunginsLoaded['FarmSupply'];
			if (!fs) return this.whisper(from, 'FarmSupply not loaded');
			this.whisper(from, 'Starting resupply...');
			await fs.resupply();
			this.whisper(from, 'Resupply complete');
		}
	},
	'dismiss': {
		desc: `Send a bot offline`,
		allowed: ['wmantly', 'useless666', 'tux4242'],
		ignoreLock: true,
		async function(from, botName){
			if(botName in this.constructor.bots){
				let bot = this.constructor.bots[botName];
				
				if(bot.isReady){
					bot.quit();
					this.whisper(from, `${botName} is now offline`);
				} else {
					this.whisper(from, `${botName} is already offline`);
				}
			} else {
				this.whisper(from, `Unknown bot: ${botName}`);
			}
		}
	},
};
