#!/usr/bin/env node --experimental-modules



const yargs = require("yargs"),
  fs = require('fs');

const options = yargs
  .usage("Usage: -n <name>")
  .option("r", { alias: "random", describe: "full random photo and multiple generator", type: "boolean"})
  .option("m", { alias: "multiple", describe: "multiple answers", type: "boolean"})
  .option("p", { alias: "photo", describe: "quiz with photo", type: "boolean"})
  .option("s", { alias: "seed", describe: "random seed", type: "string"})
  .option("v", { alias: "verbose", describe: "print log", type: "string"})
  .option("n", { alias: "nolog", describe: "no text log in object", type: "boolean"})

  .argv;

const greeting = `Hello, ${options.name}!`;





const sources = [
  "js/releasable-observer.js",
  "js/pcg-base.js",
  "js/pcg-dom-util.js",

  "js/helpers/rand.js",

  "js/model/store.js",
  "js/model/data.js",

  "js/model/provider.js",
  "js/model/slice/productTable.js",

  "js/model/tag.js",
  "js/model/product.js",

  "js/helpers/textFilter.js",
  "js/helpers/answer.js",

  "js/controller/quizBits/main.js",

  "js/controller/quizGenerator.js",
  "js/controller/quizBits/checkbox.js",
  "js/controller/quizBits/radio.js"
];

var js = sources.map(a=>'./public/'+a).map(n=>fs.readFileSync(n)+'').join('\n\n');

const data = require("../db.js");
const body = 'const window = {};'+js+'; return {quizGenerator, initDataProvider, seeded: Math.random.seeded}';
var ctx = new Function('',body)();
//console.log(options);
data.after = function() {

    ctx.initDataProvider( data );

  if(options.seed)
    ctx.seeded.setStringSeed(options.seed);

  const result = ctx.quizGenerator( options.multiple?'checkbox':'radio', options.photo);

  if(options.verbose){
    console.log( result.log.join( '\n' ) )
  }else{
    if(options.nolog)
      delete result.log;
    console.log('--- START ---\n\n'+JSON.stringify(result,null,2)+'\n\n--- END ---')
  }
};


//console.log(greeting);
/*const fs = require('fs');
fs.readFileSync()*/
/*

 */