'use strict';

const fs      = require('fs');
const Clapp   = require('./modules/clapp-discord');
const cfg     = require('../config.js');
const pkg     = require('../package.json');
const Discord = require('discord.js');
const bot     = new Discord.Client();
const tumblrLib  = require('tumblr');
const foodsites  = require('../foodtumblrs.js').sites;
const shibesites = require('../shibetumblrs.js').sites;
const birbsites = require('../birbtumblrs.js').sites;
const duccsites = require('../ducctumblrs.js').sites;

// Heroku workaround
const http = require('http'); 

const server = http.createServer( (req, res) => { 
	res.writeHead(200, {'Content-Type': 'text/plain'}); 
})
server.listen( process.env.PORT );


var app = new Clapp.App({
  name: cfg.name,
  desc: pkg.description,
  prefix: cfg.prefix,
  version: pkg.version,
  onReply: (msg, context) => {
    // Fired when input is needed to be shown to the user.

    context.msg.reply('\n' + msg).then(bot_response => {
      if (cfg.deleteAfterReply.enabled) {
        context.msg.delete(cfg.deleteAfterReply.time)
          .then(msg => console.log(`Deleted message from ${msg.author}`))
          .catch(console.log);
        bot_response.delete(cfg.deleteAfterReply.time)
          .then(msg => console.log(`Deleted message from ${msg.author}`))
          .catch(console.log);
      }
    });
  }
});

// Load every command in the commands folder
fs.readdirSync('./lib/commands/').forEach(file => {
  app.addCommand(require("./commands/" + file));
});

bot.on('message', msg => {
  // Fired when someone sends a message
  if (!msg.content.startsWith('!')) return;
  if (msg.author.bot) return;

  
  if (msg.content === '!food') {
		getRandomPhoto( msg, foodsites );
  }
  
  else if (msg.content === '!shibe') {
  		getRandomPhoto( msg, shibesites );
  }
  
  else if (msg.content === '!birb') {
  		getRandomPhoto( msg, birbsites );
  }
  
  else if (msg.content === '!ducc') {
  		getRandomPhoto( msg, duccsites );
  }
  
  
  
  if (app.isCliSentence(msg.content)) {
    app.parseInput(msg.content, {
      msg: msg
      // Keep adding properties to the context as you need them
    });
  }
  
  
  
});

bot.login(cfg.token).then(() => {
  console.log('Discord bot running!');
});

function getRandomPhoto( msg, sites, callback ) {
	
	var site = sites[Math.floor(Math.random()*sites.length)];
	var blog = new tumblrLib.Blog(site, cfg.tumblr_OAuth );
	
	var post_count = getPostCount( blog, function( postCount ) { 
	
		if( !postCount || postCount < 1 ) {
			console.log( "Post count < 1 or not valid" );
			msg.reply( "Oops! Something went wrong. Try again later." );
		}
	
		else {
			var offset = Math.floor(Math.random() * postCount );
			console.log( '-------\n' +
				'Site: ' + site + '\n' +
				'PostCount: ' + postCount + '\n' +
				'Selected post: ' + offset + '\n' 
			);
			var url = getURL( blog, offset, function( url ) { 
				msg.reply( url );
			});
		}
	});
};

/** gets the post count from a blog **/
function getPostCount( blog, callback ) {
	blog.info( function(error, response) {
		return callback( response ? response.blog.total_posts : -1 );
	});
};


function getURL( blog, offset, callback ) {
	
	var url = "";
	blog.photo( {type: 'photo', limit: '20', offset: offset}, function(error, response) {
		if (error) {
			console.log( error );
		}
		// pick a post
		var posts = response.posts;
		var rng = Math.floor(Math.random()*posts.length);
		var url = posts[rng].photos[0].original_size.url;
		console.log( 'url: ' + url );
		return callback( url );
	});
	//return "Oops! Something went wrong. Try again later.";
};
@@ -2664,32 +2664,33 @@
 et = Math.floor(
+ 
 Math.random() * 
@@ -2702,16 +2702,93 @@
 ount );%0A
+%09%09%09%0A%09%09%09if( offset + 20 %3E postCount ) %7B %0A%09%09%09%09offset = postCount - 1;%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%09conso
@@ -3264,23 +3264,8 @@
 %7B%0A%09%0A
-%09var url = %22%22;%0A
 %09blo
@@ -3297,18 +3297,17 @@
 limit: '
-20
+1
 ', offse
@@ -3441,60 +3441,8 @@
 ts;%0A
-%09%09var rng = Math.floor(Math.random()*posts.length);%0A
 %09%09va
@@ -3459,11 +3459,9 @@
 sts%5B
-rng
+0
 %5D.ph
@@ -3554,64 +3554,6 @@
 %7D);%0A
-%09//return %22Oops! Something went wrong. Try again later.%22;%0A
 %7D;

Fix the thing with the images, maybe