/* 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);
}

/* assuming JS code returns an ArrayBuffer, copy result to a Vec<u8> */
fn eval_code_ab(scope: &mut v8::HandleScope, code: &str) -> Vec<u8> {
  let source = v8::String::new(scope, code).unwrap();
  let script = v8::Script::compile(scope, source, None).unwrap();
  let result: v8::Local<v8::ArrayBuffer> = script.run(scope).unwrap().try_into().unwrap();
  unsafe { return std::slice::from_raw_parts_mut(result.data().unwrap().cast::<u8>().as_ptr(), result.byte_length()).to_vec(); }
}

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.next() {
    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, Default::default());
  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, &format!("var wb = XLSX.read(buf, {{dense: true}}); XLSX.utils.sheet_to_csv(wb.Sheets[{}])", sheetname));
    println!("{}", result);
  }

  /* write sheetjsw.numbers */
  {
    let result = eval_code_ab(context_scope, "XLSX.write(wb, {type:'array', bookType:'numbers', numbers: XLSX_ZAHL_PAYLOAD})");
    std::fs::write("sheetjsw.numbers", result).unwrap();
  }
}