'use strict';

/* Requirements  ----------------------------------------------------------------- */
var gulp = require('gulp');
var sass = require('gulp-sass');
var sassGlob = require('gulp-sass-glob');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
var sassdoc = require('sassdoc');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var browserify = require('gulp-browserify');
var copy = require('copy');
var _ = require('underscore');
var urlAdjuster = require('gulp-css-url-adjuster');
var browserSync = require('browser-sync').create();

/* Settings  ----------------------------------------------------------------- */
var sassfiles = './resources/assets/sass/**/*.scss';
var cssoutput = './dist/css/';
var jsfiles = 'resources/assets/js/**/*.js';
var jsoutput = './dist/';

/* Default  ----------------------------------------------------------------- */
gulp.task('default', ['watch']);

/* Build  -----------------------------------------------------------------
 Before releasing or deploying always run build task to make sure all assets
 have been copied and all css has been compiled and included.
*/
gulp.task('build', [
    'sync-resources',
    'sync-css-resources',
    'sass',
    'jshint',
    'build-js'
]);

/* Watch  -----------------------------------------------------------------
 When running watch gulp will automatically watch these folders for changes
 and run the proper tasks when files change.
*/
gulp.task('watch', function() {

    browserSync.init({
        proxy: "dev.local",
        open: false
    });

    gulp.watch(sassfiles, ['sass'])
        .on('change', function(event) {
            console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
        });

    gulp.watch(jsfiles, ['jshint','build-js']).on('change', browserSync.reload);

});

/* SYNC Resources -------------------------------------------------------
 Define all your modules here that you have included with npm. Assets like images will be automatically moved
 to a nice folder and urls will be adjusted automatically for your.
 */
var resource_files = {
    sliderpro: [
        { src: './node_modules/slider-pro/dist/css/images/**', dest: './dist/images/sliderpro'}
    ],
    lightgallery: [
        { src: './node_modules/lightgallery/dist/img/*.{png,jpg,svg,gif}', dest: './dist/images/lightgallery'},
        { src: './node_modules/lightgallery/dist/fonts/*.{eot,svg,ttf,woff}', dest: './dist/fonts/lightgallery'}
    ],
};

var resource_css_files = {
    sliderpro: [
        {   source: './node_modules/slider-pro/dist/css/slider-pro.css',
            replace_path: [
                { from: 'images/', to: '../images/sliderpro/'}
            ]
        }
    ],
    lightgallery: [
        {   source: './node_modules/lightgallery/dist/css/lightgallery.css',
            replace_path: [
                { from: '../img/', to: '../images/lightgallery/'},
                { from: '../fonts/', to: '../fonts/lightgallery/'}
            ]
        }
    ],
};

gulp.task('sync-resources', function() {

    _.each(resource_files, function(resources, key){

        _.each(resources, function(res) {
            copy(res.src, res.dest, function(err, files){});
        });

    });

});

gulp.task('sync-css-resources', function() {

    _.each(resource_css_files, function(files, module_name){
        _.each(files, function(value){

            var gulpAdjuster = gulp.src(value.source);

            if (Array.isArray(value.replace_path)) {
                _.each(value.replace_path, function(replace, key){
                    gulpAdjuster.pipe(urlAdjuster({
                        replace:  [replace.from,replace.to],
                    }));
                });
            }

            gulpAdjuster.pipe(gulp.dest('resources/modules/'+module_name));
        });

    });

});

/* SASS -----------------------------------------------------------------
 Define all your sass files here. Important if you add new modules that have
 css or sass files add them to the includepath over here.
 */
var sassOptions = {
    errLogToConsole: true,
    outputStyle: 'compressed',
    includePaths: [
        './resources/modules/lightgallery',
        './resources/modules/sliderpro',
    ]
};

gulp.task('sass', function () {
    return gulp
        .src(sassfiles)
        .pipe(sourcemaps.init())
        .pipe(sassGlob())
        .pipe(sass(sassOptions).on('error', sass.logError))
        .pipe(sourcemaps.write())
        //.pipe(autoprefixer())
        .pipe(gulp.dest(cssoutput))
});

var sassdocOptions = {
    dest: './public/sassdoc'
};

gulp.task('sassdoc', function () {
    return gulp
        .src(sassfiles)
        .pipe(sassdoc(sassdocOptions))
        .resume();
});

/* Javascript -----------------------------------------------------------------
 This part creates the javascript files for you. Use require (supported because of
 the browserfy plugin) to load the module when needed.
 */
gulp.task('jshint', function() {
    return gulp.src(jsfiles)
        .pipe(jshint())
        .pipe(jshint.reporter('jshint-stylish'));
});

gulp.task('build-js', function() {
    return gulp.src(jsfiles)
        .pipe(sourcemaps.init())
        .pipe(concat('jquery-mediaplayer.min.js'))
        //only uglify if gulp is ran with '--type production'
        .pipe(browserify())
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(jsoutput));
});