//! C FFI for cpdf annotation operations.

use super::document::{CPDF_DOCS, CPDF_RANGES};
use super::{clear_error, set_error};
use crate::cpdf::annotations;
use crate::ffi::Handle;

#[unsafe(no_mangle)]
pub extern "C" fn cpdf_getAnnotationsJSON(
    doc: Handle,
    range_h: Handle,
    len_out: *mut usize,
) -> *mut u8 {
    clear_error();
    let d = match CPDF_DOCS.get(doc) {
        Some(arc) => arc,
        None => {
            set_error(1, &format!("invalid handle: {doc}"));
            return std::ptr::null_mut();
        }
    };
    let r = match CPDF_RANGES.get(range_h) {
        Some(arc) => arc.lock().unwrap().clone(),
        None => {
            set_error(1, &format!("invalid range: {range_h}"));
            return std::ptr::null_mut();
        }
    };
    let guard = d.lock().unwrap();
    match annotations::get_annotations_json(&guard, &r) {
        Ok(json) => {
            let bytes = json.into_bytes();
            let len = bytes.len();
            let ptr = unsafe {
                let layout = std::alloc::Layout::from_size_align_unchecked(len, 1);
                let p = std::alloc::alloc(layout);
                if p.is_null() {
                    set_error(1, "alloc failed");
                    return std::ptr::null_mut();
                }
                std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, len);
                p
            };
            if !len_out.is_null() {
                unsafe {
                    *len_out = len;
                }
            }
            ptr
        }
        Err(e) => {
            set_error(1, &e.to_string());
            std::ptr::null_mut()
        }
    }
}

#[unsafe(no_mangle)]
pub extern "C" fn cpdf_removeAnnotations(doc: Handle, range_h: Handle) -> Handle {
    clear_error();
    let d = match CPDF_DOCS.get(doc) {
        Some(arc) => arc.lock().unwrap().clone(),
        None => {
            set_error(1, &format!("invalid handle: {doc}"));
            return 0;
        }
    };
    let r = match CPDF_RANGES.get(range_h) {
        Some(arc) => arc.lock().unwrap().clone(),
        None => {
            set_error(1, &format!("invalid range: {range_h}"));
            return 0;
        }
    };
    match annotations::remove_annotations(d, &r) {
        Ok(doc) => CPDF_DOCS.insert(doc),
        Err(e) => {
            set_error(1, &e.to_string());
            0
        }
    }
}
