#include "include/raylib.h"
#include "include/raymath.h"
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

#define TEXTURES_BUF_SIZE 16
#define TARGET_FPS 60
#define MAX_KNIGHTS 10000
#define SCREEN_WIDTH 1300
#define SCREEN_HEIGHT 1080

#define DEBUG_MODE_ENABLED

bool global_debug_mode = false;

#define ENNIX_LIB_IMPLEMENTATION
#include "lib.h"
#include "sprites.h"
#include "game_data.h"

typedef enum KnightState {
    KNIGHT_IDLE = 0,
    KNIGHT_RUNNING = 1,
    KNIGHT_ATTACKING = 2,
} KnightState;

typedef enum Direction {
    DIR_UP = 0,
    DIR_DOWN = 1,
    DIR_LEFT = 2,
    DIR_RIGHT = 3,
    // DIR_UP_LEFT = 4,
    // DIR_UP_RIGHT = 5,
    // DIR_DOWN_LEFT = 6,
    // DIR_DOWN_RIGHT = 7,
} Direction;

typedef struct Knight {
    Point position;
    Point move_target_point;
    u8 look_dir;
    u8 state;
    u8 selected;
    u8 ordered_to_move;
} Knight;

typedef struct GameState {
    int frame_count;
    Point camera_position;

    PointOption selected_point;
    Knight *knights;
    SpriteAnimationPlayback* anim_playbacks;
    Knight *selected_knights;

    PointOption selection_mouse_start_pos;

    int entity_count;
} GameState;

typedef struct Assets {
    Texture2D *textures;
} Assets;

Assets Init() {
    knight_anims[ANIM_KNIGHT_IDLE]         = knight_idle;
    knight_anims[ANIM_KNIGHT_RUN]          = knight_run;
    knight_anims[ANIM_KNIGHT_ATTACK_SIDE1] = knight_attack_side1;
    knight_anims[ANIM_KNIGHT_ATTACK_SIDE2] = knight_attack_side2;
    knight_anims[ANIM_KNIGHT_ATTACK_TOP1]  = knight_attack_front1;
    knight_anims[ANIM_KNIGHT_ATTACK_TOP2]  = knight_attack_front2;
    knight_anims[ANIM_KNIGHT_ATTACK_BACK1] = knight_attack_back1;
    knight_anims[ANIM_KNIGHT_ATTACK_BACK2] = knight_attack_back2;

    Assets assets = {0};
    assets.textures = malloc(sizeof(Texture2D) * TEXTURES_BUF_SIZE);
    assets.textures[TEX_GROUND] = LoadTexture("assets/Terrain/Ground/Tilemap_Flat.png");
    assets.textures[TEX_KNIGHT] =
        LoadTexture("assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png");
    assets.textures[TEX_MOUSE_CURSOR] = LoadTexture("assets/UI/Pointers/01.png");
    assets.textures[TEX_TARGET_RETICLE] = LoadTexture("assets/UI/Pointers/02.png");
    return assets;
}

void Update(GameState *game, Camera2D *cam, float dt) {
    if (IsMouseButtonPressed(0)) {
        Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
        game->selection_mouse_start_pos = (PointOption){ .tag = SOME, .some.point = mouse_pos };
    }

    if (game->selection_mouse_start_pos.tag == SOME) {
        Point start_pos = game->selection_mouse_start_pos.some.point;
        Point current_pos = GetScreenToWorld2D(GetMousePosition(), *cam);

        if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
            f32 width = current_pos.x - start_pos.x;
            f32 height = current_pos.y - start_pos.y;
            f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
            f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
            Rectangle rect = {x, y, fabs(width), fabs(height)};
            for (int i = 0; i < game->entity_count; i++) {
                Rectangle mouse_select_area = {
                    game->knights[i].position.x + knight_colrect_select.x,
                    game->knights[i].position.y + knight_colrect_select.y,
                    knight_colrect_select.width,
                    knight_colrect_select.height,
                };
                game->knights[i].selected =
                    CheckCollisionRecs(rect, mouse_select_area);
            }
        }
    }
    if (IsMouseButtonReleased(0)) {
        // TODO: This is what should happen when the distance is too small
        // Point start_pos = game->selection_mouse_start_pos.some.point;
        Point start_pos = game->selection_mouse_start_pos.some.point;
        Point current_pos = GetScreenToWorld2D(GetMousePosition(), *cam);

        if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
            f32 width = current_pos.x - start_pos.x;
            f32 height = current_pos.y - start_pos.y;
            f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
            f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
            Rectangle rect = {x, y, fabs(width), fabs(height)};
            for (int i = 0; i < game->entity_count; i++) {
                Rectangle mouse_select_area = {
                    game->knights[i].position.x + knight_colrect_select.x,
                    game->knights[i].position.y + knight_colrect_select.y,
                    knight_colrect_select.width,
                    knight_colrect_select.height,
                };
                game->knights[i].selected =
                    CheckCollisionRecs(rect, mouse_select_area);
            }
        } else {
            // TODO: This kind of sucks that we're doing all the math here to calculate
            // the position of the collider, we need a helper that calculates origin offset
            int k_idx = -1;
            for (int i = 0; i < game->entity_count; i++) {
                Rectangle mouse_select_area = {
                    game->knights[i].position.x + knight_colrect_select.x,
                    game->knights[i].position.y + knight_colrect_select.y,
                    knight_colrect_select.width,
                    knight_colrect_select.height,
                };
                if (CheckCollisionPointRec(current_pos, mouse_select_area)) {
                    k_idx = i;
                }
                game->knights[i].selected = false;
            }
            if (k_idx != -1) {
                game->knights[k_idx].selected = true;
            }
        }
        game->selection_mouse_start_pos = (PointOption){ .tag = NONE, };
    }

    if (IsMouseButtonPressed(1)) {
        Point target_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
        bool any = false;
        for (int i = 0; i < game->entity_count; i++) {
            if (game->knights[i].selected) {
                game->knights[i].move_target_point = target_pos;
                game->knights[i].ordered_to_move = true;
                any = true;
            }
        }
        if (any) {
            game->selected_point = (PointOption){ .tag = SOME, .some.point = target_pos };
        }
    }

    const float cam_move_speed = 1050.0f * dt;
    Vector2 cam_vel = {0};
    if (IsKeyDown(KEY_D)) {
        cam_vel.x = -1;
    }
    if (IsKeyDown(KEY_A)) {
        cam_vel.x = 1;
    }
    if (IsKeyDown(KEY_W)) {
        cam_vel.y = 1;
    }
    if (IsKeyDown(KEY_S)) {
        cam_vel.y = -1;
    }
    cam_vel = Vector2Normalize(cam_vel);
    game->camera_position = Vector2Scale(cam_vel, cam_move_speed);
    cam->offset = Vector2Add(cam->offset, game->camera_position);
    // if (IsKeyPressed(KEY_SPACE) && game->selected_knight != -1) {
    //     int k_idx = game->selected_knight;
    //     if (game->knights[k_idx].state != KNIGHT_ATTACKING) {
    //         game->knights[k_idx].state = KNIGHT_ATTACKING;
    //         PlayAnimation(ANIM_KNIGHT_ATTACK_SIDE1, knight_anims, &game->anim_playbacks[k_idx]);
    //     }
    // }
    if (IsKeyPressed(KEY_F1)) {
        global_debug_mode = !global_debug_mode;
    }

    // Process animation data
    TickSpriteAnimations(game->anim_playbacks, game->entity_count);

    // Handle knight movement if they are moving somewhere
    for (int i = 0; i < game->entity_count; i++) {
        Vector2 input_vel = {0};
        Knight *knight = &game->knights[i];
        if (knight->ordered_to_move) {
            Vector2 target = knight->move_target_point;
            if (Vector2DistanceSqr(target, knight->position) < 2.5f) {
                knight->ordered_to_move = false;
            } else {
                input_vel.x = knight->position.x - target.x < 0.0f ? 1 : -1;
                knight->position = Vector2MoveTowards(knight->position, target, 4.0f);
            }
        }

        // Handle the attacking state, if not handle state transitions
        if (knight->state == KNIGHT_ATTACKING) {
            if (IsAnimationFinished(game->anim_playbacks[i])) {
                knight->state = KNIGHT_IDLE;
                PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
            }
        } else {
            if (input_vel.x != 0) {
                knight->look_dir = input_vel.x == -1 ? DIR_LEFT : DIR_RIGHT;
            }
            if (input_vel.x != 0 || input_vel.y != 0) {
                if (knight->state == KNIGHT_IDLE) {
                    PlayAnimation(ANIM_KNIGHT_RUN, knight_anims, &game->anim_playbacks[i]);
                    knight->state = KNIGHT_RUNNING;
                }
            } else {
                if (knight->state == KNIGHT_RUNNING) {
                    PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
                    knight->state = KNIGHT_IDLE;
                }
            }
        }
        if (i == 0) continue;
        // Sort the entities by y position using Insertion Sort
        int j = i;
        while (j > 0 && game->knights[j].position.y < game->knights[j - 1].position.y) {
            // Swap position
            Knight temp_k = game->knights[j - 1];
            game->knights[j - 1] = game->knights[j];
            game->knights[j] = temp_k;
            // Swap animations
            SpriteAnimationPlayback temp_a = game->anim_playbacks[j - 1];
            game->anim_playbacks[j - 1] = game->anim_playbacks[j];
            game->anim_playbacks[j] = temp_a;
            j--;
        }
    }
}

void Draw(GameState *game, Assets assets, Camera2D cam, float dt) {
    (void)cam;
    (void)dt;
    ClearBackground((Color){100, 149, 237, 255});

    int size = 32;
    int topx = SCREEN_WIDTH / 2 - size * 32 / 2;
    int topy = SCREEN_HEIGHT / 2 - size * 32 / 2;
    for (int col = 0; col < size; col++) {
        for (int row = 0; row < size; row++) {
            int atlas_col = 0;
            int atlas_row = 0;
            if (col == size - 1) {
                atlas_col = 5;
            } else if (col > 0) {
                atlas_col = (col % 4) + 1;
            }
            if (row == size - 1) {
                atlas_row = 5;
            } else if (row > 0) {
                atlas_row = (row % 4) + 1;
            }
            Vector2 pos = {32 * col + topx, 32 * row + topy};
            Rectangle src_rect = {32 * atlas_col, 32 * atlas_row, 32, 32};
            D_DrawTextureRec(assets.textures[TEX_GROUND], src_rect, pos, WHITE);
        }
    }

    if (game->selected_point.tag == SOME) {
        Vector2 marker_pos = game->selected_point.some.point;
        marker_pos.x -= assets.textures[TEX_TARGET_RETICLE].width / 2;
        marker_pos.y -= assets.textures[TEX_TARGET_RETICLE].height / 2;
        D_DrawTextureV(assets.textures[TEX_TARGET_RETICLE], marker_pos, WHITE);
    }

    // for (int i = 0; i < game->anim_playbacks_count; i++) {
    for (int i = 0; i < game->entity_count; i++) {
        Knight *knight = &game->knights[i];
        SpriteAnimationPlayback *playback = &game->anim_playbacks[i];
        Rectangle src_rect = {
            playback->current_frame * knight_sprite_size,
            playback->row * knight_sprite_size,
            knight_sprite_size,
            knight_sprite_size,
        };
        // TODO: This might be a nice place to optimize
        if (knight->look_dir == DIR_LEFT) {
            src_rect.width = -abs((int)src_rect.width);
        }
        Rectangle dest_rect = {knight->position.x, knight->position.y, 192, 192};
        Texture2D tex = assets.textures[TEX_KNIGHT];
        D_DrawTexturePro(tex, src_rect, dest_rect, knight_origin, 0.0f, WHITE);
        if (knight->selected == true) {
            Rectangle knight_col_area = {
                knight->position.x + knight_colrect_select.x,
                knight->position.y + knight_colrect_select.y,
                knight_colrect_select.width,
                knight_colrect_select.height,
            };
            // Color color = game->selected_knight == NULL ? RED : GREEN;
            DrawRectangleLinesEx(knight_col_area, 2.0f, GREEN);
        }
    }

    if (game->selection_mouse_start_pos.tag == SOME) {
        Point start_pos = game->selection_mouse_start_pos.some.point;
        Point current_pos = GetScreenToWorld2D(GetMousePosition(), cam);

        if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
            f32 width = current_pos.x - start_pos.x;
            f32 height = current_pos.y - start_pos.y;
            f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
            f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
            Rectangle rect = {x, y, fabs(width), fabs(height)};
            DrawRectangleLinesEx(rect, 4.0f, WHITE);
        }
    }

    Vector2 world = GetScreenToWorld2D(GetMousePosition(), cam);
    Vector2 pointer_pos = Vector2Subtract(world, (Vector2){24, 19});
    D_DrawTextureV(assets.textures[TEX_MOUSE_CURSOR], pointer_pos, WHITE);

    DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE);
    DrawFPS(SCREEN_WIDTH - 100, 10);
}

int main(void) {
    printf("Knight Size: %ld \n", sizeof(Knight));
    printf("SpriteAnimationPlayback Size: %ld \n", sizeof(SpriteAnimationPlayback));
    printf("Point Size: %ld \n", sizeof(Point));
    printf("Point Option Size: %ld \n", sizeof(PointOption));
    printf("Direction Size: %ld \n", sizeof(Direction));
    printf("KnightState Size: %ld \n", sizeof(KnightState));

    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");

    int monitor = GetCurrentMonitor();
    int monitor_width = GetMonitorWidth(monitor);
    int monitor_height = GetMonitorHeight(monitor);
    int win_pos_x = monitor_width / 2 - SCREEN_WIDTH / 2;
    int win_pos_y = monitor_height / 2 - SCREEN_HEIGHT / 2;
    SetWindowPosition(win_pos_x, win_pos_y);

    SetTargetFPS(TARGET_FPS);

    HideCursor();

    Camera2D cam = {0};
    cam.zoom = 1.0f;

    Assets assets = Init();

    GameState game = {0};
    game.knights = calloc(MAX_KNIGHTS, sizeof(Knight));
    game.anim_playbacks = calloc(MAX_KNIGHTS, sizeof(SpriteAnimationPlayback));
    const int entities = MAX_KNIGHTS;
    for (int i = 0; i < entities; i++) {

        int rand_x = GetRandomValue(165, 1130);
        int rand_y = 100 + ((float)950 / (float)entities) * i;
        game.knights[i].position = (Vector2){rand_x,rand_y};

        PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game.anim_playbacks[i]);
        int rand_frame = GetRandomValue(0, knight_anims[ANIM_KNIGHT_IDLE].total_frames);
        game.anim_playbacks[i].current_frame = rand_frame;
    }
    game.entity_count = entities;

    while (!WindowShouldClose()) {
        game.frame_count++;
        float dt = GetFrameTime();

        Update(&game, &cam, dt);

        BeginDrawing();
        {
            BeginMode2D(cam);
            {
                Draw(&game, assets, cam, dt);
            }
            EndMode2D();
        }
        EndDrawing();
    }

    free(game.knights);
    free(game.anim_playbacks);
    for (int i = 0; i < TEXTURES_BUF_SIZE; i++) {
        UnloadTexture(assets.textures[i]);
    }
    free(assets.textures);
    CloseWindow();
    return 0;
}
