var logging = require("./logging");
var config = require("../config");
var cache = require("./cache");
var path = require("path");
var df = require("node-df");
var fs = require("fs");

var redis = cache.get_redis();
var exp = {};

// does nothing
function nil() {}

// compares redis' used_memory with cleaning_redis_limit
// callback: error, true|false
function should_clean_redis(callback) {
  cache.info(function(err, info) {
    if (err) {
      callback(err, false);
    } else {
      try {
        // logging.debug(info.toString());
        logging.debug("used mem:" + info.used_memory);
        var used = parseInt(info.used_memory) / 1024;
        logging.log("RedisCleaner:", used + "KB used");
        callback(err, used >= config.cleaner.redis_limit);
      } catch(e) {
        callback(e, false);
      }
    }
  });
}

// uses `df` to get the available fisk space
// callback: error, true|false
function should_clean_disk(callback) {
  df({
    file: config.directories.faces,
    prefixMultiplier: "KiB",
    isDisplayPrefixMultiplier: false,
    precision: 2
  }, function(err, response) {
    if (err) {
      callback(err, false);
    } else {
      var available = response[0].available;
      logging.log("DiskCleaner:", available + "KB available");
      callback(err, available < config.cleaner.disk_limit);
    }
  });
}

// check if redis limit reached, then flush redis
// check if disk limit reached, then delete images
exp.run = function() {
  should_clean_redis(function(err, clean) {
    if (err) {
      logging.error("Failed to run RedisCleaner");
      logging.error(err);
    } else if (clean) {
      logging.warn("RedisCleaner: Redis limit reached! flushing now");
      redis.flushall();
    } else {
      logging.log("RedisCleaner: Nothing to clean");
    }
  });

  should_clean_disk(function(err, clean) {
    if (err) {
      logging.error("Failed to run DiskCleaner");
      logging.error(err);
    } else if (clean) {
      logging.warn("DiskCleaner: Disk limit reached! Cleaning images now");
      var skinsdir = config.directories.skins;
      var capesdir = config.directories.capes;
      var facesdir = config.directories.faces;
      var helmsdir = config.directories.helms;
      var rendersdir = config.directories.renders;
      fs.readdir(skinsdir, function(readerr, files) {
        if (!readerr) {
          for (var i = 0, l = Math.min(files.length, config.cleaner.amount); i < l; i++) {
            var filename = files[i];
            if (filename[0] !== ".") {
              fs.unlink(path.join(facesdir, filename), nil);
              fs.unlink(path.join(helmsdir, filename), nil);
              fs.unlink(path.join(skinsdir, filename), nil);
            }
          }
        }
      });
      fs.readdir(rendersdir, function(readerr, files) {
        if (!readerr) {
          for (var j = 0, l = Math.min(files.length, config.cleaner.amount); j < l; j++) {
            var filename = files[j];
            if (filename[0] !== ".") {
              fs.unlink(rendersdir + filename, nil);
            }
          }
        }
      });
      fs.readdir(capesdir, function(readerr, files) {
        if (!readerr) {
          for (var j = 0, l = Math.min(files.length, config.cleaner.amount); j < l; j++) {
            var filename = files[j];
            if (filename[0] !== ".") {
              fs.unlink(capesdir + filename, nil);
            }
          }
        }
      });
    } else {
      logging.log("DiskCleaner: Nothing to clean");
    }
  });
};

module.exports = exp;
@@ -545,36 +545,28 @@
     
-logging.debug(%22used mem:%22 + 
+var used = parseInt(
 info
@@ -578,16 +578,23 @@
 _memory)
+ / 1024
 ;%0A      
@@ -602,71 +602,72 @@
  var
+ result =
  used 
+%3E
 = 
-parseInt(info.used_memory) / 1024;%0A        logging.log(
+config.cleaner.redis_limit;%0A        var msg = 
 %22Red
@@ -676,18 +676,20 @@
 Cleaner:
-%22,
+ %22 +
  used + 
@@ -701,66 +701,90 @@
 sed%22
-);%0A        callback(err, used %3E= config.cleaner.redis_limi
+;%0A        (result ? logging.log : logging.debug)(msg);%0A        callback(err, resul
 t);%0A
@@ -1229,36 +1229,92 @@
 able;%0A      
-logging.log(
+var result = available %3C config.cleaner.disk_limit;%0A      var msg = 
 %22DiskCleaner
@@ -1314,18 +1314,20 @@
 Cleaner:
-%22,
+ %22 +
  availab
@@ -1349,67 +1349,86 @@
 ble%22
-);%0A      callback(err, available %3C config.cleaner.disk_limi
+;%0A      (result ? logging.log : logging.debug)(msg);%0A      callback(err, resul
 t);%0A
@@ -1848,18 +1848,20 @@
 logging.
-lo
+debu
 g(%22Redis
@@ -3564,18 +3564,20 @@
 logging.
-lo
+debu
 g(%22DiskC

be less verbose