/*! sheetjs (C) SheetJS -- https://sheetjs.com */
/* 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() {
  /* initialize */
  let platform = v8::new_default_platform(0, false).make_shared();
  v8::V8::initialize_platform(platform);
  v8::V8::initialize();

  let isolate = &mut v8::Isolate::new(Default::default());
  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);

  /* load library */
  {
    let script = std::fs::read_to_string("./xlsx.full.min.js").expect("Error reading xlsx.full.min.js");
    let _result = eval_code(context_scope, &script);
  }

  /* get version string */
  {
    let result = eval_code(context_scope, "XLSX.version");
    println!("SheetJS library version {}", result);
  }

  /* read file */
  {
    let path: String = std::env::args().collect::<Vec<_>>().into_iter().nth(1).unwrap().to_string();
    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());
    println!("Loaded file {}", path);
  }

  /* parse workbook and assign to global `wb` property */
  {
    let _result = eval_code(context_scope, "void (globalThis.wb = XLSX.read(buf))");
  }

  /* print CSV of first worksheet */
  {
    let result = eval_code(context_scope, "XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]])");
    println!("{}", result);
  }

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