'use strict';

var gulp = require('gulp');
var gulp_if = require('gulp-if');
var sourcemaps = require('gulp-sourcemaps');
var gulp_util = require('gulp-util');
var autoprefixer = require('gulp-autoprefixer');
var clean_css = require('gulp-clean-css');
var argv = require('minimist')(process.argv.slice(2));
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var del = require('del');
var iconfont = require('gulp-iconfont');
var iconfont_css = require('gulp-iconfont-css');

var DEBUG = argv.debug;

var JS_LIB_SOURCE = 'private/js/lib/**/*.js';
var JS_MAIN_SOURCE = 'private/js/{main,modules/**/*}.js';
var JS_OUTPUT = 'static/js/';

gulp.task('JS_LIB', function() {
  return gulp.src(JS_LIB_SOURCE)
    .pipe(gulp_if(DEBUG, sourcemaps.init()))
    .pipe(concat('lib.js'))
    .pipe(uglify())
    .pipe(gulp_if(DEBUG, sourcemaps.write()))
    .pipe(gulp.dest(JS_OUTPUT));
});

gulp.task('JS_MAIN', function() {
  return gulp.src(JS_MAIN_SOURCE)
    .pipe(gulp_if(DEBUG, sourcemaps.init()))
    .pipe(concat('main.js'))
    .pipe(uglify())
    .pipe(gulp_if(DEBUG, sourcemaps.write()))
    .pipe(gulp.dest(JS_OUTPUT));
});

var SCSS_SOURCE = 'private/scss/**/*.{scss,sass}';
var SCSS_OUTPUT = 'static/css';

gulp.task('SCSS', function() {
  return gulp.src(SCSS_SOURCE)
    .pipe(gulp_if(DEBUG, sourcemaps.init()))
    .pipe(sass())
    .on('error', function(error) {
      gulp_util.log(gulp_util.colors.red(
        'Error (' + error.plugin + '): ' + error.messageFormatted)
      );
      // used on Aldryn to inform aldryn client about the errors in
      // SASS compilation
      if (process.env.EXIT_ON_ERRORS) {
        process.exit(1);
      }
    })
    .pipe(autoprefixer({
      cascade: false
    }))
    .pipe(clean_css({
      rebase: false,
      compatibility: 'ie8'
    }))
    .pipe(gulp_if(DEBUG, sourcemaps.write()))
    .pipe(gulp.dest(SCSS_OUTPUT))
});


var ICON_FONT_NAME = 'Icons';
var ICON_SOURCE = 'private/icons/**/*';

gulp.task('ICONFONT', function() {
  return gulp.src(ICON_SOURCE)
    .pipe(iconfont_css({
      fontName: ICON_FONT_NAME,
      path: 'private/scss/templates/_icons.scss',
      targetPath: '../../../private/scss/_icons.scss',
      fontPath: '/static/fonts/' + ICON_FONT_NAME + '/'
    }))
    .pipe(iconfont({
      fontName: ICON_FONT_NAME,
      prependUnicode: true,
      formats: ['woff', 'woff2']
    }))
    .pipe(gulp.dest('static/fonts/' + ICON_FONT_NAME + '/'));
});

var COPY_SOURCES = ['fonts', 'img', 'animation'];

gulp.task('copy', function() {
  COPY_SOURCES.forEach(function(source) {
    gulp.src(['private/' + source + '/**/*'])
      .pipe(gulp.dest('static/' + source));
  });
});

gulp.task('watch', function() {
  gulp.watch(JS_LIB_SOURCE, ['JS_LIB']);
  gulp.watch(JS_MAIN_SOURCE, ['JS_MAIN']);
  gulp.watch(SCSS_SOURCE, ['SCSS']);
  COPY_SOURCES.forEach(function(source) {
    gulp.watch('private/' + source + '/**/*', ['copy']);
  });
});

gulp.task('clean', function() {
  del([
    JS_OUTPUT + '/**/*',
    SCSS_OUTPUT + '/**/*'
  ]);
  COPY_SOURCES.forEach(function(source) {
    del('static/' + source + '/**/*');
  });
});

gulp.task('build', function() {
  gulp.start('clean');
  setTimeout(function() {
    gulp.start('JS_LIB');
    gulp.start('JS_MAIN');
    gulp.start('SCSS');
    gulp.start('copy');
  }, 500);
});

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