
#include "zori.h"
#include "zori_console.h"
#include "zori_screen.h"
#include "miao.h"
#include <allegro5/allegro_color.h>
#include "str.h"
#include "draw.h"
#include "monolog.h"

/*
* Pardon the pun name, but Zori is the submodule that handles the user
* interface and the menus. 
*/

/* registry functionality */

/** Compare registry entries. */
int zori_registry_entry_compare(const void * v1, const void * v2) {
  const struct zori_registry_entry * entry1 = v1;
  const struct zori_registry_entry * entry2 = v2;
  return (entry2->id - entry1->id);
}

/** Initialize the registry. */
zori_id zori_registry_init(struct zori_registry * registry) {
  miao_init(registry);
  return ZORI_ID_OK;
}


/** Add an entry to the registry. */
zori_id 
zori_registry_add(struct zori_registry * registry, zori_id id, 
struct zori_widget * widget) {
  struct zori_registry_entry entry = { id, widget };
  if (miao_push(registry, entry)) { 
    miao_qsort(registry, zori_registry_entry_compare);
    return ZORI_ID_OK;
  }
  return ZORI_ID_ENOMEM;
}

/** Look up an entry in the registry. */
struct zori_registry_entry *  
zori_registry_lookup_entry(struct zori_registry * registry, zori_id id) {
  struct zori_registry_entry key = { id, NULL };
  return miao_bsearch(registry, zori_registry_entry_compare, &key);
}


/** Look up a widget in the registry. */
struct zori_widget *  
zori_registry_lookup(struct zori_registry * registry, zori_id id) {
  struct zori_widget * result = NULL;
  struct zori_registry_entry * entry = NULL;
  
  entry = zori_registry_lookup_entry(registry, id);
  if (entry) { 
    result = entry->widget;
  }
  return result;
}

/** Remove an entry from the registry. */
zori_id 
zori_registry_remove(struct zori_registry * registry, zori_id id) {
  struct zori_registry_entry * entry = NULL;
  entry = zori_registry_lookup_entry(registry, id);
  if (entry) {
    miao_delete_entry(registry, entry);
  }
  return ZORI_ID_OK;
}



/** Handler functionality */

int zori_handler_compare(const void * v1, const void * v2) {
  const struct zori_handler * h1 = v1;
  const struct zori_handler * h2 = v2;
  
  if (h1->type < h2->type) return -1;
  if (h1->type > h2->type) return 1;
  return 0;  
}

struct zori_handler * zori_handlers_add(struct zori_handlers  * me, zori_event_type type, zori_handler_func * handler, void * data) {
  struct zori_handler * result = NULL;
  
  result = miao_push_ptr(me);
  if (result) {
    result->type    = type;
    result->handler = handler;
    result->data    = data;
  }
  miao_qsort(me, zori_handler_compare);
  return result;    
}


void zori_handlers_done(struct zori_handlers * me) {
  miao_done(me);
}

void zori_handlers_init(struct zori_handlers * me) {  
  miao_init(me);
}


struct zori_handler * zori_handlers_search(struct zori_handlers * me,  zori_event_type type) {    
  struct zori_handler * result;
  struct zori_handler key;
  key.type    = type;
  key.handler = NULL;
  key.data    = NULL;
  if (!me) return NULL;
  result = miao_bsearch(me, zori_handler_compare, &key);
  return result; 
}


int zori_handlers_handle(struct zori_handlers * me, union zori_event * event) {    
  struct zori_handler * handler = zori_handlers_search(me, event->type);
  if (!handler) return ZORI_HANDLE_IGNORE;
  event->any.data = handler->data;
  return handler->handler(event);
}


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




static struct zori_root * the_zori_root = NULL;

static struct zori_style * the_default_style = NULL;

static struct zori_registry * the_zori_registry = NULL;

struct zori_root * zori_get_root(void) {
  return the_zori_root;
}


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_widget * zori_get_widget(zori_id id) {
  if (!the_zori_registry) return NULL;
  return zori_registry_lookup(the_zori_registry, id);
}

zori_id zori_get_unused_id(void) {
  zori_id id = -1;
  struct zori_widget * found;
  do {
    id++;
    if (id == INT_MAX) { return ZORI_ID_ERROR; }
    found = zori_get_widget(id);
  } while(found);
  return 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;
}



zori_id zori_start(struct zori_style * default_style) {
  if (the_zori_root) return ZORI_ID_OK;
  the_zori_root = calloc(1, sizeof(*the_zori_root));
  if (!the_zori_root) return ZORI_ID_ENOMEM;
  the_default_style = calloc(1, sizeof(*the_default_style));
  if (!the_default_style) return ZORI_ID_ENOMEM;
  the_zori_registry = calloc(1, sizeof(*the_zori_registry));
  if (!the_zori_registry) return ZORI_ID_ENOMEM;
  
 
  the_default_style->text.font      = al_create_builtin_font();  
  the_default_style->text.color     = al_color_name("white");
  the_default_style->text.font_flags= ALLEGRO_ALIGN_LEFT;
  the_default_style->border.color   = al_color_name("white");  
  the_default_style->back.color     = al_color_name("green");
  the_default_style->fore.color     = al_color_name("white");  
  the_default_style->mark.color     = al_color_name("lightgreen");
  the_default_style->hover.color    = al_color_name("yellowgreen");  

  
  if (default_style) {
    the_zori_root->widget.style = *default_style;
  } else {
    the_zori_root->widget.style = *the_default_style;
  }
  
  the_zori_root->widget.id = 0;
  zori_registry_add(the_zori_registry, the_zori_root->widget.id, &the_zori_root->widget);
  return the_zori_root->widget.id;  
}

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);
  }
    
  miao_done(&widget->handlers);
  miao_done(&widget->children);
  
  return widget;
}

void zori_widget_free(struct zori_widget * widget) {
  if (widget) {
    zori_registry_remove(the_zori_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);
}


zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom) {
  struct zori_widget * widget = zori_get_widget(id);
  return zori_widget_margins_(widget, left, top, right, bottom);
}


zori_id zori_set_margin(zori_id id, int size) {
  struct zori_widget * widget = zori_get_widget(id);
  return zori_widget_margin_(widget, size);
}


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_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom) {
  struct zori_widget * widget = zori_get_widget(id);
  return zori_widget_paddings_(widget, left, top, right, bottom);
}


zori_id zori_set_padding(zori_id id, int size) {
  struct zori_widget * widget = zori_get_widget(id);
  return zori_widget_padding_(widget, size);
}


zori_font * zori_widget_text_font(struct zori_widget * widget) {  
  if ((!widget)) {
    return the_default_style->text.font;
  }
  return  widget->style.text.font;
}

zori_font * zori_text_font(zori_id id) {  
  struct zori_widget * widget = zori_get_widget(id);
  return zori_widget_text_font(widget);
}



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;  
    
    miao_init(&widget->children);
    miao_init(&widget->handlers);
    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 = the_default_style->text.font;
      }
    } else {
      widget->style = *the_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(the_zori_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;
}



/* Shut down Zori and destroys all widgets. Return 0 on succes or 
 * negative on error.
 */
zori_id zori_shutdown() {
  assert((void *)(&the_zori_root->widget) == (void *)the_zori_root);
  zori_widget_free(&the_zori_root->widget);
  the_zori_root = NULL;
  free(the_default_style);
  the_default_style = NULL;
  /* clean up registry last so zori_widget fre can unregister it's widgets.*/
  miao_done(the_zori_registry);
  free(the_zori_registry);
  the_zori_registry = NULL;
  
  
  return ZORI_ID_OK;
}




/* Creates a new screen widget. Normally this should be the first widget 
 * you create after zori_start.  */
zori_id zori_new_screen(zori_id id, zori_display * display);

/* Creates a new page widget on the given screen. The page is not 
 * made the active page, unless if it is the first one created. */
zori_id zori_new_page(zori_id id, zori_id screen);

/* Activates the page on it's display. All other pages are dectivated and 
 * hidden. */
zori_id zori_activate_page(zori_id page);

/* Creates a new generic widget on the given screen with the given
 * dimensions. */
zori_id zori_new(zori_id screen, zori_rebox * box);

/* Sets the flags of a widget.  */
zori_id zori_set_flags(zori_id widget, enum zori_flag flags);


/* Sets the whole style of a widget. */
zori_id zori_set_style(zori_id id, struct zori_style * style);

/* Sets the background color of the widget. */
zori_id zori_set_background_color(zori_id id, zori_color color);

/* Sets the foreground color of the widget. */
zori_id zori_set_foreground_color(zori_id id, zori_color color);

/* Creates a new frame widget. */
zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);

/* Creates a new (vertical) menu widget. */
zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);

/* Creates a new button widget. */
zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);

/* Creates a new conversation widget. */
zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);


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

/* Draws the whole UI and all visible parts. */
void zori_draw_all(void) {
  zori_widget_raise_draw_event(&the_zori_root->widget);
  zori_widget_raise_overdraw_event(&the_zori_root->widget);
}

/* Dispatches system events throughout the GUI.  */
int zori_handle_system_event(zori_system_event * sysev) {
  /* All events are passed on to the console regardless if it is active. 
   * Otherwise, the events go into the system. */
  if (the_zori_root->console ) { 
    if (zori_console_active(the_zori_root->console)) {
      int res = zori_widget_raise_system_event(&the_zori_root->console->widget, sysev);
      if (!zori_propagate_event_p(res)) return res;
    }
  }
  return zori_widget_raise_system_event(&the_zori_root->widget, sysev);
}


/* Updates the state of the UI. Pass in the time passed since last update. */
void zori_update(double dt) 
{
  struct zori_root * root = zori_get_root();
  zori_widget_raise_update_event(&root->widget, dt);
}


int zori_result(zori_id id) {
  int result = 0;
  struct zori_widget * widget = zori_get_widget(id);
  if (widget) {
    result = widget->result;
    widget->result = 0; 
  }
  return result;
}

/* 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_predicate(struct zori_widget * widget, void * extra) {
  zori_widget_type * type_ptr = extra;
  if(!type_ptr) return false;
  return (*type_ptr) == widget->type; 
}

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


