/*!
 * @author:    Divio AG
 * @copyright: http://www.divio.ch
 */

'use strict';

// #############################################################################
// #IMPORTS#
var browserSync = require('browser-sync');
var cache = require('gulp-cached');
var gulp = require('gulp');
var gutil = require('gulp-util');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var jscs = require('gulp-jscs');
var karma = require('karma').server;
var protractor = require('gulp-protractor').protractor;
var scsslint = require('gulp-scss-lint');
var webdriverUpdate = require('gulp-protractor').webdriver_update;
var yuidoc = require('gulp-yuidoc');

// #############################################################################
// #SETTINGS#
var PROJECT_ROOT = __dirname;
var PROJECT_PATH = {
    'css': PROJECT_ROOT + '/static/css/',
    'docs': PROJECT_ROOT + '/static/docs',
    'html': PROJECT_ROOT + '/templates/',
    'images': PROJECT_ROOT + '/static/img/',
    'js': PROJECT_ROOT + '/static/js/',
    'sass': PROJECT_ROOT + '/private/sass/',
    'tests': PROJECT_ROOT + '/tests'
};

var PROJECT_PATTERNS = {
    'images': [
        PROJECT_PATH.images + '*',
        PROJECT_PATH.images + '**/*',
        '!' + PROJECT_PATH.images + 'dummy/*/**'
    ],
    'js': [
        PROJECT_PATH.js + '*.js',
        PROJECT_PATH.js + '**/*.js',
        PROJECT_PATH.tests + '*.js',
        PROJECT_PATH.tests + '*/**.js',
        '!' + PROJECT_PATH.js + '*.min.js',
        '!' + PROJECT_PATH.js + '**/*.min.js'
    ],
    'sass': [
        PROJECT_PATH.sass + '*',
        PROJECT_PATH.sass + '**/*',
        '!' + PROJECT_PATH.sass + 'libs/*',
        '!' + PROJECT_PATH.sass + 'settings/*',
        '!' + PROJECT_PATH.sass + 'layout/_print.{scss,sass}'
    ]
};

var PORT = parseInt(process.env.PORT, 10) || 8000;

// #############################################################################
// #LINTING#
gulp.task('lint', ['lint:javascript', 'lint:sass']);

gulp.task('lint:javascript', function () {
    // DOCS: http://jshint.com/docs/
    return gulp.src(PROJECT_PATTERNS.js)
        .pipe(jshint())
        .pipe(jscs())
        .on('error', function (error) {
            gutil.log('\n' + error.message);
        })
        .pipe(jshint.reporter('jshint-stylish'));
});

gulp.task('lint:sass', function () {
    // DOCS: https://github.com/brigade/scss-lint/
    return gulp.src(PROJECT_PATTERNS.sass)
        .pipe(cache('scsslint'))
        .pipe(scsslint({
            'config': './scss-lint.json'
        }));
});

// #######################################
// #PREPROCESSING#
gulp.task('preprocess', ['images', 'docs']);

gulp.task('images', function () {
    var options = {
        'interlaced': true,
        'optimizationLevel': 5,
        'progressive': true
    };

    gulp.src(PROJECT_PATTERNS.images)
        .pipe(cache(imagemin(options)))
        .pipe(gulp.dest(PROJECT_PATH.images)).on('error', function (error) {
            gutil.log('\n' + error.message);
        });
});

gulp.task('docs', function () {
    gulp.src(PROJECT_PATTERNS.js)
        .pipe(yuidoc())
        .pipe(gulp.dest(PROJECT_PATH.docs));
});

// #######################################
// #SERVICES#
gulp.task('browser', function () {
    var files = [
        PROJECT_PATH.css + '*.css',
        PROJECT_PATH.html + '**/*.html',
        PROJECT_PATH.js + '**/*.js'
    ];

    // DOCS: http://www.browsersync.io/docs/options/
    setTimeout(function () {
        browserSync.init(files, {
            'proxy': '0.0.0.0:' + PORT,
            'port': PORT + 1,
            'ui': {
                'port': PORT + 2
            }
        });
    }, 1000);
});

// #######################################
// #TESTS#
gulp.task('tests', ['tests:unit', 'tests:integration', 'tests:lint']);
gulp.task('tests:unit', function (done) {
    // run javascript tests
    karma.start({
        'configFile': PROJECT_PATH.tests + '/karma.conf.js',
        'singleRun': true
    }, done);
});

gulp.task('tests:webdriver', webdriverUpdate);
gulp.task('tests:integration', ['tests:webdriver'], function () {
    return gulp.src([PROJECT_PATH.tests + '/integration/*.js'])
        .pipe(protractor({
            'configFile': PROJECT_PATH.tests + '/protractor.conf.js',
            'args': []
        }))
        .on('error', function (error) {
            gutil.log(gutil.colors.red(
                'Error (' + error.plugin + '): ' + error.message
            ));
        });
});

gulp.task('tests:lint', ['lint']);

gulp.task('tests:watch', ['tests:lint'], function () {
    // run javascript tests
    karma.start({
        'configFile': PROJECT_PATH.tests + '/karma.conf.js'
    });
});

// #############################################################################
// #COMMANDS#
gulp.task('watch', function () {
    gulp.watch(PROJECT_PATTERNS.js, ['lint']);
});

gulp.task('default', ['browser', 'lint', 'watch']);

@@ -2503,16 +2503,14 @@
     
-'
 config
-'
 : '.
@@ -2714,17 +2714,16 @@
         
-'
 interlac
@@ -2724,17 +2724,16 @@
 terlaced
-'
 : true,%0A
@@ -2740,17 +2740,16 @@
         
-'
 optimiza
@@ -2757,17 +2757,16 @@
 ionLevel
-'
 : 5,%0A   
@@ -2770,17 +2770,16 @@
         
-'
 progress
@@ -2781,17 +2781,16 @@
 gressive
-'
 : true%0A 
@@ -3516,15 +3516,13 @@
     
-'
 proxy
-'
 : '0
@@ -3542,38 +3542,36 @@
 RT,%0A            
-'
 port
-'
 : PORT + 1,%0A    
@@ -3582,12 +3582,10 @@
     
-'
 ui
-'
 : %7B%0A
@@ -3875,33 +3875,32 @@
 start(%7B%0A        
-'
 configFile': PRO
@@ -3885,33 +3885,32 @@
       configFile
-'
 : PROJECT_PATH.t
@@ -3942,17 +3942,16 @@
         
-'
 singleRu
@@ -3951,17 +3951,16 @@
 ingleRun
-'
 : true%0A 
@@ -4185,33 +4185,32 @@
 r(%7B%0A            
-'
 configFile': PRO
@@ -4195,33 +4195,32 @@
       configFile
-'
 : PROJECT_PATH.t
@@ -4265,14 +4265,12 @@
     
-'
 args
-'
 : %5B%5D
@@ -4605,17 +4605,16 @@
         
-'
 configFi
@@ -4615,17 +4615,16 @@
 nfigFile
-'
 : PROJEC

remove quotes