var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pump = require('pump');
var htmlmin = require('gulp-htmlmin');
var merge = require('merge-stream');
var swPrecache = require('sw-precache');

gulp.task('default',['sw'], function (callback) {
  // place code for your default task here



  var i = gulp.src("favicon.ico").pipe(gulp.dest('public'));



  return i;
});

gulp.task('htmlfrg', function (callback) {
  var b = gulp.src('htmlfiles/*.html')
    .pipe(htmlmin({
      collapseWhitespace: true
    }))
    .pipe(gulp.dest('public/htmlfiles'));
  return b;
});

gulp.task('htmlindex', function (callback) {
  var b = gulp.src('index.html')
    .pipe(htmlmin({
      collapseWhitespace: true
    }))
    .pipe(gulp.dest('public'));
  return b;
});

gulp.task('js', function (callback) {
  var a = gulp.src('scripts/*.js')
    .pipe(uglify())
    .pipe(gulp.dest('public/scripts'));
  return a;
});

gulp.task('jspoly', function (callback) {
  var a = gulp.src('scripts/polyfill/*.js')
    .pipe(uglify())
    .pipe(gulp.dest('public/scripts/polyfill'));
  return a;
});

gulp.task('mdl', function (callback) {
  var e = gulp.src(["mdl/*.*", "mdl/*.*"]).pipe(gulp.dest('public/mdl'));
  return e;
});

gulp.task('sw',['htmlfrg','htmlindex','js', 'jspoly', 'mdl', 'images', 'manifest'], function (callback) {
  var e = swPrecache.write('public/service-worker.js', {
    staticFileGlobs: ['public/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}'],
    stripPrefix: 'public',
    dynamicUrlToDependencies: {'/fixtures': ['/index.html'],
     '/results': ['index.html'], 
     '/fixtures': ['index.html'],
     '/signin': ['index.html'],
     '/search': ['index.html'],
     '/linkanonymous': ['index.html'],}

  }, callback);
  return callback;
});

gulp.task('images', function (callback) {
  var d = gulp.src("images/manifest/*.*").pipe(gulp.dest('public/images/manifest'));
  var e = gulp.src("images/**/*").pipe(gulp.dest('public/images/'));
  return e;
});

gulp.task('manifest', function (callback) {
 
  var d = gulp.src("images/manifest/*.*").pipe(gulp.dest('public/images/manifest'));

  var i = gulp.src("manifest.json").pipe(gulp.dest('public'));
  return i;
});