var gulp = require('gulp');
var bem = require('gulp-bem');
var concat = require('gulp-concat');
var del = require('del');
var jade = require('gulp-jade');
var sass = require('gulp-sass');
var pack = require('gulp-bem-pack');
var autoprefixer = require('gulp-autoprefixer');
var uglify = require('gulp-uglify');
var buildBranch = require('buildbranch');

var levels = [
    'levels/js',
    'libs/bootstrap/levels/*',
    'levels/base',
    'levels/blocks',
    'levels/pages'
];

var tree;
gulp.task('tree', function () {
    tree = bem.objects(levels).pipe(bem.deps()).pipe(bem.tree());
});

gulp.task('js', ['tree'], function () {
    return tree.deps('levels/pages/index')
        .pipe(bem.src('{bem}.js'))
        .pipe(pack('index.js'))
        .pipe(gulp.dest('./dist'));
});

gulp.task('uglify', ['js'], function () {
    return gulp.src('dist/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('dist'));
});

gulp.task('css', ['tree'], function () {
    function buildCss(page) {
        return tree.deps('levels/pages/' + page.id)
            .pipe(bem.src('{bem}.{css,scss}'))
            .pipe(concat(page.id + '.css'))
            .pipe(sass())
            .pipe(autoprefixer({
                browsers: ['last 2 versions'],
                cascade: false
            }))
            .pipe(gulp.dest('./dist'));
    }

    return bem.objects('levels/pages').map(buildCss);
});

gulp.task('html', ['tree'], function () {
    function buildHtml(page) {
        return tree.deps('levels/pages/' + page.id)
            .pipe(bem.src('{bem}.jade'))
            .pipe(concat({
                path: page.path + '/' + page.id + '.jade',
                base: page.path
            }))
            .pipe(jade({pretty: true}))
            .pipe(gulp.dest('./dist'));
    }

    return bem.objects('levels/pages').map(buildHtml);
});

gulp.task('cname', function () {
    return gulp.src('CNAME').pipe(gulp.dest('dist'));
});

gulp.task('assets', function () {
    return gulp.src('assets/**').pipe(gulp.dest('dist'));
});

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

gulp.task('build', ['html', 'css', 'js', 'assets', 'cname']);

gulp.task('production', ['build', 'uglify']);

gulp.task('gh', ['production'], function(done) {
    buildBranch({ folder: 'dist', ignore: ['libs'] }, done);
});

var watch = require('gulp-watch');
gulp.task('watch', ['build'], function() {
    watch('levels/**/*.js', { name: 'JS' }, function () { gulp.start('js'); });
    watch('levels/**/*.{scss,css}', { name: 'CSS' }, function () { gulp.start('css'); });
    watch('levels/**/*.jade', { name: 'HTML' }, function () { gulp.start('html'); });
});

gulp.task('default', ['watch']);

@@ -2407,39 +2407,88 @@
 ch('
-level
+asset
 s/**/*
-.js', %7B name: 'JS' %7D
+', function () %7B gulp.start('assets'); %7D);%0A    watch('levels/**/*.js'
 , fu
@@ -2560,25 +2560,8 @@
 s%7D',
- %7B name: 'CSS' %7D,
  fun
@@ -2627,26 +2627,8 @@
 de',
- %7B name: 'HTML' %7D,
  fun

add assets watcher