var _ = require("lodash");
var request = require("request");

var uuid = function() {
  var uuid = "",
    i,
    random;

  for (i = 0; i < 32; i++) {
    random = (Math.random() * 16) | 0;

    if (i == 8 || i == 12 || i == 16 || i == 20) {
      uuid += "-";
    }
    uuid += (i == 12 ? 4 : i == 16 ? (random & 3) | 8 : random).toString(16);
  }

  return uuid;
};

module.exports = function(controller) {
  controller.hears(
    ["^neko push (http(s)?://.+)?"],
    ["direct_message", "ambient"],
    function(bot, message) {
      var matches = message.text.match(
        /(http(s)?:\/\/.+\/.+(jpg|jpeg|png|gif))/i
      );
      var url = matches[1];
      var cat = {
        id: uuid(),
        channel: message.channel,
        cat: {
          url: url
        }
      };

      controller.storage.channels.save(cat, function(err) {
        if (!err) {
          bot.reply(message, "(ΦωΦ) ニャン (ID: " + cat.id + ")");
        } else {
          console.log(err);
        }
      });
    }
  );

  controller.hears(["^neko pull"], ["direct_message", "ambient"], function(
    bot,
    message
  ) {
    controller.storage.channels.all(function(err, res) {
      if (!err) {
        var pickedRecord = _.sample(
          _.filter(res, function(o) {
            return !!o.cat;
          })
        );

        if (pickedRecord && pickedRecord.cat) {
          bot.reply(message, pickedRecord.cat.url);
        } else {
          bot.reply(message, "(ΦωΦ) 猫いない");
        }
      } else {
        console.error(err);
      }
    });
  });

  controller.hears(["^neko count"], ["direct_message", "ambient"], function(
    bot,
    message
  ) {
    controller.storage.channels.all(function(err, res) {
      console.log(res);
      if (!err) {
        var catList = _.filter(res, function(o) {
          return !!o.cat;
        });

        bot.reply(message, ":cat2: " + catList.length + "匹ニャ");
      } else {
        console.error(err);
      }
    });
  });

  controller.hears(["^neko all-dump"], ["direct_message", "ambient"], function(
    bot,
    message
  ) {
    controller.storage.channels.all(function(err, res) {
      if (!err) {
        var dump = "";
        dump = JSON.stringify(res, null, 2);

        bot.reply(message, ":cat2: ```" + dump + "```");
      } else {
        console.error(err);
      }
    });
  });

  controller.hears(["^neko remove"], ["direct_message", "ambient"], function(
    bot,
    message
  ) {
    controller.storage.channels.all(function(err, res) {
      if (!err) {
        var catList = _.filter(res, function(o) {
          return !!o.cat;
        });
        
        var deletedUrlList = "";
        _.each(catList, function(cat) {
          request
            .get(cat.url, function(error, response, body) { 
              if (!error && response.statusCode === 200) return;
              controller.storage.channels.remove(cat, function(err, res) {
                if (!err) {
                  message += cat.url + "\n";
                } else {
                  console.error(err);
                }
              });
            })
        });
        bot.reply(message, ":cat2: " + deletedUrlList + "\nを削除したニャン");
      } else {
        console.error(err);
      }
    });
  });
};

@@ -1707,32 +1707,8 @@
 ) %7B%0A
-      console.log(res);%0A
     
@@ -2594,124 +2594,91 @@
     
-%0A        var deletedUrlList = %22%22;%0A        _.each(catList, function(cat) %7B%0A          request%0A            .get
+var removeCat = function(cat) %7B%0A          controller.storage.channels.remove
 (cat.
-url
+id
 , fu
@@ -2691,33 +2691,17 @@
 (err
-or
 , res
-ponse, body) %7B %0A  
+) %7B%0A
     
@@ -2720,125 +2720,36 @@
 !err
-or && response.statusCode === 200) return;%0A              controller.storage.channels.remove(cat, function(err, res) %7B
+) %7B%0A              bot.reply(
 %0A   
@@ -2757,35 +2757,32 @@
             
-if (!err) %7B
+message,
 %0A           
@@ -2790,21 +2790,24 @@
     
-  message
+%22:cat2: %22
  +
-=
  
+cat.
 cat.
@@ -2815,23 +2815,43 @@
 rl + %22%5Cn
-%22;%0A    
+%E3%82%92%E5%89%8A%E9%99%A4%E3%81%97%E3%81%9F%E3%83%8B%E3%83%A3%E3%83%B3%22%0A              );%0A
         
@@ -2873,28 +2873,24 @@
             
- 
-   
 console.erro
@@ -2905,29 +2905,39 @@
             
-    %7D
+%7D%0A          %7D);
 %0A           
@@ -2937,113 +2937,248 @@
     
+%7D;%0A
       
-%7D);%0A            %7D)%0A        %7D);%0A        bot.reply(message, %22:cat2: %22 + deletedUrlList + %22%5Cn%E3%82%92%E5%89%8A%E9%99%A4%E3%81%97%E3%81%9F%E3%83%8B%E3%83%A3%E3%83%B3%22
+  var checkCat = function(cat) %7B%0A          request.get(cat.cat.url, function(err, res) %7B%0A            if (!err && res.statusCode === 200) return;%0A            removeCat(cat);%0A          %7D);%0A        %7D;%0A%0A        catList.forEach(checkCat
 );%0A 

fix fix fix