use core::{
    cmp::Ordering,
    ffi::{c_char, c_int, c_void},
};

use super::{locale::locale_t, string::mem::memmove};

#[no_mangle]
unsafe extern "C" fn ffs(_f: c_int) -> c_int {
    todo!()
}

#[no_mangle]
unsafe extern "C" fn strcasecmp(s1: *const c_char, s2: *const c_char) -> c_int {
    strncasecmp(s1, s2, usize::MAX)
}

#[no_mangle]
unsafe extern "C" fn strcasecmp_l(
    _s1: *const c_char,
    _s2: *const c_char,
    _locale: locale_t,
) -> c_int {
    todo!()
}

#[allow(clippy::unnecessary_cast)]
#[no_mangle]
unsafe extern "C" fn strncasecmp(s1: *const c_char, s2: *const c_char, n: usize) -> c_int {
    if s1.is_null() {
        if s2.is_null() {
            return 0;
        } else {
            return 1;
        }
    } else if s2.is_null() {
        return -1;
    }

    for i in 0..n {
        let c0 = (*s1.add(i) as u8).to_ascii_lowercase();
        let c1 = (*s2.add(i) as u8).to_ascii_lowercase();

        match c0.cmp(&c1) {
            Ordering::Less => return -1,
            Ordering::Equal => (),
            Ordering::Greater => return 1,
        }

        if c0 == 0 {
            break;
        }
    }

    0
}

#[no_mangle]
unsafe extern "C" fn strncasecmp_l(
    _s1: *const c_char,
    _s2: *const c_char,
    _n: usize,
    _locale: locale_t,
) -> c_int {
    todo!()
}

#[deprecated]
#[no_mangle]
unsafe extern "C" fn bcopy(src: *const c_void, dst: *mut c_void, n: usize) {
    memmove(dst, src, n);
}
