extern crate winapi;
extern crate log;
use winapi::um::winuser::{
    GetCursorPos, GetSystemMetrics, SM_CXSCREEN, SM_CYSCREEN,GetWindowRect
};
use winapi::shared::windef::{
    POINT,
    RECT,
};
use winapi::um::winuser::{GetForegroundWindow, GetWindowThreadProcessId};
use winapi::um::processthreadsapi::OpenProcess;
use winapi::um::winnt::PROCESS_QUERY_INFORMATION;
use winapi::um::psapi::GetProcessImageFileNameW;

pub fn screen_size() -> (i32, i32) {
    unsafe {
        let width = GetSystemMetrics(SM_CXSCREEN);
        let height = GetSystemMetrics(SM_CYSCREEN);
        (width, height)
    }
}

pub fn cursor_pos() -> (i32, i32) {
    let mut point = POINT { x: 0, y: 0 };
    unsafe {
        GetCursorPos(&mut point);
        let x = point.x;
        let y = point.y;
        (x, y)
    }
}

pub fn get_foredround_window_rect() -> Option<(i32, i32, i32, i32)> {
    unsafe {
        let h = GetForegroundWindow();

        let mut rect: RECT = std::mem::zeroed();
        if GetWindowRect(h, &mut rect) != 0 {
            println!("GetWindowRect: {:?}", rect);
            Some((rect.left, rect.top, rect.right, rect.bottom))
        } else {
            log::error!("Cant get foreground window rect");
            None
        }
    }
}

pub fn get_foredround_window_name() -> String {
    let full_path;
    unsafe {
        let foreground_window = GetForegroundWindow();

        let mut process_id = 0;
        GetWindowThreadProcessId(foreground_window, &mut process_id);

        // let mut process_name: [u16; 1024] = [0; 1024];
        // GetWindowTextW(foreground_window, process_name.as_mut_ptr(), 1024);

        let process_handle = OpenProcess(PROCESS_QUERY_INFORMATION, 0, process_id);
        if process_handle.is_null() {
            log::warn!("process {} cant process handle!", process_id);
            return "".to_string();
        }

        let mut image_name = [0u16; 1024];
        if GetProcessImageFileNameW(process_handle, image_name.as_mut_ptr() as *mut _, image_name.len() as u32) == 0 {
            log::error!("Cant Get foreground prog name!");
            return "".to_string();
        }

        // let mut class_name: [u16; 256] = [0; 256];
        // GetClassNameW(foreground_window, class_name.as_mut_ptr(), 256);

        full_path = String::from_utf16_lossy(&image_name);
        // let mut class_name_str = OsString::from_wide(&class_name);
        // log::info!("Class Name: {}", class_name_str.to_string_lossy().to_string());
    }
    let name = full_path.split('\\').last().unwrap().trim_end_matches('\0').to_string();
    //let name = path.file_name().unwrap().to_string_lossy().to_string();
    // println!("Current Proc Name: [{}]", name);
    name

}


#[test]
fn test_screen_size() {
    // env_logger::init();
    let (width, height) = screen_size();
    assert!(width > 0);
    assert!(height > 0);
}
#[test]
fn test_get_foredround_window_rect() {
    // env_logger::init();
    let (_x, _y, width, height) = get_foredround_window_rect().unwrap();
    // assert!(x >= 0);
    // assert!(y >= 0);
    assert!(width > 0);
    assert!(height > 0);
}
#[test]
fn test_cursor_pos() {
    // env_logger::init();
    let (x, y) = cursor_pos();
    assert!(x >= 0);
    assert!(y >= 0);
}

#[test]
fn test_get_foredround_window_name() {
    env_logger::init();
    get_foredround_window_name();
}