#include "engine/engine.h"

// main input
enum Binding {
  Binding_Back,
  Binding_Forward,
  Binding_Pause,
  Binding_LeftClick,
  Binding_RightClick,
  Binding_MouseX,
  Binding_MouseY,
  Binding_PlayerLeft,
  Binding_PlayerRight,
  Binding_PlayerUp,
  Binding_PlayerDown,
};

struct engine_input_BackendPair main_input_backend[] = {
  { engine_input_Device_keyboardEnter, Binding_Forward },
  { engine_input_Device_keyboardEscape, Binding_Back },
  { engine_input_Device_keyboardP, Binding_Pause },
  { engine_input_Device_mouseLeftButton, Binding_LeftClick },
  { engine_input_Device_mouseRightButton, Binding_RightClick },
  { engine_input_Device_mousePositionX, Binding_MouseX },
  { engine_input_Device_mousePositionY, Binding_MouseY },
  { engine_input_Device_keyboardLeft, Binding_PlayerLeft },
  { engine_input_Device_keyboardRight, Binding_PlayerRight },
  { engine_input_Device_keyboardUp, Binding_PlayerUp },
  { engine_input_Device_keyboardDown, Binding_PlayerDown },
  { engine_input_Device_keyboardA, Binding_PlayerLeft },
  { engine_input_Device_keyboardD, Binding_PlayerRight },
  { engine_input_Device_keyboardW, Binding_PlayerUp },
  { engine_input_Device_keyboardS, Binding_PlayerDown },
};

struct MainInputs {
  struct engine_input_Signal menu_up;
  struct engine_input_Signal menu_down;
  struct engine_input_Signal menu_left;
  struct engine_input_Signal menu_right;
  struct engine_input_Signal menu_back;
  struct engine_input_Signal menu_forward;
  struct engine_input_Signal mouse_position;
  struct engine_input_Signal mouse_left_click;
};

struct MainInputs main_inputs = {
  .menu_up = ENGINE_INPUT_SIGNAL_UP(Binding_PlayerUp),
  .menu_down = ENGINE_INPUT_SIGNAL_UP(Binding_PlayerDown),
  .menu_left = ENGINE_INPUT_SIGNAL_UP(Binding_PlayerLeft),
  .menu_right = ENGINE_INPUT_SIGNAL_UP(Binding_PlayerRight),
  .menu_back = ENGINE_INPUT_SIGNAL_UP(Binding_Back),
  .menu_forward = ENGINE_INPUT_SIGNAL_UP(Binding_Forward),
  .mouse_position = ENGINE_INPUT_SIGNAL_POINT(Binding_MouseX, Binding_MouseY),
  .mouse_left_click = ENGINE_INPUT_SIGNAL_DOWN(Binding_LeftClick)
};

// intro state
static struct engine_Image _intro_title_image = { .path = "images/intro_title.png" };

static int _intro_l = 0;
static char * _unicat_names[] = {
  "Sarah",
  "Charlie",
  "Frank",
  "Anna",
  "Willa",
  "Melonie"
};

static void _intro_ui(void * ud) {
  (void)ud;

  engine_ui_center();
    engine_ui_image(&_intro_title_image);

  engine_ui_center();
    engine_ui_fontColor(alias_Color_GRAY);

    engine_ui_vertical();
      engine_ui_fontSize(40);
      engine_ui_text("Alias Town");

      engine_ui_fontSize(20);
      engine_ui_text("A game by %s | Unicat", _unicat_names[(_intro_l++ >> 5) % (sizeof(_unicat_names)/sizeof(_unicat_names[0]))]);
    engine_ui_end();

  engine_ui_top();
  engine_ui_fontColor(alias_Color_BLACK);
  engine_ui_text("version 1");
}

static void _intro_frame(void * ud) {
  (void)ud;

  if(main_inputs.menu_back.boolean) {
    engine_state_pop();
    return;
  }

  if(main_inputs.menu_forward.boolean) {
    engine_state_pop();
    //Engine_push_state(&main_menu_state);
    return;
  }
}

static struct engine_State _intro_state = {
  .ui = _intro_ui,
  .frame = _intro_frame
};

// entry point
struct engine_State * engine_application_entryPoint(void) {
  engine_input_setPlayerBackend(0, main_input_backend, sizeof(main_input_backend) / sizeof(main_input_backend[0]));
  engine_input_addFrontend(0, &main_inputs.menu_up, 8);
  return &_intro_state;
}