#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <raylib.h>

#include "dialog.h"

Dialog dialogs[2];

void handle_click_cancel_yes(Game *g) {
  g->player.dekiyaku_action = DEKIYAKU_ACTION_CANCEL;
  g->state = GAME_STATE_CALCULATING_DEKIYAKU_SCORE;
}

void handle_click_cancel_no(Game *g) {
  g->state = GAME_STATE_CHOOSING_FROM_HAND;
}

void handle_click_shoubu(Game *g) {
  g->player.dekiyaku_action = DEKIYAKU_ACTION_SHOUBU;
  g->state = GAME_STATE_CALCULATING_DEKIYAKU_SCORE;
}

void handle_click_sage(Game *g) {
  g->player.dekiyaku_action = DEKIYAKU_ACTION_SAGE;
  g->state = GAME_STATE_CHOOSING_FROM_HAND;
}

void init_dialogs(Game *g) {
  Dialog *cancel_dialog = &dialogs[0];
  cancel_dialog->text = malloc(200);
  strcpy(cancel_dialog->text, "Would you like to cancel?");
  cancel_dialog->text_color = BLACK;
  cancel_dialog->options_count = 2;
  cancel_dialog->game = g;

  cancel_dialog->options[0].text = malloc(50);
  strcpy(cancel_dialog->options[0].text, "Yes");
  cancel_dialog->options[0].color = GREEN;
  cancel_dialog->options[0].handle = &handle_click_cancel_yes;

  cancel_dialog->options[1].text = malloc(50);
  strcpy(cancel_dialog->options[1].text, "No");
  cancel_dialog->options[1].color = RED;
  cancel_dialog->options[1].handle = &handle_click_cancel_no;


  Dialog *shoubu_dialog = &dialogs[1];
  shoubu_dialog->text = malloc(200);
  strcpy(shoubu_dialog->text, "You have dekiyaku! Sage or shoubu?");
  shoubu_dialog->text_color = BLACK;
  shoubu_dialog->options_count = 2;
  shoubu_dialog->game = g;

  shoubu_dialog->options[0].text = malloc(50);
  strcpy(shoubu_dialog->options[0].text, "Sage");
  shoubu_dialog->options[0].color = GREEN;
  shoubu_dialog->options[0].handle = NULL;

  shoubu_dialog->options[1].text = malloc(50);
  strcpy(shoubu_dialog->options[1].text, "Shoubu");
  shoubu_dialog->options[1].color = RED;
  shoubu_dialog->options[1].handle = NULL;
}

Dialog *cancel_dialog() { return &dialogs[0]; }
Dialog *shoubu_dialog() { return &dialogs[1]; }

Rectangle dialog_option_outer_rectangle(Dialog *d, int i) {
  return (Rectangle) {
    ((960 * (i + 1)) / (d->options_count + 1)) - 10 + 200,
    500,
    MeasureText(d->options[i].text, DIALOG_OPTION_FONT_SIZE) + 20,
    40,
  };
}

Rectangle dialog_option_inner_rectangle(Dialog *d, int i) {
  Rectangle outer = dialog_option_outer_rectangle(d, i);
  return (Rectangle) {
    outer.x + 3,
    outer.y + 3,
    outer.width - 6,
    outer.height - 6,
  };
}

void dialog_draw(Dialog *d) {
  int text_width;
  DrawRectangleRec(DIALOG_OUTER_RECTANGLE, BLACK);
  DrawRectangleRec(DIALOG_INNER_RECTANGLE, WHITE);
  text_width = MeasureText(d->text, DIALOG_TEXT_FONT_SIZE);
  DrawText(d->text, 700 - (text_width / 2), 400, DIALOG_TEXT_FONT_SIZE, d->text_color);

  for (int i = 0; i < d->options_count; i++) {
    DialogOption *o = &d->options[i];
    Rectangle outer = dialog_option_outer_rectangle(d, i);
    Rectangle inner = dialog_option_inner_rectangle(d, i);
    DrawRectangleRec(outer, BLACK);
    DrawRectangleRec(inner, o->color);
    DrawText(o->text, inner.x + 3, inner.y + 3, DIALOG_OPTION_FONT_SIZE, BLACK);
  }
}

void dialog_handle_input(Dialog *d) {
  if (IsMouseButtonPressed(0)) {
    Vector2 mouse_pos = GetMousePosition();
    for (int i = 0; i < d->options_count; i++) {
      DialogOption *o = &d->options[i];
      Rectangle outer = dialog_option_outer_rectangle(d, i);
      if (CheckCollisionPointRec(mouse_pos, outer)) {
	o->handle(d->game);
	d->game->dialog = NULL;
	break;
      }
    }
  }
}
