use gtk::{glib, gdk};
use std::ffi::CStr;
use std::ptr;

// Storage for the command line argument string
pub static mut DEADZONE_ARG: *mut std::os::raw::c_char = ptr::null_mut();

pub const DEADZONE_LONG: &[u8] = b"deadzone\0";
pub const DEADZONE_DESC: &[u8] = b"Area to avoid spawning sprites (x,y,w,h)\0";
pub const DEADZONE_ARG_DESC: &[u8] = b"x,y,w,h\0";

pub struct ModuleConfig {
    pub deadzone: Option<gdk::Rectangle>,
}

impl ModuleConfig {
    pub unsafe fn from_args() -> Self {
        let mut deadzone = None;
        if !DEADZONE_ARG.is_null() {
            let c_str = CStr::from_ptr(DEADZONE_ARG);
            if let Ok(s) = c_str.to_str() {
                let parts: Vec<&str> = s.split(',').collect();
                if parts.len() == 4 {
                    if let (Ok(x), Ok(y), Ok(w), Ok(h)) = (parts[0].parse(), parts[1].parse(), parts[2].parse(), parts[3].parse()) {
                        deadzone = Some(gdk::Rectangle::new(x, y, w, h));
                        println!("[ahfail] Configured deadzone: {:?}", deadzone);
                    } else {
                        eprintln!("[ahfail] Invalid numbers in deadzone argument: {}", s);
                    }
                } else {
                    eprintln!("[ahfail] Invalid format for deadzone argument (expected x,y,w,h): {}", s);
                }
            }
        }
        Self { deadzone }
    }
}
