/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
 * vi: set shiftwidth=2 tabstop=2 expandtab:
 * :indentSize=2:tabSize=2:noTabs=true:
 */

#include "GameButton.h"
#include "game_utils.h"

/**
 * This file, as the rest of the project is under MIT license.
 * see http://opensource.org/licenses/MIT
 *
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
 */

GameButton * game_button_new (int x, int y, int h, int w) {
  GameButton * self = malloc (sizeof(GameButton));
  fprintf (stdout, "Creating GameButton: address: %ld\n", (long) self);
  SDL_Rect * base = (SDL_Rect *) self;
  base->x = x;
  base->y = y;
  base->h = h;
  base->w = w;
  self->clicked = NULL;
  self->is_clicked = false;
  self->fill = false;
  self->hover = NULL;
  self->is_hover = false;
  self->is_stop_hover = false;
  self->colour = game_color_create_color (0,0,255,255);
  return self;
}


bool game_button_envelopes_SDL_Point (GameButton *self, SDL_Point * point) {
  if (SDL_EnclosePoints (point, 1, (SDL_Rect *)self, NULL)) {
    return true;
  } else {
    return false;
  }
}

bool game_button_check_clicked (GameButton * self, MousePointer * pointer) {
  SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
  if (contains) {
     if (pointer->pressed) {
       game_button_do_callback (self, "clicked");
       return true;
     }
     
  }
  self->is_clicked = false;
  return false;
}

bool game_button_check_hover (GameButton * self, MousePointer * pointer) {
  SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
  if (contains) {
    if (!self->is_clicked ) {
      game_button_do_callback (self, "hover");
    }
    self->is_hover = true;
    self->is_stop_hover = false;
    return true;
  } else if (self->is_hover) {
    game_button_do_callback (self, "stop_hover");
    self->is_stop_hover = true;
  }
  self->is_hover = false;
  return false;
}

void game_button_set_callback (GameButton * self,
                               const char * name,
                               BtnCallback callback,
                               void * data) {
  if (!strcmp(name, "clicked")) {
    self->clicked = callback;
    self->clicked_data = data;
  } else if (!strcmp(name, "hover")) {
    self->hover = callback;
    self->hover_data = data;
  } else if (!strcmp(name, "stop_hover") && self->hover) {
    self->stop_hover = callback;
    self->stop_hover_data = data;
  } else {
    fprintf (stderr,
             "GameButton: Can not set callback \"%s\", it does not exist!\n",
             name);
    return;
  }
  return;
}


void game_button_do_callback (GameButton * self,
                              const char * name) {
  BtnCallback callback;
  if (!strcmp(name, "clicked") && self->clicked) {
    if (self->is_clicked) {
     return;
    }
    callback = self->clicked;
    callback (self, self->clicked_data);
  } else if (!strcmp(name, "hover") && self->hover) {
    if (self->is_hover) {
      return;
    }
    callback = self->hover;
    callback (self, self->hover_data);
  } else if (!strcmp(name, "stop_hover") && self->stop_hover) {
    if (self->is_stop_hover) {
      return;
    }
    callback = self->stop_hover;
    callback (self, self->stop_hover_data);
  } else {
        fprintf (stderr,
             "GameButton: Can not do callback \"%s\", it does not exist, or is undefined!\n",
             name);
    return;
  }
  self->is_clicked = true;
  return;
}

void game_button_draw (GameButton * self, SDL_Renderer * renderer) {
  SDL_Color oc = game_color_set_draw_color (renderer, self->colour);
  if (self->fill) {
    SDL_RenderFillRect (renderer , (SDL_Rect *) self);
  } else {
    SDL_RenderDrawRect (renderer , (SDL_Rect *) self);
  }
  game_color_set_draw_color  (renderer, oc); /* reset the colour */
}

void game_button_set_fill (GameButton * self, bool fill) {
  self->fill = fill;
}

void game_button_free (GameButton * self) {
  fprintf (stdout, "Freeing GameButton: address: %ld\n", (long) self);
  free (self);
}
