var gulp = require('gulp'),
  uglify = require('gulp-uglify'),
  jshint = require('gulp-jshint'),
    sass = require('gulp-ruby-sass'),
   gutil = require('gulp-util'),
  ngAnnotate = require('gulp-ng-annotate'),
 concatFiles = require('gulp-concat'),
  stripDebug = require('gulp-strip-debug'),
autoprefixer = require('gulp-autoprefixer'),
templateCache = require('gulp-angular-templatecache');


function errorLog(error) {
  console.error.bind(error);
  this.emit('end');
}

gulp.task('styles', function() {
  gulp.src('styles/**/*.scss')
    .pipe(sass({
      cacheLocation: 'styles/.sass-cache',
      style: 'compressed'
    }))
    .on('error', errorLog)
    .pipe(autoprefixer({ browsers: ['IE 9', 'last 5 versions', 'Firefox 14', 'Opera 11.1'] }))
    .pipe(gulp.dest('styles/'));
});


gulp.task('jshint', function () {
  gulp.src(['scripts/**/*.js', '!scripts/vendor/**', '!scripts/uuid.js'])
    //.pipe(jshint('.jshintrc'))
    .pipe(jshint({
      unused: false
    }))
    .on('error', errorLog)
    .pipe(jshint.reporter('jshint-stylish'));

});

gulp.task('templates', function() {
  var TEMPLATE_HEADER = 'app.run(["$templateCache", function($templateCache) {';
  gulp.src('views/modal-tpls/system-overview/*.html')
    .pipe(templateCache({templateHeader: TEMPLATE_HEADER}))
    .pipe(gulp.dest('build/js/'));
});


gulp.task('watch', function() {
  gulp.watch('scripts/**/*.js', ['jshint']);
  gulp.watch('scripts/directives/editor/**/*.js', ['build editor directives']);
  gulp.watch('scripts/directives/assistant/**/*.js', ['build assistant directives']);
  gulp.watch('scripts/directives/installer/**/*.js', ['build installer directives']);
  gulp.watch('scripts/directives/user-login-change/**/*.js', ['build user-login-change directives']);
  gulp.watch('scripts/services/editor/**/*.js', ['build editor services']);
  gulp.watch('scripts/services/assistant/**/*.js', ['build assistant services']);
  gulp.watch('scripts/services/installer/**/*.js', ['build installer services']);
  gulp.watch('scripts/services/user-login-change/**/*.js', ['build user-login-change services']);
  gulp.watch('scripts/controllers/editor/**/*.js', ['build editor controllers']);
  gulp.watch('scripts/controllers/assistant/**/*.js', ['build assistant controllers']);
  gulp.watch('scripts/controllers/installer/**/*.js', ['build installer controllers']);
  gulp.watch('scripts/controllers/user-login-change/**/*.js', ['build user-login-change controllers']);
  gulp.watch('scripts/directives/common/*.js', ['build common directives']);

  gulp.watch('scripts/filters/*.js', ['build filters']);
  gulp.watch('styles/**/*.scss', ['styles']);
  gulp.watch('views/modal-tpls/system-overview/*.html', ['templates']);
});


gulp.task('vendor', function () {
  gulp.src([
    'bower_components/jquery/dist/jquery.js',
    'bower_components/angular/angular.js',
    'bower_components/angular-sanitize/angular-sanitize.js',
    'bower_components/angular-animate/angular-animate.js',
    'bower_components/angular-touch/angular-touch.js',
    'bower_components/angular-ui-router/release/angular-ui-router.js',
    'bower_components/lodash/dist/lodash.compat.js',
    'bower_components/angular-strap/dist/angular-strap.js',
    'bower_components/angular-strap/dist/angular-strap.tpl.js',
    'bower_components/jquery.ui/ui/jquery.ui.core.js',
    'bower_components/jquery.ui/ui/jquery.ui.widget.js',
    'bower_components/jquery.ui/ui/jquery.ui.mouse.js',
    'bower_components/jquery.ui/ui/jquery.ui.sortable.js',
    'bower_components/jquery.ui/ui/jquery.ui.draggable.js',
    'bower_components/jquery.ui/ui/jquery.ui.droppable.js',
    'bower_components/angular-ui-sortable/sortable.js',
    'bower_components/jqueryui-touch-punch/jquery.ui.touch-punch.js',
    'bower_components/venturocket-angular-slider/build/angular-slider.js',
    'bower_components/angular-loading-bar/build/loading-bar.js',
    'bower_components/ngFx/dist/ngFx.js',
    'bower_components/angular-ui-select/dist/select.min.js' ])
  .pipe(stripDebug())
  .pipe(concatFiles('vendor.js'))
  .pipe(uglify())
  .pipe(gulp.dest('build/js/'));
});

// gulp.task('build', function() {
//   gulp.src('scripts/**/*.js')
//   .pipe(stripDebug())
//   .pipe(concatFiles('main.js'))
//   .pipe(ngAnnotate())
//   .pipe(uglify())
//   .pipe(gulp.dest('build/js/'));
// });

gulp.task('build editor directives', function() {
  gulp.src('scripts/directives/editor/*.js')
  //.pipe(stripDebug())
  .pipe(concatFiles('editor-directives.js'))
  //.pipe(ngAnnotate())
  //.pipe(uglify())
  .pipe(gulp.dest('build/js/'));
});

gulp.task('build assistant directives', function() {
  gulp.src('scripts/directives/assistant/*.js')
    //.pipe(stripDebug())
    .pipe(concatFiles('assistant-directives.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build installer directives', function() {
  gulp.src('scripts/directives/installer/**/*.js')
    //.pipe(stripDebug())
    .pipe(concatFiles('installer-directives.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build user-login-change directives', function() {
  gulp.src('scripts/directives/user-login-change/**/*.js')
  //.pipe(stripDebug())
    .pipe(concatFiles('user-login-change-directives.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build editor services', function() {
  gulp.src('scripts/services/editor/*.js')
  //.pipe(stripDebug())
  .pipe(concatFiles('editor-services.js'))
  //.pipe(ngAnnotate())
  //.pipe(uglify())
  .pipe(gulp.dest('build/js/'));
});

gulp.task('build assistant services', function() {
  gulp.src('scripts/services/assistant/*.js')
    //.pipe(stripDebug())
    .pipe(concatFiles('assistant-services.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build installer services', function() {
  gulp.src('scripts/services/installer/*.js')
    //.pipe(stripDebug())
    .pipe(concatFiles('installer-services.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build user-login-change services', function() {
  gulp.src('scripts/services/user-login-change/*.js')
  //.pipe(stripDebug())
    .pipe(concatFiles('user-login-change-services.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build editor controllers', function() {
  gulp.src('scripts/controllers/editor/*.js')
  //.pipe(stripDebug())
  .pipe(concatFiles('editor-controllers.js'))
  //.pipe(ngAnnotate())
  //.pipe(uglify())
  .pipe(gulp.dest('build/js/'));
});

gulp.task('build assistant controllers', function() {
  gulp.src('scripts/controllers/assistant/*.js')
    //.pipe(stripDebug())
    .pipe(concatFiles('assistant-controllers.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build installer controllers', function() {
  gulp.src('scripts/controllers/installer/*.js')
    //.pipe(stripDebug())
    .pipe(concatFiles('installer-controllers.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});

gulp.task('build user-login-change controllers', function() {
  gulp.src('scripts/controllers/user-login-change/*.js')
  //.pipe(stripDebug())
    .pipe(concatFiles('user-login-change-controllers.js'))
    //.pipe(ngAnnotate())
    //.pipe(uglify())
    .pipe(gulp.dest('build/js/'));
});


gulp.task('build common directives', function() {
  gulp.src('scripts/directives/common/*.js')
  //.pipe(stripDebug())
  .pipe(concatFiles('common-directives.js'))
  //.pipe(ngAnnotate())
  //.pipe(uglify())
  .pipe(gulp.dest('build/js/'));
});



gulp.task('build filters', function() {
  gulp.src('scripts/filters/*.js')
  //.pipe(stripDebug())
  .pipe(concatFiles('filters.js'))
  //.pipe(ngAnnotate())
  //.pipe(uglify())
  .pipe(gulp.dest('build/js/'));
});

gulp.task('default', [
  'build editor directives',
  'build editor services',
  'build editor controllers',
  'build user-login-change directives',
  'build user-login-change services',
  'build user-login-change controllers',
  'build common directives',
  'build filters',
  'styles',
  'templates',
  'watch',
  'jshint'
]);
