#include "SDL_rect.h"
#include "SDL_surface.h"
#include "SDL_timer.h"
#include "aliases/aliases.h"
#include "fps.h"
#include "img_seq.h"
#include "ui_list.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_video.h>
#include <linux/limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720

INTERNAL colour_t bg_colour = {.colour = 0xff4c3715};
INTERNAL colour_t hover_colour = {.colour = 0xff5e4a18};
INTERNAL colour_t normal_text = {.colour = 0xff000000};
INTERNAL colour_t selected_text = {.colour = 0xffd8e6e8};

u64 max(u64 a, u64 b) { return a > b ? a : b; }
u64 digit_count(f64 num) { return (u64)log10(num) + 1; }

int main(void) {
  SDL_Init(SDL_INIT_EVERYTHING);

  IMG_Init(IMG_INIT_TIF);

  TTF_Init();

  SDL_Window *window =
      SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                       WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);

  SDL_Renderer *renderer = SDL_CreateRenderer(
      window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

  TTF_Font *font = TTF_OpenFont("./bahnschrift.ttf", 24);

  bool running = true;

  SDL_Event event = {0};

  f32 fps[COUNT_FPS] = {0};
  for (u64 i = 0; i < COUNT_FPS; ++i) {
    fps[i] = atof(fps_names[i]);
  }

  ui_list_t fps_ui = {
      .item_w = 0,
      .item_h = 0,
      .selected = FPS_12,
      .length = COUNT_FPS,
      .textures = fps_textures,
      .selected_textures = fps_textures_selected,
  };

  for (u64 i = 0; i < COUNT_FPS; ++i) {
    SDL_Surface *surface =
        TTF_RenderText_Solid(font, fps_names[i], normal_text.rgba);

    fps_textures[i] = SDL_CreateTextureFromSurface(renderer, surface);

    fps_ui.item_w = max(surface->w, fps_ui.item_w);
    fps_ui.item_h = max(surface->h, fps_ui.item_h);

    SDL_FreeSurface(surface);

    surface = TTF_RenderText_Solid(font, fps_names[i], selected_text.rgba);

    fps_textures_selected[i] = SDL_CreateTextureFromSurface(renderer, surface);

    SDL_FreeSurface(surface);
  }

  f64 wait = 1000.0f / fps[fps_ui.selected];
  u64 frame_count = 138;

  img_seq_t *seq = create_sequence(frame_count);

  seq->pos = 0;
  seq->length = frame_count;

  for (u64 i = 0; i < frame_count; ++i) {
    char frame[10];
    char path[PATH_MAX];
    u64 length = i == 0 ? 1 : digit_count(i);

    sprintf(path, "./frames/render_");

    for (u64 j = 0; j < 4 - length; ++j) {
      strcat(path, "0");
    }

    sprintf(frame, "%ld.tif", i);
    strcat(path, frame);

    seq->frames[i] = IMG_LoadTexture(renderer, path);
  }

  i32 y_pos = 50;
  u64 padding = 5;

  i64 mouse_x;
  i64 mouse_y;
  i64 clicked_x;
  i64 clicked_y;

  SDL_Rect dst = {.x = 150, .y = y_pos, .w = 960, .h = 540};

  while (running) {
    while (SDL_PollEvent(&event)) {
      switch (event.type) {
      case SDL_QUIT:
        running = false;
        break;
      case SDL_MOUSEMOTION:
        mouse_x = event.motion.x;
        mouse_y = event.motion.y;

        break;
      case SDL_MOUSEBUTTONDOWN:
        clicked_x = event.button.x;
        clicked_y = event.button.y;

        break;
      }
    }

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    SDL_RenderClear(renderer);

    SDL_RenderCopy(renderer, current_frame(seq), NULL, &dst);

    fps_ui.selected =
        ui_list(&fps_ui, 40, y_pos + padding, padding, mouse_x, mouse_y,
                clicked_x, clicked_y, renderer, bg_colour, hover_colour);

    clicked_x = clicked_y = -1;

    wait = 1000.0 / fps[fps_ui.selected];

    next_frame(seq);

    SDL_RenderPresent(renderer);

    SDL_Delay(wait);
  }

  SDL_DestroyRenderer(renderer);

  SDL_DestroyWindow(window);

  TTF_Quit();

  IMG_Quit();

  SDL_Quit();

  return 0;
}
