var gulp = require('gulp'),
    coffee = require('gulp-coffee'),
    less = require('gulp-less'),
    sourcemaps = require('gulp-sourcemaps'),
    concat = require('gulp-concat'),
    Q = require('q'),
    http = require('http'),
    express = require('express');


gulp.task('serve', function() {
  var host = '0.0.0.0';
  var port = +(process.env.PORT || 5000);
  var app = express().use('/', express.static(__dirname + '/build'));
  http.createServer(app).listen(port, host, function() {
    console.log('devel server listening on ' + host + ':' + port);
  })

  return Q.defer().promise;
});


gulp.task('ciucas', function() {
  require('coffee-script/register');
  return require('./src/data.coffee').build('ciucas');
});


gulp.task('data', function() {
  require('coffee-script/register');
  return require('./src/data.coffee').buildAll();
});


gulp.task('html', function() {
  require('coffee-script/register');
  return require('./src/data.coffee').html();
});


gulp.task('libs', function() {
  var done = Q.defer();
  var request = require('request');
  var fs = require('fs');
  var cdnjs = 'http://cdnjs.cloudflare.com/ajax/libs/';
  request(cdnjs + 'd3/3.5.3/d3.min.js', function(err, res, body) {
    fs.writeFileSync('build/d3.min.js', body);
    request(cdnjs + 'topojson/1.6.9/topojson.min.js', function(err, res, body) {
      fs.writeFileSync('build/topojson.min.js', body);
      request(cdnjs + 'twitter-bootstrap/3.3.2/css/bootstrap.min.css', function(err, res, body) {
        fs.writeFileSync('build/bootstrap.min.css', body);
        done.resolve();
      });
    });
  });
  return done.promise;
});


gulp.task('ui', function() {
  gulp.src('src/ui/**/*.coffee')
    .pipe(sourcemaps.init())
    .pipe(coffee())
    .pipe(concat('ui.js'))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./build'));

  gulp.src('src/ui/region.less')
    .pipe(less())
    .pipe(gulp.dest('./build'))
});


gulp.task('auto', function() {
  gulp.start('ui');
  gulp.watch('src/ui/**/*', ['ui']);
});


gulp.task('devel', ['auto', 'serve']);
gulp.task('default', ['data', 'ui', 'html', 'libs']);

var gulp = require('gulp'),
    coffee = require('gulp-coffee'),
    less = require('gulp-less'),
    sourcemaps = require('gulp-sourcemaps'),
    concat = require('gulp-concat'),
    Q = require('q'),
    http = require('http'),
    express = require('express');


gulp.task('serve', function() {
  var host = '0.0.0.0';
  var port = +(process.env.PORT || 5000);
  var app = express().use('/', express.static(__dirname + '/build'));
  http.createServer(app).listen(port, host, function() {
    console.log('devel server listening on ' + host + ':' + port);
  })

  return Q.defer().promise;
});


gulp.task('ciucas', function() {
  require('coffee-script/register');
  return require('./src/data.coffee').build('ciucas');
});


gulp.task('data', function() {
  require('coffee-script/register');
  return require('./src/data.coffee').buildAll();
});


gulp.task('html', function() {
  require('coffee-script/register');
  return require('./src/data.coffee').html();
});


gulp.task('libs', function() {
  var done = Q.defer();
  var request = require('request');
  var fs = require('fs');
  var cdnjs = 'http://cdnjs.cloudflare.com/ajax/libs/';
  request(cdnjs + 'd3/3.5.3/d3.min.js', function(err, res, body) {
    fs.writeFileSync('build/d3.min.js', body);
    request(cdnjs + 'topojson/1.6.9/topojson.min.js', function(err, res, body) {
      fs.writeFileSync('build/topojson.min.js', body);
      request(cdnjs + 'twitter-bootstrap/3.3.2/css/bootstrap.min.css', function(err, res, body) {
        fs.writeFileSync('build/bootstrap.min.css', body);
        done.resolve();
      });
    });
  });
  return done.promise;
});


gulp.task('js', function() {
  return gulp.src('src/ui/**/*.coffee')
    .pipe(sourcemaps.init())
    .pipe(coffee())
    .pipe(concat('ui.js'))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./build'));
});


gulp.task('css', function() {
  return gulp.src('src/ui/region.less')
    .pipe(less())
    .pipe(gulp.dest('./build'))
});


gulp.task('ui', ['js', 'css'], function() {
  gulp.start('html');
});


gulp.task('auto', function() {
  gulp.start('ui');
  gulp.watch('src/ui/**/*', ['ui']);
  gulp.watch('templates/**/*', ['ui']);
});


gulp.task('devel', ['auto', 'serve']);
gulp.task('default', ['data', 'ui', 'libs']);

build html as part of the ui