'use strict';

/* ============================================================ *\
	SCRIPTS JS / lint, concat and minify scripts
\* ============================================================ */

// Gulp dependencies
var concat     = require('gulp-concat');
var gulpif = require('gulp-if')
var path = require('path');
var sourcemaps = require('gulp-sourcemaps');
var babel = require("gulp-babel");

// Module dependencies
var jshint = require('gulp-jshint');
var jsdoc = require('gulp-jsdoc3');
var stylish = require('jshint-stylish');
var uglify   = require('gulp-uglify');

var helpers = require('./helpers');

module.exports = function(gulp, projectConfig, tasks) {

	/* --------------------
	*	CONFIGURATION
	* ---------------------*/

	var TASK_NAME = 'scripts';

	// Task Config
	var taskConfig = require(path.resolve(process.cwd(), projectConfig.dirs.config, 'task.' + TASK_NAME + '.js'))(projectConfig);

	var scriptTasks = [];

	/* --------------------
	*	MODULE TASKS
	* ---------------------*/

	if(taskConfig.useBabel) {
		helpers.symlinkBabelDirectories();
	}

	Object.keys(taskConfig.files).forEach(function(key) {

		var bundleTaskName = TASK_NAME + ':concat:' + key;
		var lintTaskName = TASK_NAME + ':lint:' + key;
		var docTaskName = TASK_NAME + ':docs:' + key;

		gulp.task(bundleTaskName, function() {
			return gulp.src(taskConfig.files[key])
				.pipe(gulpif(!projectConfig.isProd, sourcemaps.init())) // Default only
				.pipe(gulpif(taskConfig.useBabel, babel()))
				.pipe(concat(key + '.js'))
				.pipe(gulpif(projectConfig.isProd, uglify())) // Production only
				.pipe(gulpif(!projectConfig.isProd, sourcemaps.write('.'))) // Default only
				.pipe(gulp.dest(projectConfig.paths.dest[TASK_NAME]));
		})

		gulp.task(lintTaskName, function() {
			return gulp.src(taskConfig.files[key])
				.pipe(gulpif(!projectConfig.isProd, jshint(taskConfig.jshint))) // Default only
				.pipe(gulpif(!projectConfig.isProd, jshint.reporter(stylish))) // Default only
		})

		gulp.task(docTaskName, function() {
			return gulp.src(taskConfig.files[key])
				.pipe(gulpif(!projectConfig.isProd, jsdoc(taskConfig.docs))); // Default only
		})

		scriptTasks.push(bundleTaskName, lintTaskName, docTaskName);
	});

	// gulp.task(TASK_NAME + ':bundle', [ TASK_NAME + ':ensure-babel-symlink'], function () {
		// return gulp.src(taskConfig.src)
		// 	.pipe(gulpif(!projectConfig.isProd, sourcemaps.init())) // Default only
		// 	.pipe(gulpif(taskConfig.useBabel, babel()))
		// 	.pipe(concat(taskConfig.bundle))
		// 	.pipe(gulpif(projectConfig.isProd, uglify())) // Production only
		// 	.pipe(gulpif(!projectConfig.isProd, sourcemaps.write('.'))) // Default only
		// 	.pipe(gulp.dest(projectConfig.paths.dest[TASK_NAME]));
	// });
	//
	// gulp.task(TASK_NAME + ':lint', function () {
		// return gulp.src(taskConfig.src)
		// 	.pipe(gulpif(!projectConfig.isProd, jshint(taskConfig.jshint))) // Default only
		// 	.pipe(gulpif(!projectConfig.isProd, jshint.reporter(stylish))) // Default only
	// });
	//
	// gulp.task(TASK_NAME + ':docs', function () {
		// return gulp.src(taskConfig.src)
		// 	.pipe(gulpif(!projectConfig.isProd, jsdoc(taskConfig.docs))); // Default only
	// });
	//
	// gulp.task(TASK_NAME + ':ensure-babel-symlink', function (done) {
		// if(taskConfig.useBabel) {
		// 	helpers.symlinkBabelDirectories();
		// }
	//
	// 	done();
	// });


	gulp.task(TASK_NAME, scriptTasks);

	/* --------------------
	*	WATCH TASKS
	* ---------------------*/

	gulp.task('watch:' + TASK_NAME, function () {
		gulp.watch(
			taskConfig.watch,
			[TASK_NAME]
		);
	});

	/* ----------------------------
	*	CARTRIDGE TASK MANAGEMENT
	* -----------------------------*/

	// Add the clean path for the generated scripts
	projectConfig.cleanPaths.push(projectConfig.paths.dest[TASK_NAME]);
	// Add the task to the default list
	tasks.default.push(TASK_NAME);
	// Add the task to the watch list
	tasks.watch.push('watch:' + TASK_NAME);
}

@@ -1114,16 +1114,30 @@
 function
+ generateTasks
 (key) %7B%0A
@@ -2229,1161 +2229,15 @@
 e);%0A
+%0A
 %09%7D);%0A%0A
-%09// gulp.task(TASK_NAME + ':bundle', %5B TASK_NAME + ':ensure-babel-symlink'%5D, function () %7B%0A%09%09// return gulp.src(taskConfig.src)%0A%09%09// %09.pipe(gulpif(!projectConfig.isProd, sourcemaps.init())) // Default only%0A%09%09// %09.pipe(gulpif(taskConfig.useBabel, babel()))%0A%09%09// %09.pipe(concat(taskConfig.bundle))%0A%09%09// %09.pipe(gulpif(projectConfig.isProd, uglify())) // Production only%0A%09%09// %09.pipe(gulpif(!projectConfig.isProd, sourcemaps.write('.'))) // Default only%0A%09%09// %09.pipe(gulp.dest(projectConfig.paths.dest%5BTASK_NAME%5D));%0A%09// %7D);%0A%09//%0A%09// gulp.task(TASK_NAME + ':lint', function () %7B%0A%09%09// return gulp.src(taskConfig.src)%0A%09%09// %09.pipe(gulpif(!projectConfig.isProd, jshint(taskConfig.jshint))) // Default only%0A%09%09// %09.pipe(gulpif(!projectConfig.isProd, jshint.reporter(stylish))) // Default only%0A%09// %7D);%0A%09//%0A%09// gulp.task(TASK_NAME + ':docs', function () %7B%0A%09%09// return gulp.src(taskConfig.src)%0A%09%09// %09.pipe(gulpif(!projectConfig.isProd, jsdoc(taskConfig.docs))); // Default only%0A%09// %7D);%0A%09//%0A%09// gulp.task(TASK_NAME + ':ensure-babel-symlink', function (done) %7B%0A%09%09// if(taskConfig.useBabel) %7B%0A%09%09// %09helpers.symlinkBabelDirectories();%0A%09%09// %7D%0A%09//%0A%09// %09done();%0A%09// %7D);%0A%0A%0A
 %09gul

Remove comment, name anom function