import * as fs from 'fs';

import * as Util from '@dra2020/util';
import * as FSM from '@dra2020/fsm';

import * as Poly from '../lib/poly';
import * as Q from '../lib/quad';

let data: any = null;

function loadData(): void
{
  if (process.argv.length > 2)
  {
    try
    {
      let s: string = fs.readFileSync(process.argv[process.argv.length-1], 'utf8');
      data = JSON.parse(s);
    }
    catch (err)
    {
      console.error(`File load/parse failed: ${err}`);
      process.exit(1);
    }
  }
  else
  {
    console.error('No data file specified.');
    process.exit(1);
  }
}

loadData();

/*

TODO: Rewrite with FSM

function runTest(env: FSM.FsmEnvironment, options: Poly.TickOptions)
{
  let elapsed = new Util.Elapsed();
  let tree = new Q.FsmQuadTree(env, options, data);
  let u = tree.union();
  console.log(`union at granularity ${options.maxLeafCount}: ${elapsed.ms()}`);
}

function runTests()
{
  let sizes: number[] = [ 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 ];

  for (let i: number = 0; i < sizes.length; i++)
    runTest({ maxLeafCount: sizes[i], maxDepth: 20 });

}

runTests();

*/

let asyncTime = new Util.Elapsed();
let env: FSM.FsmEnvironment = { fsmManager: new FSM.FsmManager() };
let fsm = new Q.FsmQuadTree(env, { maxLeafCount: 256, maxDepth: 32 }, data);
new FSM.FsmOnDone(env, fsm, (q: Q.FsmQuadTree) => {
    console.log(`asyncUnion at granularity 256: ${asyncTime.ms()}`);
  });
