/**
 * sip-generator.js
 *
 * @description :: TODO: You might write a short summary of how this service works.
 */

var request = require('request'),
  fs = require('fs-extra'),
  path = require('path'),
  uuid = require('node-uuid'),
  got = require('got'),
  querystring = require('querystring'),
  Promise = require('bluebird');

var FocusedCrawler = module.exports = function(opts) {
  this.baseURL = opts.baseURL;
}

FocusedCrawler.prototype.enrich = function(crawlRecord, res) {
  console.log('[FocusedCrawler::enrich] crawl config: ' + JSON.stringify(crawlRecord, null, 4));

  crawlRecord.status = 'pending';

  var maxNumRetry = 10,
    numRetry = 0,
    baseURL = this.baseURL;

  crawlRecord.save(function(err, record0) {
    var qs = querystring.stringify(crawlRecord),
      crawlEndpoint = baseURL + 'crawl',
      loadCrawlEndpoint = baseURL + 'loadCrawl';

    var url = crawlEndpoint + '?' + qs;
    console.log('crawlEndpoint: ' + url);

    getCrawlId(url).then(function(response) {
      response = JSON.parse(response);

      console.log('crawl_id:' + response.crawl_id);

      checkCandidates(response.crawl_id).then(function(candidates) {
        return res.send(candidates).status(200);
      }).catch(function(err) {
        return res.send(err).status(500);
      });
    });
  });
}

function checkCandidates(crawl_id) {
  console.log('checking candidates: ' + crawl_id);

  return new Promise(function(resolve, reject) {
    getCandidates(crawl_id).then(function(response) {
      response = JSON.parse(response);
      console.log('gotresponse: ' + JSON.stringify(response, null, 4));
      if (response.length) {
        console.log('CANDIDATES!!');
        return resolve(response);
      } else {
        setTimeout(function() {
          console.log('retrying');
          return checkCandidates(crawl_id);
        }, 1000);
      }
    }).catch(function(err) {
      reject(err);
    });
  });
}

function getCrawlId(url) {
  return new Promise(function(resolve, reject) {
    got(url, function(err, response, body) {
      if (err) {
        return reject(err);
      }
      return resolve(response);
    });
  });
}

function getCandidates(crawl_id) {
  return new Promise(function(resolve, reject) {
    var qs = querystring.stringify({
      // crawl_id: response.crawl_id
      crawl_id: 13
    });

    var loadCrawlEndpoint = 'http://asev.l3s.uni-hannover.de:9986/api/CrawlAPI/loadCrawl';
    var url = loadCrawlEndpoint + '?' + qs;

    console.log('loadCrawlEndpoint: ' + url);

    got(url, function(err, response, body) {
      if (err) return reject(err);
      return resolve(response);
    });
  });
}

// else {
//   numRetry++;
//
//   console.log('Crawl not yet ready. Retried ' + numRetry + ' time(s) | maxNumRetry: ' + maxNumRetry);
//
//   if (maxNumRetry < numRetry) {
//     clearInterval(intervalID);
//
//     record0.status = 'error';
//
//     record0.save(function(err, record1) {
//       console.log('Reached maximum retry count. Aborting...');
//     });
//   }
// }

@@ -1444,24 +1444,31 @@
 ject) %7B%0A    
+return 
 getCandidate
@@ -1497,32 +1497,63 @@
 ion(response) %7B%0A
+      console.log('response');%0A
       response =
@@ -1576,24 +1576,27 @@
 onse);%0A     
+ //
  console.log
@@ -1773,16 +1773,23 @@
         
+return 
 setTimeo
@@ -2277,24 +2277,25 @@
 , reject) %7B%0A
+%0A
     var qs =
@@ -2324,19 +2324,16 @@
 (%7B%0A     
- //
  crawl_i
@@ -2339,44 +2339,16 @@
 id: 
-response.crawl_id%0A      crawl_id: 13
+crawl_id
 %0A   

fix for topic crawling