// Include gulp
var $ = require('gulp-load-plugins')();
var gulp = require('gulp');
var sequence = require('run-sequence');
var del = require('del');

// Postcss plugins
var autoprefixer = require('autoprefixer');
var base64 = require('postcss-base64');
var focusWithin = require('postcss-focus-within');

function swallowError() {
  console.log('UGLIFY ERROR');
  this.emit('end');
}

/**
 * File paths to various assets are defined here.
 */
var PATHS = {
  sass: [
    'assets/scss/*.scss',
    'assets/scss/**/*.scss'
  ],
  jsVendor: [
    'bower_components/fluidvids/dist/fluidvids.js',
    'bower_components/slick-carousel/slick/slick.js',
    'bower_components/selectize/dist/js/standalone/selectize.js',
    'bower_components/baguettebox.js/dist/baguetteBox.min.js',
    'bower_components/tingle/dist/tingle.js',
    'bower_components/imagesloaded/imagesloaded.pkgd.js',
    'bower_components/waypoints/lib/jquery.waypoints.js',
    'bower_components/waypoints/lib/shortcuts/sticky.js',
    'bower_components/masonry-layout/dist/masonry.pkgd.js',
    'bower_components/jarallax/dist/jarallax.js',
    'bower_components/jarallax/dist/jarallax-video.js',
    'bower_components/gsap/src/minified/TweenMax.min.js',
    'bower_components/scrollmagic/scrollmagic/minified/ScrollMagic.min.js',
    'bower_components/scrollmagic/scrollmagic/minified/plugins/jquery.ScrollMagic.js',
    'bower_components/scrollmagic/scrollmagic/minified/plugins/animation.gsap.min.js',
    'assets/js/vendor/*.min.js',
  ],
  jsTheme: [
    'assets/js/theme/*.js',
    'assets/js/theme.js'
  ],
  jsAdmin: [
    'bower_components/tingle/dist/tingle.js',
    'assets/js/admin/*.js',
    'assets/js/admin.js'
  ]
};

// Concatenate & Minify all bower dependency javascript files
gulp.task('build:scripts:vendor', function () {
  return gulp.src(PATHS.jsVendor)
    .pipe($.concat('vendor.js'))
    .pipe($.rename({
      suffix: '.min'
    }))
    .pipe($.uglify().on('error', swallowError))
    .pipe(gulp.dest('build/js'));
});

// Concatenate & Minify all theme javascript files
gulp.task('dev:scripts:vendor', function () {
  return gulp.src(PATHS.jsVendor)
    .pipe($.concat('vendor.js'))
    .pipe($.rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('build/js'));
});

// Concatenate & Minify all theme javascript files
gulp.task('build:scripts:theme', function () {
  return gulp.src(PATHS.jsTheme)
    .pipe($.concat('theme.js'))
    .pipe($.rename({
      suffix: '.min'
    }))
    .pipe($.uglify().on('error', swallowError))
    .pipe(gulp.dest('build/js'));
});

// Concatenate & Minify all theme javascript files
gulp.task('dev:scripts:theme', function () {
  return gulp.src(PATHS.jsAdmin)
    .pipe($.concat('theme.js'))
    .pipe($.rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('build/js'));
});

// Concatenate & Minify all admin javascript files
gulp.task('build:scripts:admin', function () {
  return gulp.src(PATHS.jsAdmin)
    .pipe($.concat('admin.js'))
    .pipe($.rename({
      suffix: '.min'
    }))
    .pipe($.uglify().on('error', swallowError))
    .pipe(gulp.dest('build/js'));
});

// Concatenate & Minify all admin javascript files
gulp.task('dev:scripts:admin', function () {
  return gulp.src(PATHS.jsTheme)
    .pipe($.concat('admin.js'))
    .pipe($.rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('build/js'));
});

// Compile min CSS
gulp.task('build:styles', function () {
  return gulp.src('assets/scss/main.scss')
    .pipe($.sourcemaps.init())
    .pipe($.sass({
      includePaths: PATHS.sass,
      outputStyle: 'compressed'
    })
      .on('error', $.sass.logError))
    .pipe($.postcss([
      autoprefixer(),
      base64({
        pattern: /<svg.*<\/svg>/i,
        prepend: 'data:image/svg+xml;base64,'
      }),
      focusWithin()
    ]))
    .pipe($.rename({
      basename: "theme",
      suffix: '.min'
    }))
    .pipe($.sourcemaps.write('.'))
    .pipe(gulp.dest('build/css'))
});

// Compile uncompressed CSS
gulp.task('dev:styles', function () {
  return gulp.src('assets/scss/main.scss')
    .pipe($.sass({
      includePaths: PATHS.sass,
      outputStyle: 'expanded',
      indentType: 'tab',
      indentWidth: 1
    })
      .on('error', $.sass.logError))
    .pipe($.postcss([
      autoprefixer(),
      base64({
        pattern: /<svg.*<\/svg>/i,
        prepend: 'data:image/svg+xml;base64,'
      }),
      focusWithin()
    ]))
    .pipe($.rename({
      basename: "theme",
      suffix: '.min'
    }))
    .pipe(gulp.dest('build/css'))
});

// Compile min CSS
gulp.task('build:admin:styles', function () {
  return gulp.src('assets/scss/admin.scss')
    .pipe($.sourcemaps.init())
    .pipe($.sass({
      includePaths: PATHS.sass,
      outputStyle: 'compressed'
    })
      .on('error', $.sass.logError))
    .pipe($.postcss([
      autoprefixer(),
      base64({
        pattern: /<svg.*<\/svg>/i,
        prepend: 'data:image/svg+xml;base64,'
      }),
      focusWithin()
    ]))
    .pipe($.rename({
      basename: "admin",
      suffix: '.min'
    }))
    .pipe($.sourcemaps.write('.'))
    .pipe(gulp.dest('build/css'))
});

// Compile uncompressed CSS
gulp.task('dev:admin:styles', function () {
  return gulp.src('assets/scss/admin.scss')
    .pipe($.sass({
      includePaths: PATHS.sass,
      outputStyle: 'expanded',
      indentType: 'tab',
      indentWidth: 1
    })
      .on('error', $.sass.logError))
    .pipe($.postcss([
      autoprefixer(),
      base64({
        pattern: /<svg.*<\/svg>/i,
        prepend: 'data:image/svg+xml;base64,'
      }),
      focusWithin()
    ]))
    .pipe($.rename({
      basename: "admin",
      suffix: '.min'
    }))
    .pipe(gulp.dest('build/css'))
});

// run JS lint on theme scripts
gulp.task('lint', function () {
  return gulp.src(PATHS.jsTheme)
    .pipe($.jshint())
    .pipe($.jshint.reporter('jshint-stylish'))
    .pipe($.jscs())
    .pipe($.jscs.reporter());
});

// Watch tasks
gulp.task('watch', function () {
  // Watch .js files
  gulp.watch(PATHS.jsTheme, ['build:scripts:theme']);
  // Watch .js files
  gulp.watch(PATHS.jsAdmin, ['build:scripts:admin']);
  // Watch .scss files
  gulp.watch(PATHS.sass, ['build:styles']);
  // Watch .scss files
  gulp.watch(PATHS.sass, ['build:admin:styles']);
});

// DEV Watch tasks
gulp.task('watch:dev', function () {
  // Watch .js files
  gulp.watch(PATHS.jsTheme, ['dev:scripts:theme']);
  // Watch .js files
  gulp.watch(PATHS.jsAdmin, ['dev:scripts:admin']);
  // Watch .scss files
  gulp.watch(PATHS.sass, ['dev:styles']);
  // Watch .scss files
  gulp.watch(PATHS.sass, ['dev:admin:styles']);
});

// Build and minify the theme assets
gulp.task('build', function (done) {
  sequence([
    'build:scripts:vendor',
    'build:scripts:theme',
    'build:scripts:admin',
    'build:admin:styles',
    'build:styles'
  ], done);
});

// Build the theme assets un-minified
gulp.task('dev', function (done) {
  sequence([
    'dev:scripts:vendor',
    'dev:scripts:theme',
    'dev:scripts:admin',
    'dev:admin:styles',
    'dev:styles'
  ], done);
});

// Default task, run the build
gulp.task('default', ['build']);
