/* eslint global-require: 0 */
var path = require('path');
var gulp = require('gulp');
var del = require('del');
var rename = require('gulp-rename');
var metalsmith = require('gulp-metalsmith');
var layouts = require('metalsmith-layouts');
var drafts = require('metalsmith-drafts');
var define = require('metalsmith-define');
var connect = require('gulp-connect');
var builder = require('./build/build');

var handlebars = require('handlebars');

var markupRegex = /([^\/^\.]*)\.html$/;
var locale = process.env.locale || 'en';

var defines = define({
  'urls': {
    'zh': {
      'urlTemplate' : 'http://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
      'subdomains': '[1, 2, 3, 4]'
    },
    'en': {
      'urlTemplate': 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
      'subdomains': '["a","b","c"]'
    }
  }
});

function readExamplesInfo() {
  var json = require('./build/examples.json');
  var items = json.examples;
  var count = Math.floor(items.length);
  var info = {};
  var i, j, order = 0;
  for (i = 0; i < count; i++) {
    var subItems = items[i].examples;
    var subCount = subItems.length;
    for (j = 0; j < subCount; j++) {
      order++;
      var key = path.join(items[i].name, subItems[j].name);
      info[key] = {
        'category': items[i].title,
        'title': subItems[j].title,
        'order': order
      };
    }
  }
  return info;
}

function processSingleFile(file, filepath, files, metadata, isRaw) {
  var basename = path.basename(filepath);
  var match = basename.match(markupRegex);
  if (!match) return;

  var id = match[1];
  var dirname = path.dirname(filepath);

  var info = readExamplesInfo();
  file.meta = info[dirname] || {};
  if (file.meta.category) {
    file.category = file.meta.category[locale];
  }
  if (file.meta.title) {
    file.title = file.meta.title[locale];
  }

  file.basename = basename;

  var js = path.join(dirname, id + '.js');
  if (js in files) {
    var url = metadata['urls'][locale];
    file.js = {
      basename: id + '.js',
      source: files[js].contents.toString()
        .replace(/\$\(urlTemplate\)/g, url.urlTemplate)
        .replace(/\$\(subdomains\)/g, url.subdomains)
    };
    delete files[js];
  }

  var css = path.join(dirname, id + '.css');
  if (css in files) {
    file.css = {
      basename: id + '.css',
      source: files[css].contents.toString()
    };
    if (!isRaw) {
      delete files[css];
    }
  }
}

/**
 * Process the raw demo pages
 */
function processRaw(files, metalsmith, done) {
  setImmediate(done);
  for (var filepath in files) {
    var file = files[filepath];
    processSingleFile(file, filepath, files, metalsmith.metadata(), true);
  }
}

/**
 * Process demo pages
 */
function processDemo(files, metalsmith, done) {
  setImmediate(done);
  for (var filepath in files) {
    var file = files[filepath];
    processSingleFile(file, filepath, files, metalsmith.metadata(), false);
  }
}

/**
 *
 */
function indentHelper(text, options) {
  if (!text) {
    return text;
  }
  var count = options.hash.spaces || 2;
  var spaces = new Array(count + 1).join(' ');
  return text.split('\n').map(function (line) {
    return line ? spaces + line : '';
  }).join('\n');
}

function escapeHelper(options) {
  return handlebars.Utils.escapeExpression(options.fn(this));
}

gulp.task('examples-raw', function () {
  return gulp.src('src/examples/**/*')
    .pipe(metalsmith({
      use: [
        drafts(),
        defines,
        processRaw,
        layouts({
          engine: 'handlebars',
          directory: 'layouts/raw',
          helpers: {
            indent: indentHelper
          }
        })
      ]
    }))
    .pipe(rename(function (path) {
      path.dirname += '/raw';
      return path;
    }))
    .pipe(gulp.dest(path.join('dist/', locale, 'examples')));
});

var options = [
  drafts(),
  defines,
  processDemo,
  layouts({
    engine: 'handlebars',
    directory: 'layouts',
    partials: 'layouts/raw',
    helpers: {
      indent: indentHelper,
      escape: escapeHelper,
      list: builder.listHelper
    }
  })
];

gulp.task('examples-demo', function () {
  return gulp.src('src/examples/**/*.{html,js,css}')
    .pipe(metalsmith({
      use: options
    }))
    .pipe(gulp.dest(path.join('dist', locale, 'examples')));
});

gulp.task('examples-index', function () {
  return gulp.src('src/*.{html,js,css}')
    .pipe(metalsmith({
      use: options
    }))
    .pipe(gulp.dest(path.join('dist', locale)));
});

gulp.task('examples', ['examples-index', 'examples-raw', 'examples-demo'], function () {
  del([
    'dist/**/*'
  ]);
  return gulp.src('assets/**/*')
    .pipe(gulp.dest('dist'));
});

gulp.task('watch', ['examples'], function () {
  var scriptWatcher = gulp.watch(['./src/**/*', './assets/**/*', './layouts/**/*'], ['examples']); // watch the same files in our scripts task
});

gulp.task('connect', ['watch'], function () {
  connect.server({
    root: 'dist',
    livereload: true,
    port: 20001
  });
});

gulp.task('check', function () {
  builder.check();
});

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

@@ -3337,16 +3337,64 @@
 s));%0A%7D%0A%0A
+var outputFolder = '../examples.maptalks.org';%0A%0A
 gulp.tas
@@ -3860,15 +3860,20 @@
 oin(
-'dist/'
+outputFolder
 , lo
@@ -4328,38 +4328,44 @@
 .dest(path.join(
-'dist'
+outputFolder
 , locale, 'examp
@@ -4541,22 +4541,28 @@
 th.join(
-'dist'
+outputFolder
 , locale
@@ -4664,38 +4664,8 @@
 ) %7B%0A
-  del(%5B%0A    'dist/**/*'%0A  %5D);%0A
   re
@@ -4717,15 +4717,96 @@
 est(
-'dist')
+outputFolder));%0A%7D);%0A%0Agulp.task('clean', function () %7B%0A  return del(%5B%0A    'dist/**/*'%0A  %5D
 );%0A%7D
@@ -4861,28 +4861,8 @@
  %7B%0A 
- var scriptWatcher =
  gul
@@ -4938,52 +4938,57 @@
 '%5D);
- // watch the same files in our scripts task
+%0A  gulp.watch(%5B'./build/*'%5D, %5B'examples-index'%5D);
 %0A%7D);
@@ -5068,14 +5068,20 @@
 ot: 
-'dist'
+outputFolder
 ,%0A  

update gulpfile.js