'use strict';

module.exports = function(grunt) {

  var fs = require("fs");

  var childProcess = require('child_process');
  var phantomjs = require('phantomjs');
  var ph_libutil = require("phantomizer-libutil");

  grunt.registerMultiTask("phantomizer-confess", "Measure page loading times", function () {

    var webserver           = ph_libutil.webserver;
    var router_factory      = ph_libutil.router;
    var optimizer_factory   = ph_libutil.optimizer;
    var meta_factory        = ph_libutil.meta;

    var config = grunt.config.get();

    var options = this.options({
      meta_dir:'',
      web_server_paths:[],
      in_request:'',
      host:'localhost',
      port:'',
      ssl_port:'',
      web_server_log:false,
      inject_assets:true,
      action:'performance'
    });

    var in_request = options.in_request;
    var web_server_paths = options.web_server_paths;
    var meta_dir = options.meta_dir;
    var host = options.host;
    var port = options.port?options.port:"";
    var ssl_port = options.ssl_port?options.ssl_port:"";
    var action = options.action;
    var web_server_log = options.web_server_log;
    var inject_assets = options.inject_assets;

    var meta_manager = new meta_factory(process.cwd(), meta_dir);
    var optimizer = new optimizer_factory(meta_manager, config, grunt);
    var router = new router_factory(config.routing);


    var done = this.async();
    var finish = function(stderr,stdout){
      if( stderr != "" ){
        grunt.log.warn( "phantomjs error" );
        grunt.log.warn( stderr );
      } else {
        grunt.log.writeln(stdout);
      }
      if(webserver.stop){
        webserver.stop(function(){
          done();
        });
      }
      else done();
    }

    var target_url = in_request;
    if( in_request.match(/^http/) == null ){
      router.load(function(){

        webserver = new webserver(router,optimizer,meta_manager,grunt, web_server_paths);
        webserver.enable_dashboard(false);
        webserver.enable_build(false);
        webserver.enable_assets_inject(inject_assets);

        webserver.start(port, ssl_port, host);

        in_request = in_request.substring(1)=="/"?in_request:"/"+in_request;
        target_url = "http://"+host+(port?":"+port:port)+in_request;
        grunt.log.ok("Running "+target_url);
        run_confess(target_url, action,finish);
      });

    }else{
      run_confess(in_request,action,finish);
    }
  });

  function run_confess(target_url,action,then){
    var childArgs = [
      __dirname+'/../vendors/confess.js',
      target_url,
      action,
      __dirname+'/../vendors/config.json'
    ]

    childProcess.execFile(phantomjs.path, childArgs, function(err, stdout, stderr) {
      if(then) then(stderr,stdout);
    })
  }
};
@@ -308,247 +308,8 @@
  %7B%0A%0A
-    var webserver           = ph_libutil.webserver;%0A    var router_factory      = ph_libutil.router;%0A    var optimizer_factory   = ph_libutil.optimizer;%0A    var meta_factory        = ph_libutil.meta;%0A%0A    var config = grunt.config.get();%0A%0A
     
@@ -341,27 +341,8 @@
 s(%7B%0A
-      meta_dir:'',%0A
     
@@ -631,45 +631,8 @@
 hs;%0A
-    var meta_dir = options.meta_dir;%0A
     
@@ -803,286 +803,45 @@
 var 
-web_server_log = options.web_server_log;%0A    var inject_assets = options.inject_assets;%0A%0A    var meta_manager = new meta_factory(process.cwd(), meta_dir);%0A    var optimizer = new optimizer_factory(meta_manager, config, grunt);%0A    var router = new router_factory(config.routing)
+inject_assets = options.inject_assets
 ;%0A%0A%0A
@@ -1084,244 +1084,208 @@
     
-if(webserver.stop)%7B%0A        webserver.stop(function()%7B%0A          done();%0A        %7D);%0A      %7D%0A      else done();%0A    %7D%0A%0A    var target_url = in_request;%0A    if( in_request.match(/%5Ehttp/) == null )%7B%0A      router.load(function()%7B%0A%0A        
+done();%0A    %7D%0A%0A    var target_url = in_request;%0A    if( in_request.match(/%5Ehttp/) == null )%7B%0A// get phantomizer main instance%0A      var phantomizer = ph_libutil.get(%22main%22);%0A%0A      phantomizer.create_
 webs
@@ -1293,80 +1293,46 @@
 rver
- = new 
+(
 web
+_
 server
-(router,optimizer,meta_manager,grunt, 
+_paths,function(
 web
-_
 server
-_paths);
+)%7B
 %0A   
@@ -1740,30 +1740,153 @@
 url, action,
+ function(stderr,stdout)%7B%0A          if(webserver.stop)%7B%0A            webserver.stop(function(stderr,stdout)%7B%0A              
 finish
+(
 );%0A      %7D);
@@ -1878,28 +1878,67 @@
 sh();%0A      
+    
+  
 %7D);%0A
+          %7D%0A        %7D);%0A      %7D);
 %0A    %7Delse%7B%0A

Add a new Facade : Phantomizer