//! C++ functions.
#![allow(dead_code)]

use maraiah::ffi;

mod static_ffi {
	use maraiah::ffi;

	#[repr(C)]
	pub struct Int2 {
		pub x: i16,
		pub y: i16,
	}

	pub type QPainter = ffi::c_void;

	extern "C" {
		pub fn critical_msg(title: ffi::NT, msg: ffi::NT);

		pub fn paint_arc(paint: *mut QPainter,
		                 x:     i16,
		                 y:     i16,
		                 w:     i16,
		                 h:     i16,
		                 t:     i16,
		                 p:     i16);

		pub fn paint_chord(paint: *mut QPainter,
		                   x:     i16,
		                   y:     i16,
		                   w:     i16,
		                   h:     i16,
		                   t:     i16,
		                   p:     i16);

		pub fn paint_ellipse(paint: *mut QPainter,
		                     x:     i16,
		                     y:     i16,
		                     w:     i16,
		                     h:     i16);

		pub fn paint_image(paint: *mut QPainter, x: i16, y: i16, fname: ffi::NT);

		pub fn paint_line(paint: *mut QPainter,
		                  x1:    i16,
		                  y1:    i16,
		                  x2:    i16,
		                  y2:    i16);

		pub fn paint_point(paint: *mut QPainter, x: i16, y: i16);

		pub fn paint_polygon(paint:  *mut QPainter,
		                     points: *const Int2,
		                     len:    usize);

		pub fn paint_rect(paint: *mut QPainter, x: i16, y: i16, w: i16, h: i16);

		pub fn paint_squircle(paint: *mut QPainter,
		                      x:     i16,
		                      y:     i16,
		                      w:     i16,
		                      h:     i16,
		                      xr:    i16,
		                      yr:    i16);

		pub fn paint_text(paint: *mut QPainter, x: i16, y: i16, text: ffi::NT);
		pub fn paint_fg(paint: *mut QPainter, r: u8, g: u8, b: u8, a: u8);
	}
}

pub use static_ffi::{Int2, QPainter};

pub fn paint_arc(paint: *mut QPainter,
                 x:     i16,
                 y:     i16,
                 w:     i16,
                 h:     i16,
                 t:     i16,
                 p:     i16)
{
	unsafe {
		static_ffi::paint_arc(paint, x, y, w, h, t, p);
	}
}

pub fn paint_chord(paint: *mut QPainter,
                   x:     i16,
                   y:     i16,
                   w:     i16,
                   h:     i16,
                   t:     i16,
                   p:     i16)
{
	unsafe {
		static_ffi::paint_chord(paint, x, y, w, h, t, p);
	}
}

pub fn paint_ellipse(paint: *mut QPainter, x: i16, y: i16, w: i16, h: i16)
{
	unsafe {
		static_ffi::paint_ellipse(paint, x, y, w, h);
	}
}

pub fn paint_image(paint: *mut QPainter, x: i16, y: i16, fname: &str)
{
	let fname = ffi::CString::new(fname).unwrap();

	unsafe {
		static_ffi::paint_image(paint, x, y, fname.as_ptr());
	}
}

pub fn paint_line(paint: *mut QPainter, x1: i16, y1: i16, x2: i16, y2: i16)
{
	unsafe {
		static_ffi::paint_line(paint, x1, y1, x2, y2);
	}
}

pub fn paint_point(paint: *mut QPainter, x: i16, y: i16)
{
	unsafe {
		static_ffi::paint_point(paint, x, y);
	}
}

pub fn paint_polygon(paint: *mut QPainter, points: &[Int2])
{
	unsafe {
		static_ffi::paint_polygon(paint, points.as_ptr(), points.len());
	}
}

pub fn paint_rect(paint: *mut QPainter, x: i16, y: i16, w: i16, h: i16)
{
	unsafe {
		static_ffi::paint_rect(paint, x, y, w, h);
	}
}

pub fn paint_squircle(paint: *mut QPainter,
                      x:     i16,
                      y:     i16,
                      w:     i16,
                      h:     i16,
                      r1:    i16,
                      r2:    i16)
{
	unsafe {
		static_ffi::paint_squircle(paint, x, y, w, h, r1, r2);
	}
}

pub fn paint_text(paint: *mut QPainter, x: i16, y: i16, text: &str)
{
	let text = ffi::CString::new(text).unwrap();

	unsafe {
		static_ffi::paint_text(paint, x, y, text.as_ptr());
	}
}

pub fn paint_fg(paint: *mut QPainter, r: u8, g: u8, b: u8, a: u8)
{
	unsafe {
		static_ffi::paint_fg(paint, r, g, b, a);
	}
}

pub fn critical_msg<T, U>(title: T, msg: U)
	where T: ToString,
	      U: ToString
{
	let title = ffi::CString::new(title.to_string()).unwrap();
	let msg   = ffi::CString::new(msg.to_string()).unwrap();

	unsafe {
		static_ffi::critical_msg(title.as_ptr(), msg.as_ptr());
	}
}

// EOF
