var gulp = require('gulp');
var del = require('del');
var ts = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var runSequence = require('run-sequence');
var connect = require('gulp-connect');
var systemjsBuilder = require('systemjs-builder');
var concat = require('gulp-concat');

var tsProject = ts.createProject('tsconfig.json');

var paths = {
    buildFiles: ['gulpfile.js', 'package.json', 'typings.json', 'tsconfig.json'],
    static: ['src/**/*', '!**/*.ts', '!**/*.js'],
    tsSource: ['src/**/*.ts', 'typings/index.d.ts'],
    vendor_js: ['node_modules/zone.js/dist/zone.js','node_modules/reflect-metadata/Reflect.js', 'node_modules/systemjs/dist/system.src.js', 'node_modules/es6-shim/es6-shim.js'],
    vendor_css: ['node_modules/bootstrap/dist/**/*', 'node_modules/font-awesome/**/*'],
    compileFolder: 'build/compile',
    buildOut: 'build/target'
}

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

gulp.task('copy:static', function(){
    return gulp
        .src(paths.static)
        .pipe(gulp.dest(paths.buildOut));
});

gulp.task('copy:bootstrap', function() {
    return gulp.src(['node_modules/bootstrap/dist/css/*.css', 'node_modules/bootstrap/dist/fonts/*'], {base: './node_modules/bootstrap/dist'})
        .pipe(gulp.dest(paths.buildOut + '/vendor/bootstrap'));
})

gulp.task('copy:fa', function() {
    return gulp.src(['node_modules/font-awesome/css/*.css', 'node_modules/font-awesome/fonts/*'], {base: './node_modules'})
        .pipe(gulp.dest(paths.buildOut + '/vendor'));
})

gulp.task('copy:vendor_css', ['copy:bootstrap', 'copy:fa']);

gulp.task('compile:ts', function () {
    var tsResult = gulp.src(paths.tsSource)
        .pipe(sourcemaps.init())
        .pipe(ts(tsProject));

    tsResult.dts.pipe(gulp.dest(paths.compileFolder));

    return tsResult.js
        .pipe(sourcemaps.write({ sourceRoot: './' }))
        .pipe(gulp.dest(paths.compileFolder));
});

gulp.task('bundle:app', function() {
    var builder = new systemjsBuilder('.', './system.config.js');
    return builder.buildStatic('src/app/app.ts', paths.buildOut + '/app/app.bundle.js',
        {
            sourceMaps:true,
            runtime:false,
            sourceMapContents:true
        });
});

gulp.task('bundle:vendor', function() {
    return gulp.src(paths.vendor_js)
        .pipe(sourcemaps.init())
        .pipe(concat('vendor.bundle.js'))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(paths.buildOut + '/vendor'));
});

gulp.task('build', function(callback) {
    runSequence('clean', ['copy:static', 'copy:vendor_css', 'bundle:vendor'], 'bundle:app', callback);
});

gulp.task('watch', function() {
    gulp.watch(paths.buildFiles, ['build']);
    gulp.watch(paths.static, ['copy:static']);
    gulp.watch(paths.tsSource, ['compile:ts']);
});

gulp.task('serve', function() {
    connect.server({
        root: 'build/target'
    });
});

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

@@ -384,16 +384,18 @@
 iles: %5B'
+./
 gulpfile
@@ -401,16 +401,18 @@
 e.js', '
+./
 package.
@@ -415,24 +415,26 @@
 age.json', '
+./
 typings.json
@@ -437,16 +437,18 @@
 json', '
+./
 tsconfig
@@ -453,16 +453,38 @@
 ig.json'
+, './system.config.js'
 %5D,%0A    s
@@ -532,24 +532,29 @@
 '%5D,%0A    
-tsSource
+compiledFiles
 : %5B'src/
@@ -583,16 +583,31 @@
 ex.d.ts'
+, 'src/**/*.js'
 %5D,%0A    v
@@ -1613,26 +1613,29 @@
 copy:vendor_
-cs
+asset
 s', %5B'copy:b
@@ -1663,339 +1663,8 @@
 );%0A%0A
-gulp.task('compile:ts', function () %7B%0A    var tsResult = gulp.src(paths.tsSource)%0A        .pipe(sourcemaps.init())%0A        .pipe(ts(tsProject));%0A%0A    tsResult.dts.pipe(gulp.dest(paths.compileFolder));%0A%0A    return tsResult.js%0A        .pipe(sourcemaps.write(%7B sourceRoot: './' %7D))%0A        .pipe(gulp.dest(paths.compileFolder));%0A%7D);%0A%0A
 gulp
@@ -2310,19 +2310,22 @@
 :vendor_
-c
+a
 ss
+ets
 ', 'bund
@@ -2334,17 +2334,16 @@
 :vendor'
-%5D
 , 'bundl
@@ -2348,16 +2348,17 @@
 dle:app'
+%5D
 , callba
@@ -2516,30 +2516,35 @@
 ths.
-tsSource, %5B'compile:ts
+compiledFiles, %5B'bundle:app
 '%5D);

clean up build tasks