var Promise = require('bluebird'),
  fs = require('fs-extra'),
  path = require('path'),
  url = require('url'),
  cheerio = require('cheerio'),
  validUrl = require('valid-url'),
  Load = require('./load.js'),
  config = require('./defaults.js');

var ScraperCore = function(data){
  var options = {},
    load,
    $;

  options.url = data.url;
  options.path = data.path;
  options.indexFile = data.indexFile || config.indexFile;
  options.srcToLoad = data.srcToLoad || config.srcToLoad;
  options.staticDirectories = data.hasOwnProperty('staticDirectories') ? data.staticDirectories : config.staticDirectories;

  var prepare = Promise.method(function(html){
    $ = cheerio.load(html);

    $('base').each(function(){
      options.url = $(this).attr('href');
      console.log('Found <base>. Url changed to ' + options.url);
      $(this).remove();
    });

    // html5
    $('meta[charset]').filter(function(){
      var charset = $(this).attr('charset').toLowerCase();
      if(charset.indexOf('utf-8') < 0){
        $(this).attr('charset', 'utf-8');
        console.log('Found ' + charset + ' in <meta> and replace charset to utf-8');
      }
    });

    // other html
    $('meta[content*=charset]').filter(function(){
      var content = $(this).attr('content').toLowerCase();
      if(content.indexOf('utf-8') < 0){
        $(this).attr('content', 'text/html; charset=utf-8');
        console.log('Found ' + content + ' in <meta> and replace charset to utf-8');
      }
    });
  });

  var loadHtmlSources = Promise.method(function(srcArray){
    var p = load.loadCss($.html()),
      currentSrc;

    while(currentSrc = srcArray.pop()) {
      (function(src) {
        p = p.then(function(newHtml){return load.loadSources(newHtml, src.selector, src.attributeName)});
      }) (currentSrc);
    }

    p = p.then(function(newHtml){$ = cheerio.load(newHtml)});
    return p;
  });

  var process = Promise.method(function(){
    var indexFileFullPath = path.resolve(options.path, options.indexFile);
    load = new Load(options.url, options.path, options.staticDirectories);

    return load.loadIndexHtml()
      .then(prepare)
      .then(function(){return loadHtmlSources(options.srcToLoad)})
      .then(function(){return fs.outputFileAsync(indexFileFullPath, $.html())})
      .then(function(){return {status: 'success'}});
  });

  return {
    scrape: function(callback) {

      if(!options.url){
        return callback({status:'error', message:'Url is undefined!'});
      }
      if(!validUrl.isWebUri(options.url)){
        return callback({status:'error', message:'Url ' + options.url + ' is not correct!'});
      }
      if(!options.path){
        return callback({status:'error', message:'Path is undefined!'});
      }
      if(fs.existsSync(options.path)){
        return callback({status:'error', message:'Path ' + options.path + ' exists!'});
      }

      process()
        .then(function(res){return callback(res)})
        .catch(function(e){return callback({status: 'error', message: e.toString()})})
    }
  }
};

module.exports = ScraperCore;

@@ -2517,33 +2517,18 @@
 ack(
-%7Bstatus:'error', message:
+new Error(
 'Url
@@ -2534,33 +2534,39 @@
 l is undefined!'
-%7D
+), null
 );%0D%0A      %7D%0D%0A   
@@ -2634,33 +2634,18 @@
 ack(
-%7Bstatus:'error', message:
+new Error(
 'Url
@@ -2681,17 +2681,23 @@
 orrect!'
-%7D
+), null
 );%0D%0A    
@@ -2755,33 +2755,18 @@
 ack(
-%7Bstatus:'error', message:
+new Error(
 'Pat
@@ -2781,17 +2781,23 @@
 efined!'
-%7D
+), null
 );%0D%0A    
@@ -2869,33 +2869,18 @@
 ack(
-%7Bstatus:'error', message:
+new Error(
 'Pat
@@ -2910,17 +2910,23 @@
 exists!'
-%7D
+), null
 );%0D%0A    
@@ -2993,16 +2993,22 @@
 allback(
+null, 
 res)%7D)%0D%0A
@@ -3054,48 +3054,15 @@
 ack(
-%7Bstatus: 'error', message: e.toString()%7D
+e, null
 )%7D)%0D

change callback to node style - callback(err, res)