const Canvas = require('canvas');
const w = 300; const h = 100;
const canvas = new Canvas(w, h);
const ctx = canvas.getContext('2d');

const botUtil = require('../util/botUtil');
const CommandHandler = require('../util/msgUtil');

const config = require('../config');

/**
 * Primary message listener
 * @param  {IMessage} msg Message object to be used
 * @param  {Discordie} client Bot client object
 */
function respond (msg, client) {
	const msgText = msg.content;
	const sender = msg.member || msg.author; // IUser as a substitute for DMs
	const msgChannel = msg.channel;
	const msgGuild = msg.guild;

	const botUser = msg.isPrivate ? client.User : client.User.memberOf(msgGuild);

	const sendMessage = (s, e) => msgChannel.sendMessage(s, false, e);
	const uploadFile = (s, n) => msgChannel.uploadFile(s, n || "drawing.png");

	const command = msgText.slice(config.prefix.length);
	const handler = new CommandHandler(command);

	if (!msgText.startsWith(config.prefix)) return;

	const addCommand = (c, f) => handler.addCommand(c, f);
	const addCommandSentence = (c, f) => handler.addCommandSentence(c, f);
	const addCommandArgs = (c, f) => handler.addCommandArgs(c, f);

	const senderIsOwner = botUtil.senderIsOwner(msg);

	addCommandSentence('evalC', a => {
		if (!senderIsOwner || a === 'e.message.content' || a === 'msgText') return;
		let result;
		return new Promise(resolve => {
			result = eval(a);
			resolve('Success');
		}).catch(e => {
			// sendMessage('\u{1F52B}'); // Peestol
			console.log(e);
			sendMessage('It didn\'t work.');
		}).then(v => {
			if (v === 'Success') {
				if (typeof result === 'string' || typeof result === 'number' || typeof result === 'boolean') sendMessage(result);
				else if (Array.isArray(result)) sendMessage(result.join(', '));
				// else sendMessage('\u{1F44C}'); // Ok hand sign
			}
		});
	});

	addCommand('testDraw', () => {
		ctx.fillStyle = '#FFB2C5';
		ctx.fillRect(0, 0, w, h);
		ctx.fillStyle = 'white';
		ctx.font = '48px serif';
		ctx.fillText('Hello, ' + (sender.nick || sender.username), 10, 50);
		const imgBuffer = canvas.toBuffer();
		uploadFile(imgBuffer);
	});
}

exports.respond = respond;

@@ -30,108 +30,8 @@
 s');
-%0Aconst w = 300; const h = 100;%0Aconst canvas = new Canvas(w, h);%0Aconst ctx = canvas.getContext('2d');
 %0A%0Aco
@@ -923,32 +923,275 @@
 dCommand(c, f);%0A
+%09const addDrawCommand = (c, f, w, h) =%3E addCommand(c, () =%3E %7B%0A%09%09const canvas = new Canvas(w %7C%7C 640, h %7C%7C 480);%0A%09%09const ctx = canvas.getContext('2d');%0A%09%09f(ctx, canvas, w, h);%0A%09%09const imgBuffer = canvas.toBuffer();%0A%09%09uploadFile(imgBuffer);%0A%09%7D);%0A
 %09const addComman
@@ -1985,24 +1985,28 @@
 ;%0A%09%7D);%0A%0A%09add
+Draw
 Command('tes
@@ -2014,16 +2014,33 @@
 Draw', (
+ctx, canvas, w, h
 ) =%3E %7B%0A%09
@@ -2224,73 +2224,19 @@
 );%0A%09
-%09const imgBuffer = canvas.toBuffer();%0A%09%09uploadFile(imgBuffer);%0A%09%7D
+%7D, 300, 100
 );%0A%7D

add draw command method