use std::ffi::CStr;
use std::ptr::NonNull;
use std::cell::RefCell;
use crate::c_api::{mts_status_t, MTS_SUCCESS, mts_last_error};
const RUST_FUNCTION_FAILED_ERROR_CODE: i32 = -4242;
thread_local! {
    pub static LAST_RUST_ERROR: RefCell<Error> = RefCell::new(Error {code: None, message: String::new()});
}
#[derive(Debug, Clone)]
pub struct Error {
    pub code: Option<mts_status_t>,
    pub message: String
}
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)?;
        if let Some(code) = self.code {
            write!(f, " (code {})", code)?;
        }
        Ok(())
    }
}
impl std::error::Error for Error {}
pub fn check_status(status: mts_status_t) -> Result<(), Error> {
    if status == MTS_SUCCESS {
        return Ok(())
    } else if status > 0 {
        let message = unsafe {
            CStr::from_ptr(mts_last_error())
        };
        let message = message.to_str().expect("invalid UTF8");
        return Err(Error { code: Some(status), message: message.to_owned() });
    } else if status == RUST_FUNCTION_FAILED_ERROR_CODE {
        return Err(LAST_RUST_ERROR.with(|e| e.borrow().clone()));
    } else {
        return Err(Error { code: Some(status), message: "external function call failed".into() });
    }
}
pub fn check_ptr<T>(ptr: *mut T) -> Result<NonNull<T>, Error> {
    if let Some(ptr) = NonNull::new(ptr) {
        return Ok(ptr);
    } else {
        let message = unsafe {
            CStr::from_ptr(mts_last_error())
        };
        let message = message.to_str().expect("invalid UTF8");
        return Err(Error { code: None, message: message.to_owned() });
    }
}
impl From<Box<dyn std::any::Any + Send + 'static>> for Error {
    fn from(error: Box<dyn std::any::Any + Send + 'static>) -> Error {
        let message = if let Some(message) = error.downcast_ref::<String>() {
            message.clone()
        } else if let Some(message) = error.downcast_ref::<&str>() {
            (*message).to_owned()
        } else {
            panic!("panic message is not a string, something is very wrong")
        };
        return Error {
            code: None, message,
        }
    }
}
pub(crate) fn catch_unwind<F>(function: F) -> mts_status_t where F: FnOnce() + std::panic::UnwindSafe {
    match std::panic::catch_unwind(function) {
        Ok(()) => MTS_SUCCESS,
        Err(e) => {
            LAST_RUST_ERROR.with(|last_error| {
                let mut last_error = last_error.borrow_mut();
                *last_error = e.into();
            });
            RUST_FUNCTION_FAILED_ERROR_CODE
        }
    }
}