/* run code, get result as a Rust String */
fn eval_code(scope: &mut v8::HandleScope, code: &str) -> std::string::String {
  let source = v8::String::new(scope, &code).unwrap();
  let script = v8::Script::compile(scope, source, None).unwrap();
  let result = script.run(scope).unwrap();
  return result.to_string(scope).unwrap().to_rust_string_lossy(scope);
}

fn main() {
  /* parse arguments */
  let mut iter = std::env::args();
  let path: String = iter.nth(1).expect("must specify a file name");
  let sheetname: String = match iter.nth(0) {
    Some(v) => format!("'{}'", v),
    None => "wb.SheetNames[0]".to_string()
  };

  /* initialize */
  let platform = v8::new_default_platform(0, false).make_shared();
  v8::V8::initialize_platform(platform);
  v8::V8::initialize();

  /* read snapshot */
  let startup_data: Vec<u8> = include_bytes!("./snapshot.bin").to_vec();
  let params = v8::Isolate::create_params().snapshot_blob(startup_data);
  let isolate = &mut v8::Isolate::new(params);
  let handle_scope = &mut v8::HandleScope::new(isolate);
  let context = v8::Context::new(handle_scope);
  let context_scope = &mut v8::ContextScope::new(handle_scope, context);

  /* read file */
  let data: Vec<u8> = std::fs::read(path.clone()).unwrap();
  let back: v8::UniqueRef<v8::BackingStore> = v8::ArrayBuffer::new_backing_store_from_vec(data);
  let shared = back.make_shared();
  let ab: v8::Local<v8::ArrayBuffer> = v8::ArrayBuffer::with_backing_store(context_scope, &shared);
  let key = v8::String::new(context_scope, "buf").unwrap();
  context.global(context_scope).set(context_scope, key.into(), ab.into());

  /* print CSV of specified sheet */
  let result = eval_code(context_scope, &mut format!("var wb = XLSX.read(buf, {{dense: true}}); XLSX.utils.sheet_to_csv(wb.Sheets[{}])", sheetname));
  println!("{}", result);
}