'use strict';

var DEBUG = true;
var kue = require('kue');

module.exports = function setup(options, imports, register) {
    console.log("Setup Core module.");
    var server = new Server(options, imports);
    if(server.start()){
      register(null, {
        "server": server
      });
    }else{
      console.log('error starting the server');
    }
};

var Server = function(options, imports){
    this.$scraper = imports.scraper;
    this.$resolveurl = imports.resolveurl;
    this.$db = imports.db;
    this.jobs = kue.createQueue({
      prefix: 'l',
      redis: {
        host : options.redis.host,
        port : options.redis.port,
        user : options.redis.username,
        pass : options.redis.password
      }
    });
};

(function(){

    this.start = function(data, done){
      var me = this;
      this.jobs.process('resolveUrl', function(job, done){
        me.doResolveUrl(job.data, done);
      });
      this.jobs.process('scrape', function(job, done){
        me.doScrapeUrl(job.data, done);
      });
      this.jobs.process('html', function(job, done){
        me.doScrapeHtml(job.data, done);
      });
      return true;
    };

    this.doResolveUrl = function (data, done) {
      DEBUG && console.log("doResolveUrl: " + data.u);
      var url = data.u;
      if(!url){
        return done(new Error("Url required."));
      }

      this.$resolveurl.getOriginalURL(url, function(err, _url){
        if (err) {
          return done(new Error(err.message));
        }
        if (url != _url) {
          var result = {
            url: url,
            resolve: _url
          };
          console.log( _url );
          done(null, result);
        } else {
          done();
        }
      });
    };

    this.doScrapeUrl = function (data, done) {
      DEBUG && console.log("doScrapeUrl: " + data.u);
      var url = data.u;
      if(!url){
        return done(new Error("Url required."));
      }
      this.$scraper.getStructuredData(url, done);
    };

    this.doScrapeHtml = function (data, done) {
      DEBUG && console.log("doScrapeHtml: " + data.u);
      var url = data.u;
      if(!url){
        return done(new Error("Url required."));
      }

      this.$scraper.getLinksFromHtml(text, done);
    };

}).call(Server.prototype);

@@ -12,26 +12,8 @@
 ';%0A%0A
-var DEBUG = true;%0A
 var 
@@ -34,16 +34,16 @@
 'kue');%0A
+
 %0Amodule.
@@ -375,16 +375,48 @@
 ports)%7B%0A
+    this.DEBUG = options.DEBUG,%0A
     this
@@ -1215,32 +1215,37 @@
 , done) %7B%0A      
+this.
 DEBUG && console
@@ -1366,33 +1366,32 @@
 ed.%22));%0A      %7D%0A
-%0A
       this.$reso
@@ -1631,39 +1631,8 @@
  %7D;%0A
-          console.log( _url );%0A
     
@@ -1765,32 +1765,37 @@
 , done) %7B%0A      
+this.
 DEBUG && console
@@ -2035,16 +2035,21 @@
 %7B%0A      
+this.
 DEBUG &&
@@ -2178,24 +2178,24 @@
 ed.%22));%0A
+
       %7D%0A
 %0A      t
@@ -2186,17 +2186,16 @@
       %7D%0A
-%0A
       th

DEBUG is passed from app.js