'use strict';

// Load plugins
var gulp = require('gulp'),
    sass = require('gulp-ruby-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    cssnano = require('gulp-cssnano'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
    del = require('del'),
    htmlmin = require('gulp-htmlmin'),
    imageResize = require('gulp-image-resize'),
    critical = require('critical');

// Styles
gulp.task('styles', function() {
    return sass('src/styles/*.scss', {
            style: 'expanded'
        })
        .pipe(autoprefixer('last 2 version'))
        .pipe(gulp.dest('dist/styles'))
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(cssnano())
        .pipe(gulp.dest('dist/styles'))
        .pipe(notify({
            message: 'Styles task complete'
        }));
});

// Scripts
gulp.task('scripts', function() {
    return gulp.src(['src/scripts/mapmanager.js',
        'src/scripts/utilities.js',
        'src/scripts/mapmanger.js',
        'src/scripts/attributes.js',
        'src/scripts/markers.js',
        'src/scripts/twitter.js',
        'src/scripts/filter.js',
        'src/scripts/glow.js',
        'src/scripts/sort.js',
        'src/scripts/divs.js',
        'src/scripts/start.js'])
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(concat('main.js'))
        .pipe(gulp.dest('dist/scripts'))
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(uglify())
        .pipe(gulp.dest('dist/scripts'))
        .pipe(notify({
            message: 'Scripts task complete'
        }));
});

// temp task used to concatenate jQuery and Knockout
// Scripts
// gulp.task('perma', function() {
//     return gulp.src(['dist/perm/jquery-1.12.0.min.js',
//         'dist/perm/knockout-3.4.0.js'])
//         .pipe(concat('jquery_and_knockout.js'))
//         .pipe(gulp.dest('dist/perm/production'))
//         .pipe(rename({
//             suffix: '.min'
//         }))
//         .pipe(uglify())
//         .pipe(gulp.dest('dist/perm/production'))
//         .pipe(notify({
//             message: 'Perma task complete'
//         }));
// });
// 
// Temp task used to minify the cssLoading script
// gulp.task('minime', function() {
//     return gulp.src(['dist/perm/production/cssLoading.js'])
//         .pipe(rename({
//             suffix: '.min'
//         }))
//         .pipe(uglify())
//         .pipe(gulp.dest('dist/perm/production'))
//         .pipe(notify({
//             message: 'minature task complete'
//         }));
// });


// Critical CSS
gulp.task('critical', function () {
    critical.generate({
        inline: true,
        base: '.',
        src: 'index.html',
        minify: true,
        css: ['dist/styles/main.min.css'],
        width: 1900,
        height: 1300,
        dest: 'index.html',
    });
});


// Clean images
gulp.task('clean-images', function() {
    return del(['dist/images']);
});

// Resize images
gulp.task('images', function() {
    gulp.src('src/images/*tab*.png')
        .pipe(imageResize({
            width: 40
        }))
        .pipe(gulp.dest('dist/images'))
        .pipe(notify({
            message: 'Resize (no minification) task complete'
        }));
});

// Clean
gulp.task('clean', function() {
    return del(['dist/styles', 'dist/scripts', './index.html']);
});


// Default
gulp.task('default', ['clean'], function() {
    gulp.start('styles', 'scripts', 'images', 'minify-html');
});

// Minify HTML
gulp.task('minify-html', function() {
    return gulp.src('src/*.html')
        .pipe(htmlmin({
            collapseWhitespace: true
        }))
        .pipe(gulp.dest('.'));
});

// Lint gulpfile.js
gulp.task('gulpfile-lint', function() {
    return gulp.src('gulpfile.js')
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(notify({
            message: 'Gulpfile-lint task complete'
        }));
});

// Watch
gulp.task('watch', function() {

    // Watch .scss files
    gulp.watch('src/styles/**/*.scss', ['styles']);

    // Watch .js files
    gulp.watch('src/scripts/**/*.js', ['scripts']);

    // Watch image files
    gulp.watch('src/images/**/*', ['images']);

    // Watch src/index.html
    gulp.watch('src/*.html', ['minify-html']);

    // Watch this file
    gulp.watch('gulpfile.js', ['gulpfile-lint']);
});

@@ -2717,16 +2717,508 @@
 itical',
+ %5B'styles', 'scripts', 'images', 'minify-html'%5D, function () %7B%0A    critical.generate(%7B%0A        inline: true,%0A        base: '.',%0A        src: 'index.html',%0A        minify: true,%0A        css: %5B'dist/styles/main.min.css'%5D,%0A        width: 1900,%0A        height: 1300,%0A        dest: 'index.html',%0A    %7D);%0A%7D);%0A%0A// Critical CSS that is used by watch, responds to changes in any scss files,%0A// requires that a minified HTML file is already present in root dir.%0Agulp.task('critical-styles', %5B'styles'%5D,
  functio
@@ -3965,16 +3965,20 @@
 %7D);%0A%0A%0A//
+ old
  Default
@@ -3978,16 +3978,19 @@
 Default%0A
+// 
 gulp.tas
@@ -4018,32 +4018,35 @@
 %5D, function() %7B%0A
+// 
     gulp.start('
@@ -4091,16 +4091,97 @@
 html');%0A
+// %7D);%0A%0A// Default%0Agulp.task('default', function() %7B%0A    gulp.start('critical');%0A
 %7D);%0A%0A// 
@@ -4741,24 +4741,33 @@
 /*.scss', %5B'
+critical-
 styles'%5D);%0A%0A

Update gulpfile to watch for and handle critical css