#include <stdio.h>
#include <stdlib.h>
#include "title.h"

void initialize_title(Game *g) {
  g->title = malloc(sizeof(Title));
  Image rules_qr_img = LoadImage("img/rules_qr.png");
  g->title->rules_qr = LoadTextureFromImage(rules_qr_img);
  UnloadImage(rules_qr_img);
}

void title_handle_click_start(Game *g) {
  for (int i = 0; i < 48; i++) {
    g->cards[i].move.end_time = 0.;
  }
  g->state = GAME_STATE_INITIALIZING;
}

void title_handle_click_options(Game *g) {
  g->state = GAME_STATE_OPTIONS;
}

void title_handle_click_quit(Game *g) {
  g->should_close = true;
}

Vector2 tmp; // stands for "title mouse position"
void title_handle_input(Game *g) {
  tmp = GetMousePosition();
  Title *t = g->title;
  t->hover_start = false;
  t->hover_options = false;
  t->hover_quit = false;
  t->hover_credits = false;
  t->hover_rules = false;

  int half_start_width = MeasureText("Start", 60) / 2;
  if (tmp.x > 700-half_start_width && tmp.x < 700+half_start_width && tmp.y > 350 && tmp.y < 410) {
    t->hover_start = true;
    if (IsMouseButtonPressed(0)) title_handle_click_start(g);
  }
  int half_options_width = MeasureText("Options", 60) / 2;
  if (tmp.x > 700-half_options_width && tmp.x < 700+half_options_width && tmp.y > 500 && tmp.y < 560) {
    t->hover_options = true;
    if (IsMouseButtonPressed(0)) title_handle_click_options(g);
  }
  int half_quit_width = MeasureText("Quit", 60) / 2;
  if (tmp.x > 700-half_quit_width && tmp.x < 700+half_quit_width && tmp.y > 650 && tmp.y < 710) {
    t->hover_quit = true;
    if (IsMouseButtonPressed(0)) title_handle_click_quit(g);
  }
  int half_credits_width = MeasureText("Credits", 40) / 2;
  if (tmp.x > 1100-half_credits_width && tmp.x < 1100+half_credits_width && tmp.y > 600 && tmp.y < 640) t->hover_credits = true;
  int half_rules_width = MeasureText("Rules", 40) / 2;
  if (tmp.x > 300-half_rules_width && tmp.x < 300+half_rules_width && tmp.y > 600 && tmp.y < 640) t->hover_rules = true;
}

void title_fly_card(Game *g, Card *card) {
  int x, y;
  switch ((int) rand() % 4) {
  case 0:
    x = (rand() % 1600) - 100;
    y = -100;
    break;
  case 1:
    x = (rand() % 1600) - 100;
    y = 1000;
    break;
  case 2:
    x = -100;
    y = (rand() % 1100) - 100;;
    break;
  case 3:
    x = 1500;
    y = (rand() % 1100) - 100;;
    break;
  }

  card->position.x = x;
  card->position.y = y;
  Move *move = &card->move;
  move->position->x = x;
  move->position->y = y;
  move->origin.x = x;
  move->origin.y = y;


  switch ((int) rand() % 4) {
  case 0:
    x = (rand() % 1600) - 100;
    y = -100;
    break;
  case 1:
    x = (rand() % 1600) - 100;
    y = 1000;
    break;
  case 2:
    x = -100;
    y = (rand() % 1100) - 100;;
    break;
  case 3:
    x = 1500;
    y = (rand() % 1100) - 100;;
    break;
  }

  move->destination.x = x;
  move->destination.y = y;

  move->curve = CURVE_LINEAR;

  move->current_time = 0.;
  move->end_time = (rand() % 5) + 1;
}

void run_frame_title(Game *g) {
  if (rand() % 20 > 3) return;

  Card *card = NULL;
  for (int i = 0; i < 48; i++) {
    int index = rand() % 48;
    if (g->cards[index].move.current_time > g->cards[index].move.end_time) {
      card = &g->cards[index];
      break;
    }
  }

  if (!card) return;

  title_fly_card(g, card);
}

Texture *cards_texture_fun2(Game *g) {
  return g->black_card_backs ? &g->cards_texture_black : &g->cards_texture_red;
}

void title_draw(Game *g) {
  for (int i = 0; i < 48; i++) {
    g->cards[i].visible = true;
    draw_card(&g->cards[i], cards_texture_fun2(g), g->black_card_backs);
  }

  Title *t = g->title;
  DrawTextCentered("Hanafuda Hachi-Hachi", 700, 100, 90, BLACK);
  DrawTextCentered("Start", 700, 350, 60, t->hover_start ? RED : BLACK);
  DrawTextCentered("Options", 700, 500, 60, t->hover_options ? RED : BLACK);
  DrawTextCentered("Quit", 700, 650, 60, t->hover_quit ? RED : BLACK);
  DrawTextCentered("Credits", 1100, 600, 40, BLACK);
  DrawTextCentered("Rules", 300, 600, 40, BLACK);

  if (t->hover_rules) DrawTextureEx(t->rules_qr, (Vector2) { 135, 250 }, 0., 3., WHITE);

  if (t->hover_credits) {
    DrawRectangle(870, 200, 460, 380, BLACK);
    DrawRectangle(873, 203, 454, 374, WHITE);
    DrawTextCentered("Programmed by bassguitarbill", 1100, 210, 25, BLACK);
    DrawTextCentered("https://bassguitarbill.rocks", 1100, 240, 25, BLACK);
    DrawTextCentered("Running on raylib", 1100, 310, 25, BLACK);
    DrawTextCentered("https://www.raylib.com", 1100, 340, 25, BLACK);
    DrawTextCentered("Drawn with Aseprite", 1100, 410, 25, BLACK);
    DrawTextCentered("https://www.aseprite.org", 1100, 440, 25, BLACK);
    DrawTextCentered("Art adapted from Louiemantia", 1100, 510, 25, BLACK);
    DrawTextCentered("https://commons.wikimedia.org/wiki/User:Louiemantia", 1100, 540, 18, BLACK);
  }
}
