use core::{ffi::c_double, time::Duration};

use crate::{
    error::{self, CIntZeroResult, CResult},
    headers::{errno::Errno, sys_time::__ygg_timespec_t, sys_types::time_t},
    process,
    util::PointerExt,
};

#[no_mangle]
unsafe extern "C" fn nanosleep(
    rqtp: *const __ygg_timespec_t,
    rmtp: *mut __ygg_timespec_t,
) -> CIntZeroResult {
    let rqtp = rqtp.ensure();
    let amount = Duration::from(*rqtp);

    match process::sleep(amount) {
        Ok(()) => CIntZeroResult::SUCCESS,
        Err(remaining) => {
            if let Some(rmtp) = rmtp.as_mut() {
                *rmtp = remaining.into();
            }
            error::set_errno(Errno::EINTR);
            CIntZeroResult::ERROR
        }
    }
}

#[no_mangle]
unsafe extern "C" fn time(_tp: *mut time_t) -> time_t {
    time_t(0)
}

#[no_mangle]
unsafe extern "C" fn tzset() {
    todo!()
}

#[no_mangle]
unsafe extern "C" fn difftime(_a: time_t, _b: time_t) -> c_double {
    todo!()
}
