'use strict';

var gulp = require('gulp'),
    plumber = require('gulp-plumber'),
    colors = require('gulp-util'),
    size = require('gulp-size'),
    del = require('del'),
    sass = require('gulp-sass'),
    browserify = require('browserify'),
    watchify = require('watchify'),
    babelify = require('babelify'),
    buffer = require('vinyl-buffer'),
    source = require('vinyl-source-stream'),
    packageJson = require('./package.json'),
    globals = Object.keys(packageJson.dependencies);

gulp.task('clean', function (callback) {
    del('./dist/');
});

gulp.task('build:css', function () {
    gulp
        .src('./src/sass/styles.scss')
        .pipe(plumber())
        .pipe(sass())
        .pipe(gulp.dest('./dist/css/'))
        .pipe(buffer())
        .pipe(size());
});

gulp.task('build:js', function () {
    browserify('./src/jsx/index.jsx')
        .external(globals)
        .bundle()
        .on('error', colors.log)
        .pipe(source('scripts.js'))
        .pipe(gulp.dest('./dist/js/'))
        .pipe(buffer())
        .pipe(size());
});

gulp.task('copy:html', function () {
    gulp
        .src('./src/index.html')
        .pipe(gulp.dest('./dist/'));
});

gulp.task('copy:img', function () {
    gulp
        .src('./src/static/img/**', {
            base: './src/static/'
        })
        .pipe(gulp.dest('./dist/'));
});

gulp.task('copy:fonts', function () {
    gulp
        .src('./src/static/fonts/**', {
            base: './src/static/'
        })
        .pipe(gulp.dest('./dist/'));
});

gulp.task('copy:css', function () {
    gulp
        .src('./bower_components/materialize/dist/css/materialize.css')
        .pipe(gulp.dest('./dist/css/'));
});

gulp.task('copy:js', function () {
    gulp
        .src([
            './bower_components/react/react-with-addons.js',
            './bower_components/jquery/dist/jquery.js',
            './bower_components/materialize/dist/js/materialize.js'
        ])
        .pipe(gulp.dest('./dist/js/'));
});

gulp.task('copy:fonts', function () {
    gulp
        .src('./bower_components/materialize/dist/font/**', {
            base: './bower_components/materialize/dist/font/'
        })
        .pipe(gulp.dest('./dist/font/'));
});

gulp.task('watch:html', function () {
    gulp.watch('./src/index.html', ['copy:html']);
});

gulp.task('watch:css', function () {
    gulp.watch('./src/static/sass/styles.scss', ['build:css']);
});

gulp.task('watch:js', function () {
    var rebundler = function rebundler(watch) {
        var bundler = watchify(browserify({
            entries: './src/jsx/index.jsx'
        }).external(globals));

        var rebundle = function rebundle() {
            bundler
                .bundle()
                .on('error', function (error) {
                    colors.log(error);
                    this.emit('end');
                })
                .pipe(source('scripts.js'))
                .pipe(gulp.dest('./dist/js/'))
                .pipe(buffer())
                .pipe(size());
        };

        if (watch) {
            bundler.on('update', function () {
                rebundle();
                colors.log(colors.colors.green('Re-bundling complete!'));
            });
        }

        rebundle();
    };

    rebundler(true);
});

gulp.task('build', [
    'build:css',
    'build:js'
]);

gulp.task('copy', [
    'copy:html',
    'copy:img',
    'copy:css',
    'copy:js',
    'copy:fonts'
]);

gulp.task('watch', [
    'watch:html',
    'watch:css',
    'watch:js'
]);

gulp.task('default', [
    'build',
    'watch',
    'copy'
]);
