#include <daguerre.hpp>

void alpha_overlay(
  daguerre::hilbert_color &dest, const daguerre::rgb24 &src) {
  if (src.r != 0xff || src.g != 0x00 || src.b != 0xff)
    daguerre::default_overlay(dest, src);
}

void invert(daguerre::rgb24 &dest, const daguerre::rgb24 &src) {
  dest.r = 255 - src.r;
  dest.g = 255 - src.g;
  dest.b = 255 - src.b;
}

int main(int, char **) {

  auto framebuffer = daguerre::get_hilbert_framebuffer();

  daguerre::image<daguerre::rgb24> burden;
  daguerre::try_load_ppm("/assets/burden.ppm", burden);

  daguerre::image<daguerre::rgb24> pointer;
  daguerre::try_load_ppm("/assets/pointer.ppm", pointer);

  daguerre::fixed_bitmap_font<bool> terminus;
  daguerre::try_load_psf("/assets/terminus-bold-18x10.psf", terminus);

  terminus.overlay_text<>(burden, 0, 0, "this is a test");

  int32_t width = burden.width < framebuffer.width
                ? burden.width : framebuffer.width;
  int32_t height = burden.height < framebuffer.height
                 ? burden.height : framebuffer.height;
  unsigned x = (framebuffer.width - width) / 2;
  unsigned y = (framebuffer.height - height) / 2;

  int32_t new_mouse_x = width / 2;
  int32_t new_mouse_y = height / 2;
  int32_t old_mouse_x = new_mouse_x;
  int32_t old_mouse_y = new_mouse_y;
  bool was_left_mouse_down = false;

  daguerre::overlay_region<>(framebuffer, x, y, burden, 0, 0, width, height);

  while (1) {

    __euler_mouse_buttons mouse_buttons;
    int16_t mouse_change_x, mouse_change_y;
    uint32_t key_packet;
    __euler_input_packet_type packet_type = __euler_get_input_packet(
      mouse_buttons, mouse_change_x, mouse_change_y, key_packet);

    bool anything_changed = false;
    bool should_invert = false;

    if (packet_type == __EULER_IPT_MOUSE) {

      if (mouse_change_x != 0 || mouse_change_y != 0) {

        old_mouse_x = new_mouse_x;
        old_mouse_y = new_mouse_y;
        new_mouse_x += mouse_change_x;
        new_mouse_y += mouse_change_y;

        if (new_mouse_x < 0)
          new_mouse_x = 0;
        else if ((unsigned)new_mouse_x > width - pointer.width)
          new_mouse_x = width - pointer.width;

        if (new_mouse_y < 0)
          new_mouse_y = 0;
        else if ((unsigned)new_mouse_y > height - pointer.height)
          new_mouse_y = height - pointer.height;

        anything_changed = true;

      }

      if (!was_left_mouse_down && (mouse_buttons & __EULER_MB_LEFT))
        should_invert = true;

      was_left_mouse_down = mouse_buttons & __EULER_MB_LEFT;

    }

    else if (packet_type == __EULER_IPT_KEYBOARD)
      if ((key_packet & 0x0400ff) == 0x00005a)
        should_invert = true;

    if (should_invert) {

      //this works with the current implementation of overlay_region, but
      //maybe it would be better to have a dedicated function for when the
      //two regions are exactly the same, given the comment on overlay_region.
      daguerre::overlay_region<daguerre::rgb24, daguerre::rgb24, invert>(
        burden, 0, 0, burden, 0, 0, width, height);

      daguerre::overlay_region<>(
        framebuffer, x, y, burden, 0, 0, width, height);

      anything_changed = true;

    }

    if (anything_changed) {
      daguerre::overlay_region<>(
        framebuffer, old_mouse_x + x, old_mouse_y + y, burden,
        old_mouse_x, old_mouse_y, pointer.width, pointer.height);
      daguerre::overlay_region<
        daguerre::hilbert_color, daguerre::rgb24, alpha_overlay>(
        framebuffer, new_mouse_x + x, new_mouse_y + y,
        pointer, 0, 0, pointer.width, pointer.height);
    }

  }

}
