'use strict';
var _fs = require('fs');
var _path = require('path');

var async = require('async');
var webpack = require('webpack');

var utils = require('./utils');
var webpackConfig = require('../config/build');

var cwd = process.cwd();

module.exports = function(o, cb) {
  if(o.task === 'copy_assets') {
    utils.copyIfExists.apply(null, o.params.concat([cb]));
  }
  else if(o.task === 'write_main') {
    writeMain(o.params, cb);
  }
  else if(o.task === 'copy_extra_assets') {
    utils.copyExtraAssets.apply(null, o.params.concat([cb]));
  }
  else if(o.task === 'write') {
    write(o.params, cb);
  }
  else if(o.task === 'write_pages') {
    writePages(o.params, cb);
  }
  else {
    cb();
  }
};

function writeMain(params, cb) {
  var assetsDir = params.assetsDir;
  var mainPath = params.mainPath;

  _fs.exists(mainPath, function(exists) {
    if(!exists) {
      return cb();
    }

    _fs.readFile(mainPath, function(err, data) {
      if(err) {
        return cb(err);
      }

      _fs.writeFile(_path.join(assetsDir, 'main.css'), data, cb);
    });
  });
}

function writePages(params, cb) {
  var cwd = process.cwd();

  try {
    var config = require(_path.join(cwd, 'antwar.config.js'));
  }
  catch(err) {
    return cb(err);
  }

  webpackConfig(config).then(function(c) {
    webpack(c, function(err) {
      if(err) {
        return cb(err);
      }

      // XXXXX: figure out why this path might not exist
      var renderPage = require(_path.join(cwd, './.antwar/build/bundleStaticPage.js'));

      async.each(params.pages, function(page, cb) {
        // XXX: why page can be null?
        if(page) {
          // TODO: use user defined logger instead
          console.log('Starting to write page', page.page);

          renderPage(page.page, function(err, html) {
            if(err) {
              return cb(err);
            }

            write({
              path: page.path,
              data: html
            }, function(err) {
              if(err) {
                return cb(err);
              }

              // TODO: use user defined logger instead
              console.log('Finished writing page', page.page);

              cb();
            });
          });
        }
        else {
          cb();
        }
      }, cb);
    });
  }).catch(cb);
}

function write(params, cb) {
  _fs.writeFile(params.path, params.data, cb);
}

@@ -162,56 +162,8 @@
 s');
-%0Avar webpackConfig = require('../config/build');
 %0A%0Ava
@@ -1093,308 +1093,8 @@
 ();%0A
-%0A  try %7B%0A    var config = require(_path.join(cwd, 'antwar.config.js'));%0A  %7D%0A  catch(err) %7B%0A    return cb(err);%0A  %7D%0A%0A  webpackConfig(config).then(function(c) %7B%0A    webpack(c, function(err) %7B%0A      if(err) %7B%0A        return cb(err);%0A      %7D%0A%0A      // XXXXX: figure out why this path might not exist%0A    
   va
@@ -1174,20 +1174,16 @@
 js'));%0A%0A
-    
   async.
@@ -1218,28 +1218,24 @@
 page, cb) %7B%0A
-    
     // XXX: 
@@ -1256,20 +1256,16 @@
 e null?%0A
-    
     if(p
@@ -1273,28 +1273,24 @@
 ge) %7B%0A      
-   
- 
 // TODO: use
@@ -1314,28 +1314,24 @@
 ger instead%0A
-    
       consol
@@ -1381,20 +1381,16 @@
 %0A%0A      
- 
-   
 renderPa
@@ -1425,36 +1425,32 @@
 html) %7B%0A        
-    
 if(err) %7B%0A      
@@ -1435,36 +1435,32 @@
       if(err) %7B%0A
-    
           return
@@ -1473,35 +1473,27 @@
 r);%0A        
-   
- 
 %7D%0A%0A
-    
         writ
@@ -1506,20 +1506,16 @@
         
-  
-  
 path: pa
@@ -1523,20 +1523,16 @@
 e.path,%0A
-    
         
@@ -1552,20 +1552,16 @@
         
-    
 %7D, funct
@@ -1577,28 +1577,24 @@
 %7B%0A          
-   
- 
 if(err) %7B%0A  
@@ -1587,28 +1587,24 @@
   if(err) %7B%0A
-    
             
@@ -1625,35 +1625,27 @@
 ;%0A          
-  
-  
 %7D%0A%0A
-    
           //
@@ -1693,20 +1693,16 @@
         
- 
-   
 console.
@@ -1749,28 +1749,24 @@
 %0A%0A          
-   
- 
 cb();%0A      
@@ -1763,28 +1763,24 @@
 ();%0A        
-    
 %7D);%0A        
@@ -1773,36 +1773,32 @@
   %7D);%0A      
-  
-  
 %7D);%0A    
     %7D%0A      
@@ -1789,33 +1789,21 @@
     
-  
-  
 %7D%0A    
- 
-   
 else %7B%0A
-    
     
@@ -1818,47 +1818,15 @@
     
-    %7D%0A      %7D, cb);%0A    %7D);%0A  %7D).catch(
+%7D%0A  %7D, 
 cb);

Drop Webpack bits from the writePages worker