#include <stdlib.h>

#include "../lib/raylib.h"
#include "../include/menu.h"
#include "../include/util.h"
#include "../include/const.h"
#include "../include/game.h"
#include "../include/level.h"

menu_t *menu_create(void) {
  menu_t *menu = malloc(sizeof(menu_t));
  menu->idx = 0;
  return menu;
}

void menu_update(menu_t *menu, game_t *game) {
  (void) menu;
  if (IsKeyPressed(KEY_ESCAPE)) {
    game->state = STATE_GAME;
  }
  if (IsKeyPressed(KEY_Q)) {
    game->quit = true;
  }
  // if (IsKeyPressed(KEY_W)) {
  //   if (menu->idx > 0) {
  //     menu->idx--;
  //   }
  // }
  // if (IsKeyPressed(KEY_S)) {
  //   if (menu->idx < 2) {
  //     menu->idx++;
  //   }
  // }
  // if (IsKeyPressed(KEY_ENTER)) {
  //   switch (menu->idx) {
  //     case 0:
  //       game->state = STATE_GAME;
  //       break;
  //     case 1: {
  //       if (game->level != LEVEL_NULL) {
  //         level_e level = game->level->type;
  //         int max_health = game->player->max_health;
  //         level_unload(game);
  //         level_load(game, level);
  //         game->player->max_health = max_health;
  //         game->player->health = max_health;
  //         game->victory = false;
  //         game->defeat = false;
  //       }
  //       game->state = STATE_GAME;
  //       break;
  //     }
  //     case 2:
  //       game->quit = true;
  //       break;
  //     default:
  //       break;
  //   }
  // }
}

void menu_draw(menu_t *menu, game_t *game) {
  (void) menu;
  (void) game;
  DrawRectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, (Color) { 0, 0, 0, 125 });
  // int shift = 20;
  // DrawRectangle(WINDOW_WIDTH / 2.0 - 100, WINDOW_HEIGHT / 2.0 - 75 + shift - 10, 200, 150, (Color) { 50, 50, 50, 255 });
  // DrawTextureRec(
  //   game->assets.images,
  //   (rect_t) { 0, 0, 56 * SCALE, 28 * SCALE },
  //   pos_snap((pos_t) { WINDOW_WIDTH / 2.0 - 3.5 * TILE_WIDTH, WINDOW_HEIGHT / 2.0 - 115 }),
  //   WHITE
  // );
  // text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + shift }, "RESUME", TEXT_ALIGNMENT_CENTER, game);
  // text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + shift }, "RESTART", TEXT_ALIGNMENT_CENTER, game);
  // text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + 30 + shift }, "QUIT", TEXT_ALIGNMENT_CENTER, game);
  // pos_t cursor_pos = (pos_t) {
  //   .x = WINDOW_WIDTH / 2.0 - 70,
  //   .y = WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + menu->idx * 30 + shift,
  // };
  // DrawTextureRec(
  //   game->assets.font,
  //   texture_rect(0, 4, FONT_WIDTH, FONT_HEIGHT),
  //   pos_snap(cursor_pos),
  //   WHITE
  // );
}

void menu_free(menu_t *menu) {
  free(menu);
}
