var gulpif = require('gulp-if');
var path = require('path');
var concat = require('gulp-concat');

module.exports = {};

module.exports.clean = function(gulp, builder) {
  var rimraf = require('rimraf');

  gulp.task('clean', function (cb) {
    rimraf(builder.config.dest, function() {
      rimraf(builder.config.tmp+"/javascript", cb);
    });
  });
};

module.exports.fonts = function(gulp, builder) {
  gulp.task('fonts', ['clean'], function() {
    return builder.run('fonts')
      .pipe(gulp.dest(builder.config.dest+'/fonts'));
  });
};

module.exports.css = function(gulp, builder, taskConfig) {
  gulp.task('css', ['clean'], function() {
    return builder.run('css')
      .pipe(gulpif(taskConfig.concat, concat(taskConfig.target || 'app.css')))
      .pipe(gulp.dest(builder.config.dest+'/css'));
  });
};

module.exports['requirejs-config'] = function(gulp, builder, taskConfig) {
  var modulePath = path.resolve(builder.resolveModule('requirejs'), '..');

  gulp.task('requirejs-config', ['clean', 'requirejs'], function() {
    return gulp.src([taskConfig.file, modulePath+'/require.js'])
      .pipe(concat('load-require.js'))
      .pipe(gulp.dest(builder.config.dest+'/js'))
  });
};

module.exports.javascript = function(gulp, builder, taskConfig) {
  if (taskConfig && taskConfig.combine) {
    var requireConfig = taskConfig.requirejs;

    requireConfig.appDir = builder.config.tmp+"/javascript"; // optimize this
    requireConfig.dir = builder.config.dest+'/js'; // into this

    if (!requireConfig.hasOwnProperty('baseUrl')) {
      requireConfig.baseUrl = './'; // everything is rooted in appDir (thats our convention)
    }

    requireConfig.keepBuildDir = false;  // start with empty dir

    if (!requireConfig.hasOwnProperty('removeCombined')) {
      // we can remove the optimized files, when we load (from html) always the module "main". Because this will contain all others. 
      //If you have other entry-points in html you have to add this entry point to modules!
      requireConfig.removeCombined = true;
    }

    if (!requireConfig.hasOwnProperty('findNestedDependencies')) {
      requireConfig.findNestedDependencies = true;
    }

    if (!requireConfig.hasOwnProperty('optimizeCss')) {
      requireConfig.optimizeCss = false;
    }

    requireConfig.optimize = "uglify2";
    requireConfig.uglify2 =  {
      output: {
        beautify: false
      },
      compress: {
        sequences: false
      },
      warnings: false,
      mangle: true
    };
      
    requireConfig.skipDirOptimize =  true;
    requireConfig.optimizeCss = "none";

    // pipe all files to www/assets/js directory and write them
    // because js files come from whole different locations (like node_modules, lib/, etc...)
    gulp.task('javascript-files', ['clean'], function(done) {
      return builder.run('js')
        .pipe(gulp.dest(requireConfig.appDir));
    });

    gulp.task('javascript', ['requirejs-config'], function() {
      // requirejs-config calls requirejs (so that the config is not removed from r.js optimizer)
    });

    // get those (hopfully written) files and optimize them
    gulp.task('requirejs', ['javascript-files'], function(done) {
      var requirejs = builder.require('requirejs');

      requirejs.optimize(requireConfig, function (buildResponse) {
        done();
      }, function(err) {
        done(err);
      });
    });

  } else if (taskConfig && taskConfig.concat) {
    gulp.task('requirejs-config', function() {
      // no requirejs-config when concat is not activated
    });

    gulp.task('javascript', ['clean', 'requirejs-config'], function() {
      return builder.run('js')
        .pipe(concat(taskConfig.target || 'app.js'))
        .pipe(gulp.dest(builder.config.dest+'/js'));
    });

  } else {
    gulp.task('requirejs', function() {
      // nothing todo if combine is not activated
      // but requirejs-config requires this task so we have to provide it!
    });

    gulp.task('javascript', ['clean', 'requirejs-config'], function() {
      return builder.run('js')
        .pipe(gulp.dest(builder.config.dest+'/js'));
    });
  }
};

module.exports.less = function(gulp, builder, taskConfig) {
  var less = require('gulp-less');
  var path = require('path');

  var includePaths = taskConfig.includes.map(function(includePath) {
    return path.join( builder.config.root, includePath )
  });

  gulp.task('less', ['clean'], function () {
    return gulp.src(taskConfig.src)
      .pipe(less({
         paths: includePaths,
         compress: !builder.config.dev
      }))
      .pipe(gulp.dest(builder.config.dest+'/css'));
  });

};

module.exports.templates = function(gulp, builder, taskConfig) {
  var addsrc = require('gulp-add-src');
  var hogan = require('gulp-hogan-compile');

  gulp.task('templates', ['clean'], function() {
    var path = require('path');
    return gulp.src(taskConfig.path+'/**/*.mustache' || 'Resources/tpl/**/*.mustache')
      .pipe(addsrc('node_modules/webforge-js-components/Resources/tpl/**'))
      .pipe(
        hogan('templates-compiled.js', {
          templateName: function(file) {
            return file.relative.replace(/\\/g, '/').slice(0, -".mustache".length);
          },
        })
      )
      .pipe(gulp.dest(builder.config.dest+'/js'));
  });
};

@@ -2317,16 +2317,70 @@
 lify2%22;%0A
+    if (!requireConfig.hasOwnProperty('uglify2')) %7B%0A  
     requ
@@ -2402,16 +2402,18 @@
 y2 =  %7B%0A
+  
       ou
@@ -2416,24 +2416,26 @@
   output: %7B%0A
+  
         beau
@@ -2448,27 +2448,31 @@
 false%0A      
+  
 %7D,%0A
+  
       compre
@@ -2485,16 +2485,18 @@
         
+  
 sequence
@@ -2500,24 +2500,26 @@
 nces: false%0A
+  
       %7D,%0A   
@@ -2521,16 +2521,18 @@
 ,%0A      
+  
 warnings
@@ -2540,16 +2540,18 @@
  false,%0A
+  
       ma
@@ -2565,18 +2565,26 @@
 rue%0A    
+  %7D;%0A    
 %7D
-;
 %0A      %0A
@@ -2583,24 +2583,29 @@
 %0A      %0A    
+if (!
 requireConfi
@@ -2606,16 +2606,32 @@
 eConfig.
+hasOwnProperty('
 skipDirO
@@ -2641,18 +2641,16 @@
 mize
- =  true;%0A
+')) %7B%0A  
     
@@ -2667,29 +2667,39 @@
 fig.
-optimizeCss = %22none%22;
+skipDirOptimize =  false;%0A    %7D
 %0A%0A  

make uglify optional