const gulp = require('gulp');
const autoprefixer = require('gulp-autoprefixer');
const cssnano = require('gulp-cssnano');
const jshint = require('gulp-jshint');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const concat = require('gulp-concat');
const cache = require('gulp-cache');
const htmlmin = require('gulp-htmlmin');
const inject = require('gulp-inject');
const del = require('del');
const htmlreplace = require('gulp-html-replace');
const stripDebug = require('gulp-strip-debug');
const babel = require('gulp-babel');

var fontOptions = { };

gulp.task('appJS', function() {
  return gulp.src(['public/javascripts/shorten.js'])
     .pipe(stripDebug())
    .pipe(jshint('.jshintrc'))
    .pipe(jshint.reporter('default'))
    .pipe(babel({ presets: ['es2015'] }))
    .pipe(concat('nurl.js'))
    .pipe(uglify({ mangle: true, compress: { sequences: true,  // Join consecutive statemets with the “comma operator”
      properties: true,  // Optimize property access: a["foo"] → a.foo
      dead_code: true,  // Discard unreachable code
      drop_debugger: true,  // Discard “debugger” statements
      unsafe: false, // Some unsafe optimizations (see below)
      conditionals: true,  // Optimize if-s and conditional expressions
      comparisons: true,  // Optimize comparisons
      evaluate: true,  // Evaluate constant expressions
      booleans: true,  // Optimize boolean expressions
      loops: true,  // Optimize loops
      unused: true,  // Drop unused variables/functions
      hoist_funs: true,  // Hoist function declarations
      hoist_vars: false, // Hoist variable declarations
      if_return: true,  // Optimize if-s followed by return/continue
      join_vars: true,  // Join var declarations
      cascade: true,  // Try to cascade `right` into `left` in sequences
      side_effects: true,  // Drop side-effect-free statements
      warnings: true,  // Warn about potentially dangerous optimizations/code
      global_defs: {}     // global definitions
    } }))
    // .pipe(update(atob('LyogPT09PT09PQ0KDQpEZXZlbG9wZWQgYnkgTWFydGluIERvbm5lbGx5IG1hcnRpbmQyMDAwe2F0fWdtYWlsLmNvbQ0KDQo9PT09PT09ICovDQoNCg==')))
    .pipe(gulp.dest('live/js'));
});


gulp.task('vendor', function() {
  return gulp.src([
    'bower_components/jquery/dist/jquery.min.js',
    'bower_components/mui/packages/cdn/js/mui.min.js'
  ])
        .pipe(concat('vendor.js'))
        .pipe(uglify({ mangle: false }))
        .pipe(gulp.dest('live/js'));
});

gulp.task('styles', function() {
  return gulp.src(['public/css/roboto.css', 'bower_components/mui/packages/cdn/css/mui.min.css', 'public/css/styles.css'])
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
        .pipe(cssnano())
        .pipe(concat('app.css'))
        .pipe(gulp.dest('live/css'));
});


gulp.task('migrate', function() {
  return gulp.src(['fav/favicon-16x16.png'])
    .pipe(gulp.dest('live'));
});

gulp.task('index', function() {

  return gulp.src(['views/index.html'])
        .pipe(htmlreplace({
          mui: 'css/mui.min.css',
          css: 'css/app.css',
          js: 'js/nurl.js',
          vendor: 'js/vendor.js'

        }))
        .pipe(htmlmin({ removeComments: true, collapseWhitespace: true, keepClosingSlash: true }))

        .pipe(gulp.dest('dist/'));
});




gulp.task('clean', function() {
  return del(['live']);
});


gulp.task('buildJS', function() {
  gulp.start('appJS', 'vendor');
});



gulp.task('default', ['clean'], function() {
  //gulp.start('buildJS','styles','index');
  gulp.start('buildJS', 'styles', 'index', 'migrate');
});
