#include "zori.h"
#include "zori_widget.h"
#include "draw.h"





/* Draws a widget's frame based on it's style. If the background bitmap is set 
 * then that is used, otherwise,  the background color is used, including a border
 * if needed. */
void zori_widget_draw_background(struct zori_widget * widget) {
    float dx, dy, dw, dh;
    struct zori_style * style = &widget->style;
    zori_bitmap * background  = style->back.bitmap;
    zori_color color = style->back.color;
    struct zori_stylepart * part =  & style->back;
    dx = rebox_x(&widget->box);
    dy = rebox_y(&widget->box);
    dw = rebox_w(&widget->box);
    dh = rebox_h(&widget->box);
    
    if (zori_widget_hover(widget)) {
      part = &style->hover;  
    }
    
    if (zori_widget_marked(widget)) {
      part = &style->mark;
    }
    
    if (part->bitmap) {
      background = part->bitmap;
    }
    
    color = part->color;

    if (background) {
      image_blitscale9(background, dx, dy, dw, dh, -1, -1);
    } else {
      draw_frame(dx, dy, dw, dh, 3, style->border.color, color);
    }
}




int zori_widget_visible(struct zori_widget * widget)  {
  return widget && ((widget->flags & ZORI_FLAG_HIDDEN) != ZORI_FLAG_HIDDEN);
}

int zori_widget_active(struct zori_widget * widget)  {
  return widget && ((widget->flags & ZORI_FLAG_DISABLED) != ZORI_FLAG_DISABLED);
}

int zori_widget_active_(struct zori_widget * widget, int set)  {
  if (set) {  
    widget->flags = widget->flags & (~ZORI_FLAG_DISABLED);
  } else {
    widget->flags = widget->flags | ZORI_FLAG_DISABLED;
  }
  return zori_widget_active(widget);
}

int zori_widget_visible_(struct zori_widget * widget, int set)  {
  if (set) {  
    widget->flags = widget->flags & (~ZORI_FLAG_HIDDEN);
  } else {
    widget->flags = widget->flags | ZORI_FLAG_HIDDEN;
  }
  return zori_widget_active(widget);
}


int zori_widget_hover(struct zori_widget * widget)  {
  return widget && ((widget->flags & ZORI_FLAG_HOVERED) == ZORI_FLAG_HOVERED);
}

int zori_widget_hover_(struct zori_widget * widget, int set)  {
  if (set) {  
    widget->flags = widget->flags | ZORI_FLAG_HOVERED;
  } else {
    widget->flags = widget->flags & (~ZORI_FLAG_HOVERED);
  }
  return zori_widget_hover(widget);
}

int zori_widget_marked(struct zori_widget * widget)  {
  return widget && ((widget->flags & ZORI_FLAG_MARKED) == ZORI_FLAG_MARKED);
}

int zori_widget_marked_(struct zori_widget * widget, int set)  {
  if (set) {  
    widget->flags = widget->flags | ZORI_FLAG_MARKED;
  } else {
    widget->flags = widget->flags & (~ZORI_FLAG_MARKED);
  }
  return zori_widget_hover(widget);
}


int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y) {
    return (rebox_coordinates_inside_p(&widget->box, x, y));
}


/* Registers an event handler for a widget. */
zori_id zori_register(zori_id id,  zori_event_type type, zori_handler_func handler, void * extra);


zori_font * zori_widget_font(struct zori_widget * widget) {
  return widget->style.text.font;
}

zori_color zori_widget_forecolor(struct zori_widget * widget) {
  return widget->style.text.color;
}

zori_color zori_widget_backcolor(struct zori_widget * widget) {
  return widget->style.back.color;
}


int zori_widget_h(struct zori_widget * widget) {
  return widget->box.size.y;
}

int zori_widget_w(struct zori_widget * widget) {
  return widget->box.size.x;
}

int zori_widget_x(struct zori_widget * widget) {
  return widget->box.at.x;
}

int zori_widget_y(struct zori_widget * widget) {
  return widget->box.at.y;
}



