#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <optional>
#include <iostream>

using namespace std;

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

GameState *InitState() {
    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 = {};
    assets.textures = (Texture*)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");

    auto game = new GameState;
    game->should_reset = false;
    game->assets = assets;
    // TODO: Probably want to use idiomatic C++ here?
    game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
    game->anim_playbacks = (SpriteAnimationPlayback*)calloc(MAX_KNIGHTS, sizeof(SpriteAnimationPlayback));
    const int entities = MAX_KNIGHTS;
    for (int i = 0; i < entities; i++) {
        f32 rand_x = GetRandomValue(165, 1130);
        f32 rand_y = 100 + ((float)950 / (float)entities) * i;
        game->knights[i].position = {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;

    game->camera = Camera2D{.offset = {0, 0}, .target = {0, 0}, .rotation = 0.0f, .zoom = 1.0f};

    return game;
}

GameState *Init() {
    SetTraceLogLevel(4);
    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();

    return InitState();
}

void Input(GameState *state) {
    if (IsMouseButtonPressed(0)) {
        if (IsKeyDown(KEY_LEFT_CONTROL)) {
            state->drag_camera_start_pos = optional<Point>{GetMousePosition()};
            state->camera_target_start_pos = state->camera.target;
        } else {
            auto point = GetScreenToWorld2D(GetMousePosition(), state->camera);
            state->selection_mouse_start_pos = optional<Point>{point};
        }
    }
    if (IsMouseButtonReleased(0)) {
        state->selection_mouse_start_pos = optional<Point>{};
        state->drag_camera_start_pos = optional<Point>{};
    }

    if (IsMouseButtonPressed(1)) {
        Point target_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
        bool any = false;
        for (int i = 0; i < state->entity_count; i++) {
            if (state->knights[i].selected) {
                state->knights[i].move_target_point = target_pos;
                state->knights[i].ordered_to_move = true;
                any = true;
            }
        }
        if (any) {
            state->selected_point = optional<Point>{target_pos};
        }
    }

    if (IsKeyPressed(KEY_SPACE)) {
        for (int i = 0; i < state->entity_count; i++) {
            auto& knight = state->knights[i];
            if (knight.selected && knight.state != KnightState::ATTACKING) {
                knight.ordered_to_move = false;
                knight.state = KnightState::ATTACKING;
                PlayAnimation(ANIM_KNIGHT_ATTACK_SIDE1, knight_anims, &state->anim_playbacks[i]);
            }
        }
    }
    if (IsKeyPressed(KEY_F1)) {
        global_debug_mode = !global_debug_mode;
    }
    if (IsKeyPressed(KEY_F2)) {
        state->should_reset = true;
        printf("Resetting GameState\n");
    }
}

void Update(GameState *state) {
    // cout << "Cool" << endl;
    if (state->selection_mouse_start_pos.has_value()) {
        Point start_pos = state->selection_mouse_start_pos.value();
        Point current_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);

        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 < state->entity_count; i++) {
            Rectangle mouse_select_area = {
                state->knights[i].position.x + knight_colrect_select.x,
                state->knights[i].position.y + knight_colrect_select.y,
                knight_colrect_select.width,
                knight_colrect_select.height,
            };
            state->knights[i].selected = CheckCollisionRecs(rect, mouse_select_area);
        }
    }
    if (state->drag_camera_start_pos.has_value()) {
        Point start_pos = state->drag_camera_start_pos.value();
        state->camera.target = state->camera_target_start_pos + (start_pos - GetMousePosition());
    }
    float dt = 0.33;
    const float cam_move_speed = 1050.0f * dt;
    // TODO: This was moved out of Input
    Vector2 cam_vel = {};
    cam_vel = Vector2Normalize(cam_vel);
    state->camera_position = Vector2Scale(cam_vel, cam_move_speed);
    state->camera.offset = Vector2Add(state->camera.offset, state->camera_position);
    // Process animation data
    TickSpriteAnimations(state->anim_playbacks, state->entity_count);

    // Handle knight movement if they are moving somewhere
    for (int i = 0; i < state->entity_count; i++) {
        Vector2 input_vel = {};
        Knight *knight = &state->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 == KnightState::ATTACKING) {
            if (IsAnimationFinished(state->anim_playbacks[i])) {
                knight->state = KnightState::IDLE;
                PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &state->anim_playbacks[i]);
            }
        } else {
            if (input_vel.x != 0) {
                knight->look_dir = input_vel.x == -1 ? Direction::LEFT : Direction::RIGHT;
            }
            if (input_vel.x != 0 || input_vel.y != 0) {
                if (knight->state == KnightState::IDLE) {
                    PlayAnimation(ANIM_KNIGHT_RUN, knight_anims, &state->anim_playbacks[i]);
                    knight->state = KnightState::RUNNING;
                }
            } else {
                if (knight->state == KnightState::RUNNING) {
                    PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &state->anim_playbacks[i]);
                    knight->state = KnightState::IDLE;
                }
            }
        }
        if (i == 0) continue;
        // TODO: This is likely getting called every frame, probably best to have a dirty flag to detect movement
        // Sort the entities by y position using Insertion Sort
        int j = i;
        while (j > 0 && state->knights[j].position.y < state->knights[j - 1].position.y) {
            // Swap position
            Knight temp_k = state->knights[j - 1];
            state->knights[j - 1] = state->knights[j];
            state->knights[j] = temp_k;
            // Swap animations
            SpriteAnimationPlayback temp_a = state->anim_playbacks[j - 1];
            state->anim_playbacks[j - 1] = state->anim_playbacks[j];
            state->anim_playbacks[j] = temp_a;
            j--;
        }
    }
}

void Draw2D(GameState *state) {
    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 = {(f32)(32 * col + topx), (f32)(32 * row + topy)};
            Rectangle src_rect = {(f32)(32 * atlas_col), (f32)(32 * atlas_row), 32, 32};
            D_DrawTextureRec(state->assets.textures[TEX_GROUND], src_rect, pos, WHITE);
        }
    }

    if (state->selected_point.has_value()) {
        Vector2 marker_pos = state->selected_point.value();
        marker_pos.x -= state->assets.textures[TEX_TARGET_RETICLE].width / 2;
        marker_pos.y -= state->assets.textures[TEX_TARGET_RETICLE].height / 2;
        D_DrawTextureV(state->assets.textures[TEX_TARGET_RETICLE], marker_pos, WHITE);
    }

    // for (int i = 0; i < game->anim_playbacks_count; i++) {
    for (int i = 0; i < state->entity_count; i++) {
        Knight *knight = &state->knights[i];
        SpriteAnimationPlayback *playback = &state->anim_playbacks[i];
        Rectangle src_rect = {
            (float)(playback->current_frame * knight_sprite_size),
            (float)(playback->row * knight_sprite_size),
            knight_sprite_size,
            knight_sprite_size,
        };
        // TODO: This might be a nice place to optimize
        if (knight->look_dir == Direction::LEFT) {
            src_rect.width = -abs((int)src_rect.width);
        }
        Rectangle dest_rect = {knight->position.x, knight->position.y, 192, 192};
        Texture2D tex = state->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,
            };
            DrawRectangleLinesEx(knight_col_area, 2.0f, GREEN);
        }
    }

    if (state->selection_mouse_start_pos.has_value()) {
        Point start_pos = state->selection_mouse_start_pos.value();
        Point current_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);

        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(), state->camera);
    Vector2 pointer_pos = Vector2Subtract(world, {24, 19});
    D_DrawTextureV(state->assets.textures[TEX_MOUSE_CURSOR], pointer_pos, WHITE);
}

extern "C" {
    GameState* game_init() {
        return Init();
    }

    GameState* game_init_state() {
        return InitState();
    }

    void game_step(GameState *state) {
        try {
            Input(state);
            Update(state);
            BeginDrawing();
            {
                ClearBackground({100, 149, 237, 255});
                BeginMode2D(state->camera);
                {
                    Draw2D(state);
                }
                EndMode2D();

                DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE);
                DrawFPS(SCREEN_WIDTH - 100, 10);
            }
            EndDrawing();
        }
        catch (const exception& e) {
            printf("Exception: %s\n", e.what());
            TraceLog(LOG_ERROR, "Exception: %s", e.what());
        }
    }

    void game_cleanup(GameState *state) {
        // free(state->assets.textures);
        delete state;
        printf("Cleaning Up\n");
        CloseWindow();
    }

    bool window_should_close() {
        return WindowShouldClose();
    }
}
