#![allow(unused)]
#![allow(nonstandard_style)]

use crate::collections::HashMap;
use crate::ffi::OsString;
use crate::io::{Error as IoError, ErrorKind, RawOsError};
use crate::mem::MaybeUninit;
use crate::str::FromStr;

#[path = "../unix/cmath.rs"]
pub mod cmath;
#[path = "../unix/os_str.rs"]
pub mod os_str;

pub mod alloc;
pub mod args;
pub mod env;
pub mod fd;
pub mod fs;
pub mod io;
pub mod locks;
pub mod memchr;
pub mod net;
pub mod once;
pub mod os;
pub mod path;
pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
pub mod thread_local_key;
pub mod time;

pub(crate) mod signal;

pub(self) use yggdrasil_rt;

mod util;

use yggdrasil_rt::process::ExitCode as OsExitCode;
use yggdrasil_rt::Error as OsError;

use crate::path::Path;

// Argument structure layout:
//
// 0x000: args pointer
// 0x008: envs pointer
static mut ARGS: usize = 0;
// TODO handle this in better way
static mut ENVS: MaybeUninit<HashMap<OsString, OsString>> = MaybeUninit::uninit();

#[doc(hidden)]
pub trait IsNegative {
    fn is_negative(&self) -> bool;
    fn negate(&self) -> i32;
}

macro_rules! impl_is_negative {
    ($($t:ident)*) => ($(impl IsNegative for $t {
        fn is_negative(&self) -> bool {
            *self < 0
        }

        fn negate(&self) -> i32 {
            i32::try_from(-(*self)).unwrap()
        }
    })*)
}

impl IsNegative for i32 {
    fn is_negative(&self) -> bool {
        *self < 0
    }

    fn negate(&self) -> i32 {
        -(*self)
    }
}

impl_is_negative! { i8 i16 i64 isize }

fn cvt_io_err(e: OsError) -> IoError {
    let raw_os_error = u32::from(e) as RawOsError;
    IoError::from_raw_os_error(raw_os_error)
}

#[inline]
pub fn run_with_path_str<T, F: FnOnce(&str) -> T>(path: &Path, f: F) -> T {
    let path_str = path.to_str().unwrap();
    f(path_str)
}

pub fn cvt_io<T>(t: Result<T, OsError>) -> crate::io::Result<T> {
    match t {
        Ok(t) => Ok(t),
        Err(e) => Err(cvt_io_err(e)),
    }
}

pub fn cvt<T: IsNegative>(_t: T) -> crate::io::Result<T> {
    println!("cvt!");
    loop {}
}

pub fn cvt_r<T, F>(_f: F) -> crate::io::Result<T>
where
    T: IsNegative,
    F: FnMut() -> T,
{
    println!("cvt_r!");
    loop {}
}

pub fn hashmap_random_keys() -> (u64, u64) {
    unsafe {
        yggdrasil_rt::sys::debug_trace("sys::yggdrasil::hashmap_random_keys()");
    }

    // Bruh
    (1234, 4321)
}

pub fn abort_internal() -> ! {
    unsafe {
        yggdrasil_rt::sys::debug_trace("sys::yggdrasil::abort_internal()");
    }
    loop {}
}

pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
    yggdrasil_rt::sys::debug_trace("sys::yggdrasil::init()");
}

pub unsafe fn cleanup() {
    yggdrasil_rt::sys::debug_trace("sys::yggdrasil::cleanup()");
}

pub fn decode_error_kind(errno: i32) -> ErrorKind {
    let errno = errno as u32;
    match OsError::try_from(errno) {
        // I/O kinds
        Ok(OsError::DoesNotExist) => ErrorKind::NotFound,
        Ok(OsError::AlreadyExists) => ErrorKind::AlreadyExists,
        Ok(OsError::TimedOut) => ErrorKind::TimedOut,
        Ok(OsError::IsADirectory) => ErrorKind::IsADirectory,
        Ok(OsError::NotADirectory) => ErrorKind::NotADirectory,
        Ok(OsError::DirectoryNotEmpty) => ErrorKind::DirectoryNotEmpty,
        Ok(OsError::Interrupted) => ErrorKind::Interrupted,
        Ok(OsError::WouldBlock) => ErrorKind::WouldBlock,

        // Memory and general
        Ok(OsError::OutOfMemory) => ErrorKind::OutOfMemory,
        Ok(OsError::InvalidArgument) | Ok(OsError::InvalidMemoryOperation) => {
            ErrorKind::InvalidInput
        }
        Ok(OsError::NotImplemented) => ErrorKind::Unsupported,

        // Uncategorized
        Ok(OsError::InvalidFile) => ErrorKind::Uncategorized,
        Err(_) => ErrorKind::Uncategorized,
    }
}

unsafe fn init_kernel_arg(program_arg: usize) {
    #[repr(C)]
    struct Header {
        args_ptr: usize,
        envs_ptr: usize,
    };

    #[allow(fuzzy_provenance_casts)]
    let hdr = program_arg as *const Header;

    ARGS = (*hdr).args_ptr;
    let env_list = util::KStringList::new((*hdr).envs_ptr);

    let mut envs = ENVS.write(HashMap::new());

    for i in 0..env_list.len() {
        let line = env_list.get(i);
        let Some((key, value)) = line.split_once('=') else {
            continue;
        };
        let key = OsString::from_str(key).unwrap();
        let value = OsString::from_str(value).unwrap();

        envs.insert(key, value);
    }
}

#[cfg(not(test))]
#[no_mangle]
pub unsafe extern "C" fn runtime_entry(program_arg: usize) -> ! {
    use crate::ffi::c_char;
    use crate::ptr::null;

    extern "C" {
        fn main(argc: isize, argv: *const *const c_char) -> i32;
    }

    init_kernel_arg(program_arg);

    // Initialize signals
    signal::signal_init();

    let result = main(0, null());

    yggdrasil_rt::sys::exit(OsExitCode::Exited(result));
}
