#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 ANIM_SPEED 8
#define MAX_ANIMATION_PLAYBACKS 64
#define MAX_KNIGHTS 64

#define DEBUG_MODE_ENABLED

bool global_debug_mode;

#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 {
    Point position;
    Point origin;
    Vector2 velocity;
    Direction look_dir;
    KnightState state;
} Knight;

typedef struct {
    int frame_count;
    Knight knight;
    // Rectangle* sprite_rects;
    Point camera_position;
    Knight *selected_knight;
    PointOrNone *target_points;
    SpriteAnimationPlayback* anim_playbacks;
    int anim_playbacks_count;
} GameState;

typedef struct {
    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[0] = LoadTexture("assets/Terrain/Ground/Tilemap_Flat.png");
    assets.textures[1] =
        LoadTexture("assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png");
    assets.textures[2] = LoadTexture("assets/UI/Pointers/01.png");
    assets.textures[3] = LoadTexture("assets/UI/Pointers/02.png");
    return assets;
}

void Update(GameState *game, Camera2D *cam, float dt) {
    TickSpriteAnimations(&knight_anims[0], game->anim_playbacks, 1);

    if (IsMouseButtonPressed(0)) {
        Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
        // 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
        Rectangle mouse_select_area = {
            game->knight.position.x - knight_colrect_select.width / 2,
            game->knight.position.y - knight_colrect_select.height / 2,
            knight_colrect_select.width,
            knight_colrect_select.height,
        };
        if (CheckCollisionPointRec(mouse_pos, mouse_select_area)) {
            game->selected_knight = &game->knight;
        } else {
            game->selected_knight = NULL;
        }
    }

    if (IsMouseButtonPressed(1)) {
        Point mouse_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
        if (game->selected_knight != NULL) {
            game->target_points[0] = (PointOrNone){.type = POINT, .value.point = mouse_pos };
        }
    }

    // for (int i = 0; i < MAX_KNIGHTS; i++) {
    Vector2 input_vel = {0};
    if (game->target_points[0].type == POINT) {
        Vector2 target = game->target_points[0].value.point;
        if (Vector2DistanceSqr(target, game->knight.position) < 0.5f) {
            game->target_points[0].type = NONE;
        } else {
            input_vel.x = game->knight.position.x - target.x < 0.0f ? 1 : -1;
            game->knight.position = Vector2MoveTowards(game->knight.position, target, 4.0f);
        }
    }

    // const float movement_speed = 250.0f * dt;
    // if (IsKeyDown(KEY_RIGHT)) {
    //     input_vel.x = 1;
    // }
    // if (IsKeyDown(KEY_LEFT)) {
    //     input_vel.x = -1;
    // }
    // if (IsKeyDown(KEY_UP)) {
    //     input_vel.y = -1;
    // }
    // if (IsKeyDown(KEY_DOWN)) {
    //     input_vel.y = 1;
    // }
    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->knight.state != KNIGHT_ATTACKING) {
        game->knight.state = KNIGHT_ATTACKING;
        game->anim_playbacks[0].anim_id = ANIM_KNIGHT_ATTACK_SIDE1;
        PlayAnimation(&game->anim_playbacks[0], game->anim_playbacks[0]);
    }
    if (IsKeyPressed(KEY_F1)) {
        global_debug_mode = !global_debug_mode;
    }
    if (game->knight.state == KNIGHT_ATTACKING) {
        game->anim_playbacks[0].anim_id = ANIM_KNIGHT_ATTACK_SIDE1;
        if (game->anim_playbacks[0].is_finished) {
            game->knight.state = KNIGHT_IDLE;
        }
    } else {
        if (input_vel.x != 0) {
            game->knight.look_dir = input_vel.x == -1 ? DIR_LEFT : DIR_RIGHT;
        }
        // game->knight.velocity = Vector2Normalize(input_vel);
        // game->knight.velocity = Vector2Scale(game->knight.velocity, movement_speed);
        // game->knight.position = Vector2Add(game->knight.position, game->knight.velocity);
        if (input_vel.x != 0 || input_vel.y != 0) {
            game->anim_playbacks[0].anim_id = ANIM_KNIGHT_RUN;
            game->knight.state = KNIGHT_RUNNING;
        } else {
            game->anim_playbacks[0].anim_id = ANIM_KNIGHT_IDLE;
            game->knight.state = KNIGHT_IDLE;
        }
    }
}

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

    int size = 32;
    int topx = 300;
    int topy = 32;
    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[0], src_rect, pos, WHITE);
        }
    }

    if (game->target_points[0].type == POINT) {
        Vector2 marker_pos = game->target_points[0].value.point;
        marker_pos.x -= assets.textures[3].width / 2;
        // marker_pos.y -= assets.textures[3].height / 2;
        D_DrawTextureV(assets.textures[3], marker_pos, WHITE);
    }

    for (int i = 0; i < game->anim_playbacks_count; i++) {
        SpriteAnimationPlayback *playback = &game->anim_playbacks[i];
        SpriteAnimation *anim = &knight_anims[playback->anim_id];
        Rectangle src_rect = anim->src_rect;
        src_rect.x = playback->current_frame * anim->src_rect.width;
        src_rect.y = anim->src_rect.y;
        if (game->knight.look_dir == DIR_LEFT) {
            src_rect.width = -abs((int)src_rect.width);
        }
        // Vector2 pos = Vector2Subtract(game->knight.position, knight_origin);
        Rectangle dest_rect = {game->knight.position.x, game->knight.position.y, 192, 192};
        D_DrawTexturePro(assets.textures[1], src_rect, dest_rect, knight_origin, 0.0f, WHITE);

        if (game->selected_knight) {
            Vector2 kpos = game->knight.position;
            Rectangle knight_col_area = {
                kpos.x - knight_colrect_select.width / 2,
                kpos.y - knight_colrect_select.height / 2,
                knight_colrect_select.width,
                knight_colrect_select.height,
            };
            // Color color = game->selected_knight == NULL ? RED : GREEN;
            DrawRectangleLinesEx(knight_col_area, 2.0f, GREEN);
        }
    }

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

int main(void) {
    const int screen_width = 1600;
    const int screen_height = 1080;

    InitWindow(screen_width, screen_height, "raylib [core] example - basic window");

    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;

    GameState game = {0};
    game.anim_playbacks = malloc(sizeof(SpriteAnimationPlayback) * MAX_ANIMATION_PLAYBACKS);
    game.anim_playbacks_count = 1;
    // First one is idle
    game.anim_playbacks[0] = (SpriteAnimationPlayback){0};
    game.target_points = calloc(MAX_KNIGHTS, sizeof(Point));
    game.knight = (Knight){0};
    game.knight.position = (Vector2){100,100};

    Assets assets = Init();

    PlayAnimation(&game.anim_playbacks[0], game.anim_playbacks[0]);
    // const int idle
    while (!WindowShouldClose()) {
        game.frame_count++;
        float dt = GetFrameTime();

        Update(&game, &cam, dt);

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

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