#include "zori.h"
#include "zori_widget.h"
#include "zori_registry.h"
#include "zori_style.h"
#include "zori_screen.h"
#include "monolog.h"
#include "draw.h"


/** Returns whether or not a widget will even handle an event in the first place.
 * For example, a hidden widget won't draw, and a disabled widget won't 
 * accept any system events, and a NULL widget doesn't accept events at all.. */
int zori_widget_accepts_event(struct zori_widget * widget, union zori_event * event) {
  if (!widget) return 0;
  switch(event->any.type) {
    case ZORI_EVENT_DRAW:
      return zori_widget_visible(widget);
    default:
      return zori_widget_active(widget);
  }
}   


/** Raises an event on the widget itself only. */
int zori_widget_self_raise_event(struct zori_widget * widget, 
union zori_event * event) {
  enum zori_handle_result result;
  if (zori_widget_accepts_event(widget, event)) { 
    event->any.widget = widget;
    return zori_handlers_handle(&widget->handlers, event);
  }
  /* If the event is not accepted, it is passed on to the child widgets. */
  return ZORI_HANDLE_PASS;
}

/* Raises an event on the widget, and if necessary, propagates it on to it's 
 * children automatically and recursively. */
int 
zori_widget_raise_event(struct zori_widget * widget, union zori_event * event) {
  if (!widget) return ZORI_HANDLE_DONE;
  
  enum zori_handle_result result = 
  zori_widget_self_raise_event(widget, event);
  
  if (zori_propagate_event_p(result)) {
    enum zori_handle_result sub;
    size_t index;
    for (index = 0; index < miao_size(&widget->children); index++) {
      struct zori_widget * child = miao_unsafe_get(&widget->children, index);
      event->any.widget = child;
      sub = zori_widget_raise_event(child, event);
      if (sub == ZORI_HANDLE_DONE) {
        result = ZORI_HANDLE_DONE;
        break;
      }
    }
  }
  return result;
}

int zori_widget_raise_system_event
(struct zori_widget * widget, zori_system_event * sysev) {
  union zori_event event;
  event.sys.any.type   = sysev->type;
  event.sys.any.widget = widget;
  event.sys.ev         = sysev;
  return zori_widget_raise_event(widget, &event);
}

int zori_widget_raise_draw_event(struct zori_widget * widget) {
  union zori_event event;
  event.draw.any.type   = ZORI_EVENT_DRAW;
  event.draw.any.widget = widget; 
  return zori_widget_raise_event(widget, &event);
}

int zori_widget_raise_overdraw_event(struct zori_widget * widget) {
  union zori_event event;
  event.draw.any.type   = ZORI_EVENT_OVERDRAW;
  event.draw.any.widget = widget; 
  return zori_widget_raise_event(widget, &event);
}


int zori_widget_raise_done_event(struct zori_widget * widget) {
  union zori_event event;
  event.done.any.type   = ZORI_EVENT_DONE;
  event.done.any.widget = widget; 
  return zori_widget_raise_event(widget, &event);
}

int zori_widget_raise_free_event(struct zori_widget * widget) {
  union zori_event event;
  event.free.any.type   = ZORI_EVENT_FREE;
  event.free.any.widget = widget; 
  return zori_widget_raise_event(widget, &event);
}

int zori_widget_raise_update_event
(struct zori_widget * widget, double dt) {
  union zori_event event;
  event.update.any.type   = ZORI_EVENT_DONE;
  event.update.any.widget = widget;
  event.update.dt         = dt; 
  return zori_widget_raise_event(widget, &event);
}

int zori_widget_raise_action_event
(struct zori_widget * widget) {
  union zori_event event;
  event.action.any.type   = ZORI_EVENT_ACTION;
  event.action.any.widget = widget;
  return zori_widget_raise_event(widget, &event);
}


int zori_widget_raise_close_event
(struct zori_widget * widget, struct zori_widget * from) {
  union zori_event event;
  event.close.any.type   = ZORI_EVENT_CLOSE;
  event.close.any.widget = widget;
  event.close.from       = from;
  return zori_widget_raise_event(widget, &event);
}


int zori_widget_compare(const void * v1, const void * v2) {
  const struct zori_widget * w1 = v1, * w2 = v2;
  return (w2->id - w1->id);
}

struct zori_handler * zori_widget_add_handler
(struct zori_widget * widget, zori_event_type type, zori_handler_func * handler, void * data) {  
  if ((!widget) || (!handler)) return NULL;
  
  return zori_handlers_add(&widget->handlers, type, handler, data);
}

struct zori_handler * zori_widget_add_handlers
(struct zori_widget * widget, struct zori_handler * handlers, size_t amount) {
  size_t i;
  for (i = 0; i < amount; i++) { 
    struct zori_handler * handler = handlers + i;
    if (!zori_widget_add_handler(widget, handler->type, handler->handler, handler->data)) return NULL;
  }
  return handlers + amount - 1;
}



void zori_widget_free(struct zori_widget * widget);

struct zori_widget * zori_widget_done(struct zori_widget * widget) {
  size_t index;
  struct zori_widget * aid, * next;
  if (!widget) return widget;
  
  for (index = 0; index < miao_size(&widget->children); index ++) {
    struct zori_widget * child = miao_unsafe_get(&widget->children, index);
    zori_widget_free(child);
  }
  
  if (widget->destroy) {
      widget->destroy(widget);
  }
      
  miao_done(&widget->handlers);
  miao_done(&widget->children);
  
  return widget;
}

void zori_widget_free(struct zori_widget * widget) {
  if (widget) {
    zori_registry_remove(zori_get_registry(), widget->id);
  }
  zori_widget_done(widget);
  free(widget);
}

struct zori_widget *  
zori_widget_add_child(struct zori_widget * parent, struct zori_widget * child) {
  if (parent) { 
    miao_push((&parent->children), child);
    child->parent = parent;
  }
  return child;
} 

zori_id
zori_widget_margins_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  if (!widget) return ZORI_ID_EINVAL;
  widget->outer = widget->box;
  widget->outer.at.x -= left;
  widget->outer.at.y -= top;
  widget->outer.size.x += (left + right);
  widget->outer.size.y += (top + bottom);
  return widget->id;
}

zori_id 
zori_widget_margin_(struct zori_widget * widget, int size) {
  return zori_widget_margins_(widget, size, size, size, size);
}



/* 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;
}



/* Gets amount of child widgets of this widget, 0 if none, -1 on error.*/
int zori_widget_count_children(struct zori_widget * widget) { 
  if (!widget) return -1;
  return miao_size(&widget->children); 
}

/* Returns the index-th child of his widget, or NULL on error or if no such child exists. */
struct zori_widget * zori_widget_get_child(struct zori_widget * widget, int index) {
    int count = zori_widget_count_children(widget);
    if (count < 1) return NULL;
    if (index < 0) return NULL;
    if (index >= count) return NULL;
    return miao_unsafe_get(&widget->children, index);
}

zori_id
zori_widget_paddings_(struct zori_widget * widget, int left, int top, int right, int bottom) {
  if (!widget) return ZORI_ID_EINVAL;
  widget->inner = widget->box;
  widget->inner.at.x += left;
  widget->inner.at.y += top;
  widget->inner.size.x -= (left + right);
  widget->inner.size.y -= (top + bottom);
  return widget->id;
}

zori_id 
zori_widget_padding_(struct zori_widget * widget, int size) {
  return zori_widget_paddings_(widget, size, size, size, size);
}

zori_font * zori_widget_text_font(struct zori_widget * widget) {  
  struct zori_style * style;
  if (!widget) {
    style = zori_get_default_style();
  } else {
    style = & widget->style;
  }
  return style->text.font;
}

struct zori_widget * zori_widget_init
  ( struct zori_widget * widget, 
    zori_widget_type type,
    zori_id id,
    struct zori_widget * parent, 
    zori_rebox * box, struct zori_style * style) {
    if (!widget) return NULL;
    struct zori_style * default_style = zori_get_default_style(); 
    
    miao_init(&widget->children);
    miao_init(&widget->handlers);
    widget->destroy = NULL;
    widget->z     = 0;
    widget->flags = 0;
    widget->type  = type;
    widget->id    = ( (id < 0) ? zori_get_unused_id() : id);
    
    if (style) {
      widget->style = *style;
      if (!widget->style.text.font) {
        widget->style.text.font = default_style->text.font;
      }
    } else {
      widget->style = *default_style;
    }
    if (box) {
      widget->box = *box;
    } else if (parent) {
      widget->box = parent->box;
    } else {
      widget->box = rebox_make(0, 0, ZORI_WIDGET_DEFAULT_W, ZORI_WIDGET_DEFAULT_H);
    }
    
    zori_widget_margin_(widget, ZORI_MARGIN_DEFAULT);
    zori_widget_padding_(widget, ZORI_PADDING_DEFAULT);
    
    zori_widget_add_child(parent, widget);
    zori_registry_add(zori_get_registry(), widget->id, widget);
    return widget;
}

struct zori_widget * 
zori_widget_initall(struct zori_widget * widget, zori_widget_type type, int id,
struct zori_widget * parent, zori_rebox * box, struct zori_style * style, 
struct zori_handler * handlers, size_t amount) {
  if (zori_widget_init(widget, type, id, parent, box, style)) {
    zori_widget_add_handlers(widget, handlers, amount);
  }
  return widget;
}

/* Lets the widget handle an event and bubbles it on to it's children 
 * recursively depending on the need to do that. */
void zori_widget_handle_event(struct zori_widget * widget, union zori_event * event) {
  zori_widget_raise_event(widget, event);
}


/* Returns the first parent or super-parent of this widget for which 
 * predicate() returns true.
 */
struct zori_widget * 
zori_widget_find_parent(struct zori_widget * widget, 
  bool (*predicate)(struct zori_widget * parent, void * extra),
  void * extra 
  ) {
  struct zori_widget * parent;
  
  if (!widget) return NULL;
  parent = widget->parent;
  while (parent) {
    if (predicate(parent, extra)) return parent;
    if (parent == parent->parent) {
      LOG_ERROR("GUI setup not correct. Parent/child loop in widget %p.", widget);
      return NULL;
    }
    parent = parent->parent;
  }
  return NULL;
}

bool zori_widget_is_type(struct zori_widget * widget, zori_widget_type type) {
  if(!widget) return false;
  return (type) == widget->type; 
}

bool zori_widget_is_type_predicate(struct zori_widget * widget, void * extra) {
  zori_widget_type * type_ptr = extra;
  if(!type_ptr) return false;
  return zori_widget_is_type(widget, (*type_ptr)); 
}

struct zori_widget * 
  zori_widget_get_parent_of_type(struct zori_widget * widget,  
  zori_widget_type type) {
  return zori_widget_find_parent(widget, zori_widget_is_type_predicate, &type);
}


struct zori_screen * zori_widget_get_screen(struct zori_widget * widget) {  
  struct zori_widget * screen_widget = zori_widget_get_parent_of_type(widget, ZORI_WIDGET_TYPE_SCREEN);
  return zori_widget_to_screen(screen_widget);
}


int zori_mark_widget(struct zori_widget * widget) {
    struct zori_screen * screen = zori_widget_get_screen(widget);
    if (screen) {
      screen->cursors.keyjoy.p = widget->box.at;
    }
    return ZORI_HANDLE_DONE;
}



