var gulp          = require('gulp');
var plugins       = require('gulp-load-plugins')();
var templatecache = require('gulp-angular-templatecache');
var karma         = require('karma').server;
var _             = require('lodash');
var browserSync   = require('browser-sync');
var at            = require('gulp-asset-transform');
var protractor    = require('gulp-protractor').protractor;
var del           = require('del');

var paths = {
  css         : ['src/assets/css/*.css', 'src/app/**/*.css'],
  fonts       : 'src/assets/fonts/*.*',
  images      : 'src/assets/images/**',
  index       : 'src/index.html',
  integration : 'test/integration/*.js',
  js          : 'src/app/**/*.js',
  less        : ['src/assets/less/*.less', 'src/app/**/**/*.less'],
  templates   : 'src/app/components/**/*.html'
};

var config = {
  jshint : 'config/jshint.conf',
  karma  : require('./config/karma.conf')
};


gulp.task('copy-images', function() {
  return gulp.src(paths.images)
    .pipe(plugins.plumber())
    .pipe(plugins.imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))
    .pipe(plugins.size({ title: 'images', showFiles: true }))
    .pipe(gulp.dest('develop/images'));
});

gulp.task('copy-fonts', function() {
  return gulp.src(paths.fonts)
    .pipe(plugins.plumber())
    .pipe(plugins.size({ title: 'fonts' }))
    .pipe(gulp.dest('develop/fonts'));
});

gulp.task('at-build', function() {
  return gulp.src(paths.index)
    .pipe(plugins.plumber())
    .pipe(at({
      css: {
        tasks: ['concat',
                plugins.size({ title: 'css', showFiles: true })]
      },
      js: {
        tasks: ['concat',
                plugins.size({ title: 'js', showFiles: true })]
      },
      js_lib: {
        tasks: ['concat',
                plugins.size({ title: 'js_lib', showFiles: true })]
      },
      less: {
        tasks: [plugins.recess(),
                plugins.recess.reporter(),
                plugins.less(),
                'concat',
                plugins.size({ title: 'less', showFiles: true})]
      }
    }))
    .pipe(gulp.dest('develop/'))
    .pipe(browserSync.reload({ stream: true }));
});

gulp.task('templates', function () {
  return gulp.src(paths.templates)
    .pipe(plugins.plumber())
    .pipe(templatecache('templates.js', { standalone: true }))
    .pipe(plugins.size({ title: 'templates' }))
    .pipe(gulp.dest('develop/js'))
    .pipe(browserSync.reload({ stream: true }));
});

gulp.task('jshint', function() {
  gulp.src(paths.js)
    .pipe(plugins.jshint(config.jshint))
    .pipe(plugins.jshint.reporter('jshint-stylish'));
});


gulp.task('clean', function(done) {
  del(['develop/**'], done);
});


gulp.task('karma', function(done) {
  karma.start(_.assign({}, config.karma, { singleRun: true, colors: true }), done);
});

gulp.task('karma-ci', function(done) {
  karma.start(_.assign({}, config.karma, { singleRun: false, colors: true, autoWatch: true }), done);
});

gulp.task('integration', ['server'], function(done) {
// gulp.task('integration', function(done) {
  gulp.src(paths.integration)
    .pipe(protractor({
      configFile: 'config/protractor.conf',
      args: ['--baseUrl', 'http://localhost:3000/#/']
    }))
    .on('error', function(e) { browserSync.exit(); throw e; })
    .on('end', function() { browserSync.exit(); done(); });
    // .on('error', function(e) { throw e; })
    // .on('end', function() { done(); });
});


gulp.task('server', function() {
  browserSync({
    server: {
      baseDir: 'develop'
    },
    logConnections: true,
    open: false
  });
});

gulp.task('watch', function() {
  gulp.watch([paths.index, paths.css, paths.less, paths.js], ['at-build']);
  gulp.watch([paths.images, paths.fonts], ['copy-assets']);
  gulp.watch([paths.templates], ['templates']);
  gulp.watch([paths.js], ['jshint']);
});

gulp.task('copy-assets', ['copy-images', 'copy-fonts']);
gulp.task('build', ['at-build', 'templates', 'copy-assets']);
gulp.task('develop', ['build', 'server', 'watch', 'karma-ci']);

@@ -737,18 +737,26 @@
 src/app/
-**
+components
 /**/*.le
@@ -1876,85 +1876,8 @@
 s: %5B
-plugins.recess(),%0A                plugins.recess.reporter(),%0A                
 plug
@@ -2529,32 +2529,191 @@
 tylish'));%0A%7D);%0A%0A
+gulp.task('less-lint', function() %7B%0A  gulp.src(paths.less)%0A    .pipe(plugins.plumber())%0A    .pipe(plugins.recess())%0A    .pipe(plugins.recess.reporter());%0A%7D);%0A%0A
 %0Agulp.task('clea
@@ -3097,53 +3097,8 @@
 ) %7B%0A
-// gulp.task('integration', function(done) %7B%0A
   gu
@@ -3379,97 +3379,8 @@
 %7D);%0A
-    // .on('error', function(e) %7B throw e; %7D)%0A    // .on('end', function() %7B done(); %7D);%0A
 %7D);%0A
@@ -3741,24 +3741,24 @@
 mplates'%5D);%0A
-
   gulp.watch
@@ -3783,16 +3783,59 @@
 int'%5D);%0A
+  gulp.watch(%5Bpaths.less%5D, %5B'less-lint'%5D);%0A
 %7D);%0A%0Agul

Move less linting to own task instead of in at-pipeline