"use strict";
var gulp = require('gulp'),

  autoprefixer = require('gulp-autoprefixer'),
  cssnano = require('gulp-cssnano'),
  jshint = require('gulp-jshint'),
  uglify = require('gulp-uglify'),

  rename = require('gulp-rename'),
  concat = require('gulp-concat'),
  notify = require('gulp-notify'),
  cache = require('gulp-cache'),
  livereload = require('gulp-livereload'),
  htmlmin = require('gulp-htmlmin'),
  inject = require('gulp-inject'),
  del = require('del'),
  htmlreplace = require('gulp-html-replace');


var filePath = {
  build_dir: './dist'
};

gulp.task('scripts', function() {
  return gulp.src(['html/js/shell.js','html/js/app.v3.js'])
    .pipe(jshint('.jshintrc'))
    .pipe(jshint.reporter('default'))
    .pipe(concat('app.js'))
    /*.pipe(gulp.dest('dist/js'))*/
    /*.pipe(rename({suffix: '.min'}))*/
     /* .pipe(uglify({mangle: false}))*/
    /*.pipe(concat('app.js'))*/
    .pipe(gulp.dest('dist/js'));
});

gulp.task('vendor', function() {
  return gulp.src(['html/libs/microevent.js','html/js/moment.min.js','html/js/ejs_production.js','html/js/jquery.unveil.js'])
    .pipe(concat('vendor.js'))
    .pipe(uglify({mangle: false}))
    .pipe(gulp.dest('dist/js'));
});

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

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

gulp.task('partials', function() {
  gulp.src(['html/ejs/**/*']).pipe(gulp.dest('dist/ejs'));
  gulp.src(['html/icons/**/*']).pipe(gulp.dest('dist/icons'));
  gulp.src(['html/assets/fm.png']).pipe(gulp.dest('dist/assets'));
  gulp.src(['html/pipes.appcache']).pipe(gulp.dest('dist/'));
});



gulp.task('index', function () {
  var sources = gulp.src(['js/apps.js', 'css/app.css'], {read: false});

  return gulp.src(['html/index.html'])
    .pipe(htmlreplace({
      'css': 'css/app.css',
      'js': 'js/app.js',
      'vendor': 'js/vendor.js',
      'remote': 'js/remote.js'
    }))
    .pipe(htmlmin({removeComments: true, collapseWhitespace: true, keepClosingSlash: true}))
    .pipe(gulp.dest('dist/'));
});

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


gulp.task('default', ['clean'], function() {
  gulp.start('styles', 'scripts', 'remote', 'vendor', 'partials','index');
});
