'use strict';

var path = require('path');
var gulp = require('gulp');
var conf = require('./conf');
var del = require('del');

var browserSync = require('browser-sync');
var browserSyncSpa = require('browser-sync-spa');

var util = require('util');

var proxyMiddleware = require('http-proxy-middleware');

function browserSyncInit(baseDir, browser) {
  browser = browser === undefined ? 'default' : browser;

  var routes = null;
  if (baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) {
    routes = {
      '/bower_components': 'bower_components'
    };
  }

  var server = {
    baseDir: baseDir,
    routes: routes
  };

  /*
   * You can add a proxy to your backend by uncommenting the line below.
   * You just have to configure a context which will we redirected and the target url.
   * Example: $http.get('/users') requests will be automatically proxified.
   *
   * For more details and option, https://github.com/chimurai/http-proxy-middleware/blob/v0.9.0/README.md
   */
  // server.middleware = proxyMiddleware('/users', {target: 'http://jsonplaceholder.typicode.com', changeOrigin: true});

  var masterProxy = proxyMiddleware('/merServPlat', {
    target: 'http://192.168.1.176:3800',
    changeOrigin: true
  });

  // var loginProxy = proxyMiddleware('/v1', {
  //   target: 'http://192.168.1.228:9000',
  //   changeOrigin: true
  // });


  var filter = function (pathname, req) {
    var reg = /\/v1\/[^platform]/g;
    return reg.test(pathname);
  }

  var loginProxy = proxyMiddleware(filter, {
    // target: 'http://10.30.3.32:9000/',
    target: 'http://sso.ipay.so/',

    cookieDomainRewrite: 'localhost',
    changeOrigin: true
  });

  var platformProxy = proxyMiddleware('/v1/platform', {
    target: 'https://tasty.ipay.so/',
    cookieDomainRewrite: 'localhost',
    changeOrigin: true
  });

  var memberProxy = proxyMiddleware('/v1/member', {
    target: 'http://sso.ipay.so/',
    cookieDomainRewrite: 'localhost',
    changeOrigin: true
  });

  server.middleware = [masterProxy, loginProxy, platformProxy, memberProxy];

  browserSync.instance = browserSync.init({
    port: 9900,
    startPath: '/',
    server: server,
    browser: browser
  });
}

browserSync.use(browserSyncSpa({
  selector: '[ng-app]' // Only needed for angular apps
}));

var gulpNgConfig = require('gulp-ng-config');
gulp.task('config-local', function () {
  gulp.src(path.join(conf.paths.src, '/app/config/config.json'))
    .pipe(gulpNgConfig('sso.config', {
      environment: 'dev'
    }))
    .pipe(gulp.dest(path.join(conf.paths.src, '/app/config')))
});

gulp.task('serve', ['config-local', 'watch'], function () {
  del([path.join(conf.paths.src, '/app/config/config.js')]);
  browserSyncInit([path.join(conf.paths.tmp, '/serve'), conf.paths.src]);
});

gulp.task('serve:dist', ['build'], function () {
  browserSyncInit(conf.paths.dist);
});

gulp.task('serve:e2e', ['inject'], function () {
  browserSyncInit([conf.paths.tmp + '/serve', conf.paths.src], []);
});

gulp.task('serve:e2e-dist', ['build'], function () {
  browserSyncInit(conf.paths.dist, []);
});
