//! Implemented drawing area for Cairo.

use super::glib::*;
use cairo_sys::*;
use gdk_pixbuf_sys::*;
use gdk_sys::*;
use pango_sys::*;
use pango_cairo_sys::*;
use maraiah::{c_str,
              durandal::{ffi, image::*},
              rozinante::draw};

/// Converts a `Color` to a `f64` triple.
fn flt_color(cr: impl Color) -> (f64, f64, f64)
{
   fn flt_color(n: u16) -> f64 {f64::from(n) / f64::from(u16::max_value())}

   (flt_color(cr.r()), flt_color(cr.g()), flt_color(cr.b()))
}

impl draw::CacheImage for CrImage
{
   fn w(&self) -> draw::Coord
   {
      unsafe {gdk_pixbuf_get_width(self.0) as draw::Coord}
   }

   fn h(&self) -> draw::Coord
   {
      unsafe {gdk_pixbuf_get_height(self.0) as draw::Coord}
   }
}

impl CrDrawArea
{
   /// Creates a new `CrDrawArea`.
   pub fn new(ctx: *mut cairo_t, w: f64, h: f64) -> Self
   {
      let pan = unsafe {
         let pan = pango_cairo_create_layout(ctx);
         let dsc = pango_font_description_from_string(c_str!("Monospace 12"));
         pango_layout_set_font_description(pan, dsc);
         pango_font_description_free(dsc);
         pan
      };

      let pan = Refc::new(pan);

      CrDrawArea{ctx, pan, w: w as draw::Coord, h: h as draw::Coord}
   }
}

impl draw::DrawArea for CrDrawArea
{
   type NativeImage = CrImage;

   fn w(&self) -> draw::Coord {self.w}
   fn h(&self) -> draw::Coord {self.h}

   fn clear(&mut self, cr: impl Color)
   {
      self.rect(draw::Rect{x: 0, y: 0, w: self.w(), h: self.h()}, cr);

      unsafe {
         cairo_set_line_width(self.ctx, 1.0);
      }
   }

   fn line_width(&mut self, width: u8)
   {
      let width = f64::from(width);

      unsafe {
         cairo_set_line_width(self.ctx, width);
      }
   }

   fn line(&mut self, p1: draw::Point, p2: draw::Point, cr: impl Color)
   {
      let (r, g, b) = flt_color(cr);

      let x1 = f64::from(p1.0);
      let y1 = f64::from(p1.1);

      let x2 = f64::from(p2.0);
      let y2 = f64::from(p2.1);

      unsafe {
         cairo_set_source_rgb(self.ctx, r, g, b);
         cairo_move_to(self.ctx, x1, y1);
         cairo_line_to(self.ctx, x2, y2);
         cairo_stroke(self.ctx);
      }
   }

   fn rect(&mut self, rect: draw::Rect, cr: impl Color)
   {
      let px = f64::from(rect.x);
      let py = f64::from(rect.y);
      let sx = f64::from(rect.w);
      let sy = f64::from(rect.h);

      let (r, g, b) = flt_color(cr);

      unsafe {
         cairo_set_source_rgb(self.ctx, r, g, b);
         cairo_rectangle(self.ctx, px, py, sx, sy);
         cairo_fill(self.ctx);
      }
   }

   fn text(&mut self, pos: draw::Point, text: &str, cr: impl Color)
   {
      let (r, g, b) = flt_color(cr);

      let x = f64::from(pos.0);
      let y = f64::from(pos.1);

      let tlen = text.len()    as ffi::c_int;
      let text = text.as_ptr() as ffi::NT;

      unsafe {
         cairo_set_source_rgb(self.ctx, r, g, b);
         cairo_move_to(self.ctx, x, y);

         pango_layout_set_markup(*self.pan, text, tlen);
         pango_cairo_update_layout(self.ctx, *self.pan);
         pango_cairo_show_layout(self.ctx, *self.pan);
      }
   }

   fn image(&mut self, pos: draw::Point, im: &Self::NativeImage)
   {
      let x = f64::from(pos.0);
      let y = f64::from(pos.1);

      unsafe {
         gdk_cairo_set_source_pixbuf(self.ctx, im.0, x, y);
         cairo_paint(self.ctx);
      }
   }
}

/// An image for a `CrDrawArea`.
pub struct CrImage(pub *const GdkPixbuf);

/// A `DrawArea` for a Cairo surface.
pub struct CrDrawArea
{
   ctx: *mut cairo_t,
   pan: Refc<'static, PangoLayout>,
   w:   draw::Coord,
   h:   draw::Coord,
}

// EOF
