/*
 * gulp-unused
 * https://github.com/felixzapata/gulp-unused
 *
 * Copyright (c) 2016 Félix Zapata
 * Licensed under the ISC license.
 */


'use strict';

var fs = require('fs');
var path = require('path');
var moment = require('moment');
var R = require('ramda');
var through = require('through2');
var gutil = require('gulp-util');
var glob = require('glob-all');
var PLUGIN_NAME = 'gulp-unused';

function fileExists(filePath) {
  try {
    return fs.statSync(filePath).isFile();
  } catch (err) {
    return false;
  }
}

//get current date and time
function getDateTime() {

  var date = new Date(),
    hour,
    min,
    sec,
    year,
    month,
    day;

  //get hours
  hour = date.getHours();
  hour = (hour < 10 ? '0' : '') + hour;

  //get minutes
  min = date.getMinutes();
  min = (min < 10 ? '0' : '') + min;

  //get seconds
  sec = date.getSeconds();
  sec = (sec < 10 ? '0' : '') + sec;

  //get year
  year = date.getFullYear();

  //get month
  month = date.getMonth() + 1;
  month = (month < 10 ? '0' : '') + month;

  //get day
  day = date.getDate();
  day = (day < 10 ? '0' : '') + day;

  return year + '-' + month + '-' + day;

}

function deleteFile(fileRef) {
  fs.unlinkSync(fileRef);
  gutil.log('deleted ' + fileRef);
}

function logFiles(fileRef) {
  gutil.log(fileRef);
}

function gulpUnused(customOptions, cb) {
  var reference;
  var directory;
  var unused;
  var content;
  var datemod;
  var todayDate = getDateTime();
  var startDate;
  var endDate;
  var dayDiff;
  var assets = [];
  var links = [];
  var defaultOptions = {
    reference: 'img/',
    directory: ['**/*.html'],
    remove: false,
    days: null,
    reportOutput: false,
    fail: false
  };

  var options = customOptions ? R.merge(defaultOptions, customOptions) : defaultOptions;
  var content;
  

  function bufferContents(file, enc, cb) {
  
    if (file.isNull()) {
      cb(null, file);
      return;
    }

    if (file.isStream()) {
      this.emit('error', new PluginError('gulp-concat',  'Streaming not supported'));
      cb();
      return;
    }
    
    // Get list of files depending on the file directory
    assets = glob.sync('**/*.*', { cwd: path.join(file.dirname, options.reference) });
      
    content = file.contents.toString();
    assets.forEach(function(asset) {
      if (content.indexOf(asset) !== -1) {
        links.push(asset);
      }
    });
    

    // Output unused files list in console
    unused = R.difference(assets, links);

    // output number of unused files
    if (unused.length) {
      gutil.log(gutil.colors.red(unused.length + ' unused file' + (unused.length === 1 ? '' : 's') + ':'));
    } else {
      gutil.log(gutil.colors.green('No unused files found.'));
    }

    unused.forEach(function(file) {
      // delete file if remove is set to true
      if (options.remove === true && options.days !== null) {
        datemod = fs.statSync(options.reference + file).mtime.toISOString();
        datemod = datemod.replace(/\T.+/, '');
        startDate = moment(datemod, 'YYYY-M-DD');
        endDate = moment(todayDate, 'YYYY-M-DD');
        dayDiff = endDate.diff(startDate, 'days');

        if (dayDiff >= options.days) {
          //delete file
          deleteFile(options.reference + file);
        } else {
          // log file references
          logFiles(options.reference + file);
        }
      } else if (options.remove === true) {
        //delete file
        deleteFile(options.reference + file);
      } else {
        // log file references
        logFiles(options.reference + file);
      }
    });
    
    if (unused.length > 0 && options.reportOutput) {
      var destDir = path.join(file.dirname, options.reportOutput);
      fs.writeFile(destDir, unused.join('\r\n'), function(err) {
        if (err) {
          throw err;
        } else {
          gutil.log(gutil.colors.green('Report "' + options.reportOutput + '" created.'));
          cb();
        }
      });
    }
    
    if (unused.length && !options.remove && options.fail) {
      gutil.log(gutil.colors.red('Unused files were found.'));
    }
    
   
  }

  return through.obj(bufferContents, cb);

}

// Exporting the plugin main function
module.exports = gulpUnused;
@@ -2758,20 +2758,20 @@
 unction(
-file
+item
 ) %7B%0A    
@@ -2924,20 +2924,20 @@
 rence + 
-file
+item
 ).mtime.
@@ -3226,32 +3226,56 @@
      deleteFile(
+path.join(file.dirname, 
 options.referenc
@@ -3267,39 +3267,39 @@
 ptions.reference
- + file
+, item)
 );%0A        %7D els
@@ -3346,32 +3346,56 @@
        logFiles(
+path.join(file.dirname, 
 options.referenc
@@ -3387,39 +3387,39 @@
 ptions.reference
- + file
+, item)
 );%0A        %7D%0A   
@@ -3500,16 +3500,40 @@
 eteFile(
+path.join(file.dirname, 
 options.
@@ -3533,39 +3533,39 @@
 ptions.reference
- + file
+, item)
 );%0A      %7D else 
@@ -3614,16 +3614,40 @@
 ogFiles(
+path.join(file.dirname, 
 options.
@@ -3655,23 +3655,23 @@
 eference
- + file
+, item)
 );%0A     

add full path to the file