'use strict';

var gulp        = require('gulp');
var plugin      = require('gulp-load-plugins')();
var browserSync = require('browser-sync');
var plumber     = require('gulp-plumber');
var pagespeed   = require('psi');
var reload      = browserSync.reload;
var publicUrl   = '';
var minifyCSS   = require('gulp-minify-css');
var uglify      = require('gulp-uglify');
var concat      = require('gulp-concat');
var spritesmith = require('gulp.spritesmith');
var svgstore    = require('gulp-svgstore');
var svgmin      = require('gulp-svgmin');
var clean       = require('gulp-clean');
var path        = require('path');
var rename      = require('gulp-rename');
var htmlmin     = require('gulp-htmlmin');
var connect     = require('gulp-connect');

var paths = {
  build: 'compressed/',
};

var AUTOPREFIXER_BROWSERS = [
'ie >= 8',
'ie_mob >= 10',
'ff >= 15',
'chrome >= 30',
'safari >= 5',
'opera >= 23',
'ios >= 6',
'android >= 2.3',
'bb >= 10'
];

// lint JavaScript
gulp.task('jshint', function () {
  return gulp.src('assets/js/app/*.js')
  .pipe(reload({stream: true, olsnce: true}))
  .pipe(plugin.jshint())
  .pipe(plugin.jshint.reporter('jshint-stylish'))
  .pipe(plugin.if(!browserSync.active, plugin.jshint.reporter('fail')));
});


// Sprites
gulp.task('sprite', function () {
  var spriteData = gulp.src('assets/img/layout/sprites/**/*.png')
  .pipe(spritesmith({
    imgName: 'sprite.png',
    imgPath : 'assets/img/layout/sprite.png',
    cssName: '_sprites.scss',
    algorithm: 'top-down'
  }));
  spriteData.img.pipe(gulp.dest('assets/img/layout'));
  spriteData.css.pipe(gulp.dest('scss/components'));
});


// copilando o scss e adicionando os prefix
gulp.task('styles', function () {
  return gulp.src(['scss/*.scss'])
  .pipe(plumber(function(error){
    console.log("Error happend!", error.message);
    this.emit('end');
  }))
  .pipe(plugin.changed('styles', {extension: '.scss'}))
  .pipe(plugin.sass().on('error', console.error.bind(console)))
  .pipe(plugin.autoprefixer(AUTOPREFIXER_BROWSERS))
  .pipe(plumber.stop())
  .pipe(minifyCSS())
  .pipe(gulp.dest('assets/css'));
});


// observando mudanças para da reload
gulp.task('serve', ['styles'], function () {
  browserSync({
    notify: false,
    proxy: 'localhost/adeltec'
  });

  gulp.watch(['scss/**/*.scss'], ['styles']);
  gulp.watch(['assets/css/*.css'], reload);
  gulp.watch(['assets/js/**/*.js'], reload);
  gulp.watch(['assets/img/layout/sprites/**/*.png'], ['sprite']);
  gulp.watch(['assets/img/content/*', 'img/layout/*'], reload);
});



// otimizando imagens
gulp.task('images', function () {
  return gulp.src(['img/**/*.png'])
  .pipe(plugin.imagemin({
    progressive: true,
    interlaced: true
  }))
  .pipe(gulp.dest(paths.build));
});

// otimizando imagens
gulp.task('images', function () {
  return gulp.src(['img/**/*.png'])
  .pipe(plugin.imagemin({
    progressive: true,
    interlaced: true
  }))
  .pipe(gulp.dest(paths.build));
});


gulp.task('clean-svg', function () {
  return gulp.src('assets/img/svg/svg.svg', {read: false})
    .pipe(clean());
});

// gerando svg
gulp.task('svg', ['clean-svg'], function () {
    return gulp.src('assets/img/svg/**/*.svg')
    .pipe(svgmin(function (file) {
        var prefix = path.basename(file.relative, path.extname(file.relative));
        return {
            plugins: [{
                cleanupIDs: {
                    prefix: prefix + '-',
                    minify: true
                }
            }]
        }
    }))
    .pipe(svgstore())
    .pipe(gulp.dest('assets/img/svg'))
});

// MINIFICAR HTML
gulp.task('html', function() {
    return gulp.src(['assets/html/**/*.html', '!assets/html/**/*min.html', '!assets/html/email/*.html'])
    .pipe(htmlmin({collapseWhitespace: true,minifyJS: true}))
    .pipe(rename(function (path) {
        path.basename += ".min";
        path.extname = ".html"
    }))
    .pipe(gulp.dest('assets/html/'))
    .pipe(connect.reload());
});

gulp.task('pagespeed-mobile', pagespeed.bind(null, {
  url: publicUrl,
  strategy: 'mobile'
}));

gulp.task('pagespeed-desktop', pagespeed.bind(null, {
  url: publicUrl,
  strategy: 'desktop'
}));

// Task padrão, exibe o menu de tasks
gulp.task('default', function (callback) {
  plugin.menu(this);
});