// Dependencies
var cheerio = require('cheerio');
var	request = require('request');
var fs = require('fs');
var rp = require('request-promise');
var dateTime = require('node-datetime');
var fs = require('fs');
var targz = require('tar.gz');
var exec = require('child_process').exec;
var mkdirp = require('mkdirp');

//define variables
var images =[];
var unsplash_images =[];
var final_unsplash_images =[];
var twitter_images =[];
var flickr_images = [];
var final_flickr_images = [];
var pinterest_images =[];
var final_array =[];
var high_resolution_images =[];
var hashtag; // search word for each source to be scraped
var limit; // limit of images to be returned to user
var download_directory = './images/'; // specify download directory


var exports = module.exports = {};


/*exports.highres = function(hashtag,compress,limit) {
  var images =[];
  var unsplash_images =[];
  var final_unsplash_images =[];


  var options = {
      uri: 'https://www.pinterest.com/search/pins/?q='+hashtag,
      transform: function (body) {
          return cheerio.load(body);
      }
  };*/


// export functionality for scraping flickr.com
exports.flickr = function(hashtag,limit,compress) {
  var flickr_images = []; // orginal array before it is trimmed
  var final_flickr_images = []; // final array of images from flickr.com

  var options = { // specify url to scrape and load into cheerio
      uri: 'https://www.flickr.com/search/?text=' +hashtag,
      transform: function (body) {
          return cheerio.load(body);
      }
  };
  return rp(options)
      .then(function ($) {
        $('div.photo-list-photo-view').each(function(){
          var img = $(this).attr('style');
          // add all images found in the above div under the 'style' attribute to an array
          flickr_images.push(img);
        })

        // check for limit, if defined trim the number of images returned
        if(limit){
          flickr_images = flickr_images.slice(0, limit);
        }

        for(var i = 0; i < flickr_images.length; i++ ){
            // get the text in between the brackets (this is the URL)
            var string_url = flickr_images[i].substring(flickr_images[i].lastIndexOf("(")+1,flickr_images[i].lastIndexOf(")"));
            // trim the forward slash at the beginning
            var final_strings = string_url.slice(2);
            // add http:// to beginning of each string so it can be recognised at a url
             final_strings = 'http://'+final_strings;
            // push to final array
            final_flickr_images.push(final_strings);
        }

      // get current date and time
      var dt = dateTime.create();
      // format date and time
      var formatted = dt.format('Y-m-d-H:M:S');

       // make folder with the name as the current date and time, place in images folder
       var mkdir = 'mkdir -p ' + download_directory+formatted;
       // set permissions for folder
       var child = exec(mkdir,'0777', function(err, stdout, stderr) {
           for(var i = 0; i < final_flickr_images.length; i++ ){
           // loop through array of images and add to folder
           request(final_flickr_images[i]).pipe(fs.createWriteStream(download_directory+formatted+'/flickr'+hashtag+ '' +i+ '.jpg'));
          }

          // check if compress variable is defined, if so, compress the folder which was just created
            if(compress ==true){
              // read folder from directory
              var read = targz().createReadStream("./images/"+formatted);
              // create tarball
              var write = fs.createWriteStream(formatted+'.tar.gz');
              read.pipe(write);
            }
       });
       // return final array of images
       return final_flickr_images;
    })
};






exports.unsplash = function(hashtag,limit,compress) {
  var unsplash_images =[];
  var final_unsplash_images =[];

  var options = { // specify url to scrape and load into cheerio
      uri: 'https://unsplash.com/search/'+hashtag,
      transform: function (body) {
          return cheerio.load(body);
      }
  };
  return rp(options)
      .then(function ($) {
        $('a','div.Nqw-T').each(function(){
          var img = $(this).attr('href');
          // add all images found in the above div under the 'href' attribute to an array
          unsplash_images.push(img);
      })
        console.log(unsplash_images);
      // loop through array
      for (var i=unsplash_images.length-1; i>=0; i--) {
      // remove all that do not being with 'http'

        if (unsplash_images[i].substring(0, 4) !== 'http'){
      unsplash_images.splice(i, 1);
      }
    }
    console.log(unsplash_images);

     for(var i = 0; i < unsplash_images.length; i++ ){
        var subStr = unsplash_images[i].substring(0,47);
        final_unsplash_images.push(subStr);
    }

  //  console.log(final_unsplash_images);
    if(limit){
      final_unsplash_images = final_unsplash_images.slice(0, limit);
    }
      var dt = dateTime.create();
      var formatted = dt.format('Y-m-d-H:M:S');
      console.log(formatted);
    //   var download_directory = './images/';
       var mkdir = 'mkdir -p ' + download_directory+formatted;
       var child = exec(mkdir,'0777', function(err, stdout, stderr) {

          // if (err) throw err;
           for(var i = 0; i < final_unsplash_images.length; i++ ){
           request(final_unsplash_images[i]).pipe(fs.createWriteStream(download_directory+formatted+'/unsplash'+hashtag+ '' +i+ '.jpg'));
            }
            if(compress ==true){
              // Create all streams that we need
              var read = targz().createReadStream("./images/"+formatted);
              var write = fs.createWriteStream(formatted+'.tar.gz');
              read.pipe(write);
            }
       });
return final_unsplash_images;

    })


};
exports.pinterest = function(hashtag,compress,limit) {
  var images =[];

  var options = {
      uri: 'https://www.pinterest.com/search/pins/?q='+hashtag,
      transform: function (body) {
          return cheerio.load(body);
      }
  };


  return rp(options)
      .then(function ($) {
        $('img','div.SearchPageContent').each(function(){
          var img = $(this).attr('src');

          images.push(img);
      })

      for (var i=images.length-1; i>=0; i--) {
        if (images[i].substring(0, 5) !== 'https') {
            images.splice(i, 1);
        }
      }
      if(limit){

        images = images.slice(0, limit);

      }

    /*//replace image size to original size
      for(var i=0; i < images.length; i++) {
       images[i] = images[i].replace(/236x/g, '564x');

     }*/



      var dt = dateTime.create();
      var formatted = dt.format('Y-m-d-H:M:S');
    //  console.log(formatted);
    //   var download_directory = './images/';

       var mkdir = 'mkdir -p ' + download_directory+formatted;
       var child = exec(mkdir,'0777', function(err, stdout, stderr) {
           if (err) throw err;

          for(var i = 0; i < images.length; i++ ){
           request(images[i]).pipe(fs.createWriteStream(download_directory+formatted+'/pinterest'+hashtag+ '' +i+ '.jpg'));
            }
            if(compress ==true){
              // Create all streams that we need
              var read = targz().createReadStream("./images/"+formatted);
              var write = fs.createWriteStream(formatted+'.tar.gz');


              read.pipe(write);
            }
       });
  return images;

    })
};


exports.twitter = function(hashtag,compress,limit) {
  var twitter_images =[];
  var options = {
      uri: 'https://twitter.com/search?q=%23'+hashtag,
      transform: function (body) {
          return cheerio.load(body);
      }
  };


  return rp(options)
      .then(function ($) {

      	$('img','div.stream').each(function(){
          var img = $(this).attr('src');

          twitter_images.push(img);
      })

      var search_term = 'https://pbs.twimg.com/media';


			for (var i=twitter_images.length-1; i>=0; i--) {
    		if (twitter_images[i].substring(0, 27) !== search_term) {
        twitter_images.splice(i, 1);
    }
}

/*for(var i = 0; i < twitter_images.length; i++ ){
request(twitter_images[i]).pipe(fs.createWriteStream(''+hashtag+ '' +i+ '.jpg'));
}
      return twitter_images;
*/
if(limit){

  twitter_images = twitter_images.slice(0, limit);

}


      var dt = dateTime.create();
      var formatted = dt.format('Y-m-d-H:M:S');
      console.log(formatted);
       var download_directory = './images/';

       var mkdir = 'mkdir -p ' + download_directory+formatted;
       var child = exec(mkdir,'0777', function(err, stdout, stderr) {
           if (err) throw err;

          for(var i = 0; i < twitter_images.length; i++ ){
           request(twitter_images[i]).pipe(fs.createWriteStream(download_directory+formatted+'/twitter'+hashtag+ '' +i+ '.jpg'));
            }
            if(compress ==true){
              // Create all streams that we need
              var read = targz().createReadStream("./images/"+formatted);
              var write = fs.createWriteStream(formatted+'.tar.gz');


              read.pipe(write);
            }
       });
return images;

    })
};

module.exports;
