#include <stdio.h>
#include "options.h"

int kan_value_from_index(int index) { return index == 0 ? 10 : 12; }
int index_from_kan_value(int kan_value) { return kan_value == 10 ? 0 : 1; }
int number_of_rounds_from_index(int index) {
  int r[] = { 1, 3, 6, 12 };
  return r[index];
}
int index_from_number_of_rounds(int number_of_rounds) {
  int r[] = { 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 3 };
  return r[number_of_rounds];
}
float deal_speed_from_index(int index) { return (index + 1) / 10. ; }
float index_from_deal_speed(float deal_speed) { return (int) (deal_speed * 10) - 1 ; }

void load_options_from_game(Game *g) {
  g->options->kan_value = index_from_kan_value(g->kan_value);
  g->options->number_of_rounds = index_from_number_of_rounds(g->number_of_rounds);
  g->options->card_backs = g->black_card_backs;
  g->options->deal_speed = index_from_deal_speed(g->deal_speed);
}

void save_options_to_game(Game *g) {
  g->kan_value = kan_value_from_index(g->options->kan_value);
  g->number_of_rounds = number_of_rounds_from_index(g->options->number_of_rounds);
  g->black_card_backs = g->options->card_backs;
  g->deal_speed = deal_speed_from_index(g->options->deal_speed);
}

void handle_options_save(Game *g) {
  save_options_to_game(g);
  g->state = GAME_STATE_TITLE_SCREEN;
}

void handle_options_cancel(Game *g) {
  load_options_from_game(g);
  g->state = GAME_STATE_TITLE_SCREEN;
}

void handle_select_kan(Game *g, int index) {
  g->options->kan_value = index;
}

void handle_select_number_of_rounds(Game *g, int index) {
  g->options->number_of_rounds = index;
}

void handle_select_card_backs(Game *g, int index) {
  g->options->card_backs = index;
}

void handle_select_deal_speed(Game *g, int index) {
  g->options->deal_speed = index;
}

OptionsChoices kan_choices = {              { "Ten", "Twelve" },                                    2, 250, &handle_select_kan };
OptionsChoices number_of_rounds_choices = { { "One", "Three", "Six", "Twelve" },                    4, 400, &handle_select_number_of_rounds };
OptionsChoices card_backs_choices = {       { "Red", "Black" },                                     2, 550, &handle_select_card_backs };
OptionsChoices deal_speed_choices = {       { "Very Fast", "Fast", "Medium", "Slow", "Very Slow" }, 5, 700, &handle_select_deal_speed };
OptionsChoices *oc[4] = { &kan_choices, &number_of_rounds_choices, &card_backs_choices, &deal_speed_choices };

void options_handle_input(Game *g) {
  if (!IsMouseButtonPressed(0)) return;

  int left = 250;
  int width = 900;

  Vector2 mouse_pos = GetMousePosition();
  int x = mouse_pos.x, y = mouse_pos.y;
  for (int i = 0; i < 4; i++) {
    OptionsChoices *choices = oc[i];
    if (y < choices->y || y > choices->y + 30) continue;

    for (int j = 0; j < choices->count; j++) {
      char *choice = choices->choice[j];
      int w = MeasureText(choice, 30);
      int center = left + (width / (choices->count + 1)) * (j + 1);
      int min = center - (w/2);
      int max = center + (w/2);
      if (x > min && x < max) {
	choices->handle(g, j);
	return;
      }
    }
  }

  if (x > 400 && x < 400 + MeasureText("Save", 30) + 12 && y > 800 && y < 836) {
    handle_options_save(g);
    return;
  }
  if (x > 900 && x < 900 + MeasureText("Cancel", 30) + 12 && y > 800 && y < 836) {
    handle_options_cancel(g);
    return;
  }
}

void DrawTextCentered(char *text, int center_x, int y, int point, Color color) {
  int width = MeasureText(text, point);
  DrawText(text, center_x - (width / 2), y, point, color);
}

void draw_option_choices(OptionsChoices *choices, int selected_index) {
  int left = 250;
  int width = 900;
  for (int i = 0; i < choices->count; i++) {
    Color color = i == selected_index ? RED : BLACK;
    DrawTextCentered(choices->choice[i], left + (width / (choices->count + 1)) * (i + 1), choices->y, 30, color);
  }
}

void options_draw(Game *g) {
  DrawTextCentered("Options", 700, 50, 60, BLACK);
  DrawTextCentered("Kan Value", 700, 175, 40, BLACK);
  draw_option_choices(&kan_choices, g->options->kan_value);
  DrawTextCentered("Number of Rounds", 700, 325, 40, BLACK);
  draw_option_choices(&number_of_rounds_choices, g->options->number_of_rounds);
  DrawTextCentered("Card Backs", 700, 475, 40, BLACK);
  draw_option_choices(&card_backs_choices, g->options->card_backs);
  DrawTextCentered("Deal Speed", 700, 625, 40, BLACK);
  draw_option_choices(&deal_speed_choices, g->options->deal_speed);

  DrawRectangle(400, 800, MeasureText("Save", 30) + 12, 36, BLACK);
  DrawRectangle(403, 803, MeasureText("Save", 30) + 6, 30, GREEN);
  DrawText("Save", 406, 806, 30, BLACK);

  DrawRectangle(900, 800, MeasureText("Cancel", 30) + 12, 36, BLACK);
  DrawRectangle(903, 803, MeasureText("Cancel", 30) + 6, 30, RED);
  DrawText("Cancel", 906, 806, 30, BLACK);
}
