//! C FFI for cpdf page labels.

use super::document::CPDF_DOCS;
use super::{clear_error, set_error};
use crate::cpdf::page_labels;
use crate::ffi::Handle;
use std::ffi::{CStr, c_char, c_int};

#[unsafe(no_mangle)]
pub extern "C" fn cpdf_addPageLabels(
    doc: Handle,
    start_page: c_int,
    style: c_int,
    prefix: *const c_char,
    start_value: c_int,
) -> 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 s = match style {
        0 => page_labels::LabelStyle::DecimalArabic,
        1 => page_labels::LabelStyle::RomanUpper,
        2 => page_labels::LabelStyle::RomanLower,
        3 => page_labels::LabelStyle::AlphaUpper,
        4 => page_labels::LabelStyle::AlphaLower,
        _ => page_labels::LabelStyle::NoNumber,
    };
    let p = if prefix.is_null() {
        ""
    } else {
        unsafe { CStr::from_ptr(prefix) }.to_str().unwrap_or("")
    };
    match page_labels::add_page_labels(
        d,
        start_page.max(0) as usize,
        s,
        p,
        start_value.max(1) as usize,
    ) {
        Ok(doc) => CPDF_DOCS.insert(doc),
        Err(e) => {
            set_error(1, &e.to_string());
            0
        }
    }
}

#[unsafe(no_mangle)]
pub extern "C" fn cpdf_removePageLabels(doc: 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;
        }
    };
    match page_labels::remove_page_labels(d) {
        Ok(doc) => CPDF_DOCS.insert(doc),
        Err(e) => {
            set_error(1, &e.to_string());
            0
        }
    }
}

#[unsafe(no_mangle)]
pub extern "C" fn cpdf_getPageLabelsJSON(doc: 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 guard = d.lock().unwrap();
    match page_labels::get_page_labels_json(&guard) {
        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()
        }
    }
}
