const content = require('./content');
const _ = require('lodash');
const md = require('./md');
const pug = require('pug');
const fs = require('fs');
const del = require('delete');
const ncp = require('ncp');
const package = require('../gekko/package');

const CONFIG = {
  dirs: {
    order: [
      'introduction',
      'installation',
      'features',
      'strategies',
      'commandline',
      'extending',
      'internals'
    ]
  }
}

const locals = {
  root: '',
  version: package.version
}

const nav = _.map(CONFIG.dirs.order, f => {
  if(!content.tree[f])
    return;

  return {
    name: f,
    content: content.tree[f]
  }
}).filter(_.identity);

locals.nav = nav;

var template = pug.compile(fs.readFileSync('templates/layout.jade', 'utf8'));

del.sync('site');
fs.mkdirSync('site');
fs.mkdirSync('site/docs/')
_.each(content.folders, f => fs.mkdirSync(`site/docs/${f}`))

_.each(content.files, f => {
  // render html
  f.html = md(f.content);
  // set title
  f.title = _.first(f.content.split('\n')).substring(2);

  // prepare all jade vars
  let vars = {isDoc: true};
  _.merge(vars, locals, f);
  const page = template(vars);

  // page path
  const newPath = 'site/docs/' + f.filename.toLowerCase() + '.html';

  // write out
  fs.writeFileSync(newPath, page);

  console.log('wrote', newPath);
});

// render home page

// gekko supports as many exchanges, as there are `.js` files in the exchange directory
const walkSync = require('walk-sync');
const amountOfExchanges = walkSync('../gekko/exchanges/').filter(f => _.endsWith(f, '.js')).length;

// prepare all jade vars
let vars = {
  isDoc: false,
  amountOfExchanges: amountOfExchanges

};
_.merge(vars, locals);
const page = template(vars);
// write out
fs.writeFileSync('site/index.html', page);
console.log('wrote', 'site/index.html');


ncp('static', 'site/_static');
ncp('favicons', 'site/');
@@ -270,20 +270,21 @@
 %7B%0A  
-dirs
+order
 : %7B%0A    
 orde
@@ -283,225 +283,954 @@
     
-order: %5B%0A      'introduction',%0A      'installation',%0A      'features',%0A      'strategies',%0A      'commandline',%0A      'extending',%0A      'internals'%0A    %5D%0A  %7D%0A%7D%0A%0Aconst locals = %7B%0A  root: '',%0A  version: package.version
+introduction: %5B%0A      'about_gekko',%0A      'supported_exchanges'%0A    %5D,%0A    installation: %5B%0A      'installing_gekko',%0A      'installing_gekko_on_a_server',%0A      'installing_gekko_on_windows',%0A      'Installing gekko_using_docker'%0A    %5D,%0A    features: %5B%0A      'backtesting',%0A      'importing'%0A    %5D,%0A    strategies: %5B%0A      'creating_a_strategy',%0A      'example_strategies',%0A      'gekko_indicators',%0A      'talib_indicators'%0A    %5D,%0A    commandline: %5B%0A      'about_the_commandline',%0A      'plugins',%0A      'Importing',%0A      'backtesting',%0A      'tradebot'%0A    %5D,%0A    extending: %5B%0A      'add_an_exchange',%0A      'add_a_plugin',%0A      'other_software'%0A    %5D,%0A    internals: %5B%0A      'architecture',%0A      'gekko_ui',%0A      'budfox',%0A    %5D%0A  %7D%0A%7D%0A%0Aconst locals = %7B%0A  root: '',%0A  version: package.version%0A%7D%0A%0Aconst getIndex = subtree =%3E name =%3E %7B%0A  const index = CONFIG.order%5Bsubtree%5D.indexOf(name);%0A%0A  if(index === -1)%0A    return 100;%0A%0A  return index;
 %0A%7D%0A%0A
@@ -1251,33 +1251,76 @@
 map(
+_.keys(
 CONFIG.
-dirs.
 order
+)
 , 
-f
+d
  =%3E %7B
+%0A%0A  // skip subtrees not specified above
 %0A  i
@@ -1335,17 +1335,17 @@
 nt.tree%5B
-f
+d
 %5D)%0A    r
@@ -1352,16 +1352,110 @@
 eturn;%0A%0A
+  const get = getIndex(d);%0A%0A  const subnab = content.tree%5Bd%5D.sort((a, b) =%3E get(a) - get(b))%0A%0A
   return
@@ -1467,17 +1467,17 @@
   name: 
-f
+d
 ,%0A    co
@@ -1496,17 +1496,17 @@
 nt.tree%5B
-f
+d
 %5D%0A  %7D%0A%7D)

allow specifying the order of pages in the nav