const gulp = require('gulp');
const ts = require('gulp-typescript');
const notify = require('gulp-notify');
const notify1 = require('node-notifier');

const finish = ts.reporter.defaultReporter().finish;
function customErrorReport(){ 
  return {
    error: function(error){
      notify1.notify({title: 'ts error', message: error.message});
    },
    finish: finish
  }
}

function customErrorReportTotal(){
  return {
    finish: function(results){
      var hasError = false;
      var errorMsg = '';
      var showErrorCount = function (count, type) {
        if (count === 0){
          return;
        }
          hasError = true;
      };
      showErrorCount(results.transpileErrors, '');
      showErrorCount(results.syntaxErrors, 'syntax');
      showErrorCount(results.globalErrors, 'global');
      showErrorCount(results.semanticErrors, 'semantic');
      showErrorCount(results.emitErrors, 'emit');
      if (results.emitSkipped) {
          //gutil.log('TypeScript: emit', gutil.colors.red('failed'));
      }
      else if (hasError) {
          //gutil.log('TypeScript: emit', gutil.colors.cyan('succeeded'), '(with errors)');
      }
    }
  }
}

gulp.task('ts', function () {
  return gulp.src('*.ts')
    .pipe(ts({
      noEmitOnError: true
    }, undefined, customErrorReport()))
    //.on('error', function(error){
    //  notify(error.message);
    //})
    .pipe(gulp.dest('dist/gulp'));
});

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

gulp.task('tsconfig', function(){
  var tsResult = tsProject.src('*.ts')
  .pipe(ts(tsProject,undefined, customErrorReport()))

  tsResult.on('error', function(error){
    notify(error.message);
  });
  return tsResult.js.pipe(gulp.dest('dist/gulp1'));
})