use std::ffi::{c_char, CString};

#[repr(C)]
pub struct Color(pub u8, pub u8, pub u8, pub u8);

#[link(name = "raylib")]
extern "C" {
    fn InitWindow(width: u32, height: u32, title: *const c_char);
    fn ClearBackground(color: Color);
    fn WindowShouldClose() -> bool;
    fn SetTargetFPS(fps: u32);
    fn BeginDrawing();
    fn EndDrawing();

    fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color);
    fn DrawCircleV(center: Vector2, radius: f32, color: Color);

    fn IsKeyDown(key: u32) -> bool;
    fn GetFrameTime() -> f32;
}

#[derive(Copy, Clone)]
pub enum Key {
    W = 87,
    S = 83,
    A = 65,
    D = 68,
}

pub mod colors {
    use super::Color;
    pub const RED: Color = Color(255, 0, 0, 255);
    pub const BLACK: Color = Color(0, 0, 0, 0);
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Vector2 {
    pub x: f32,
    pub y: f32,
}

pub fn init_window(width: u32, height: u32, title: &str) {
    unsafe {
        let title = CString::new(title).unwrap();
        InitWindow(width, height, title.as_ptr());
    }
}

pub fn clear_background(c: Color) {
    unsafe {
        ClearBackground(c);
    }
}

pub fn window_should_close() -> bool {
    unsafe { WindowShouldClose() }
}

pub fn set_target_fps(fps: u32) {
    unsafe {
        SetTargetFPS(fps);
    }
}

pub fn begin_drawing() {
    unsafe {
        BeginDrawing();
    }
}

pub fn end_drawing() {
    unsafe {
        EndDrawing();
    }
}

pub fn draw_rect(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color) {
    unsafe {
        DrawRectangle(pos_x, pos_y, width, height, color);
    }
}

pub fn draw_circle_v(center: Vector2, radius: f32, color: Color) {
    unsafe { DrawCircleV(center, radius, color) }
}

pub fn is_key_down(key: Key) -> bool {
    unsafe { IsKeyDown(key as u32) }
}

pub fn get_frame_time() -> f32 {
    unsafe { GetFrameTime() }
}
