#define DQN_ASAN_POISON 1
#define DQN_ASAN_VET_POISON 1
#define DQN_ONLY_RECT
#define DQN_ONLY_WIN
#define DQN_ONLY_V2
#define DQN_ONLY_SLICE
#define DQN_ONLY_SARRAY
#define DQN_ONLY_DSMAP
#define DQN_ONLY_LIST
#define DQN_ONLY_FS
#define _CRT_SECURE_NO_WARNINGS
#define DQN_IMPLEMENTATION
#include "External/tely/External/dqn/dqn.h"

DQN_MSVC_WARNING_PUSH
DQN_MSVC_WARNING_DISABLE(4244) // warning C4244: 'argument': conversion from 'int' to 'short', possible loss of data
#define STB_RECT_PACK_IMPLEMENTATION
#include "External/tely/external/stb/stb_rect_pack.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "External/tely/external/stb/stb_image_write.h"

#define STB_IMAGE_IMPLEMENTATION
#include "External/tely/external/stb/stb_image.h"
DQN_MSVC_WARNING_POP

struct SpriteSpecification
{
    uint16_t frame_count;
    uint16_t frames_per_second;
};

int main(int argc, char const *argv[])
{
    if (argc != 3) {
        Dqn_Log_InfoF("USAGE: feely_pona_sprite_packer <sprite_spec.txt> <sprite directory>");
        return -1;
    }

    Dqn_Library_Init();
    Dqn_ThreadScratch scratch          = Dqn_Thread_GetScratch(nullptr);
    Dqn_String8       sprite_spec_path = Dqn_String8_InitCString8(argv[1]);
    Dqn_String8       dir              = Dqn_String8_InitCString8(argv[2]);

    // NOTE: Parse the sprite specifications =======================================================
    if (!Dqn_Fs_Exists(sprite_spec_path)) {
        Dqn_Log_ErrorF("Sprite specification file does not exist, we tried to find \"%.*s\" but it does not exist", DQN_STRING_FMT(sprite_spec_path));
        return -1;
    }

    Dqn_DSMap<SpriteSpecification> sprite_spec_table = Dqn_DSMap_Init<SpriteSpecification>(1024);
    {
        Dqn_ThreadScratch inner_scratch               = Dqn_Thread_GetScratch(scratch.arena);
        Dqn_String8 sprite_spec_buffer                = Dqn_Fs_Read(sprite_spec_path, inner_scratch.allocator);
        Dqn_String8SplitAllocResult sprite_spec_lines = Dqn_String8_SplitAlloc(inner_scratch.allocator, sprite_spec_buffer, DQN_STRING8("\n"));
        DQN_FOR_UINDEX(line_index, sprite_spec_lines.size) {
            Dqn_String8 line                       = sprite_spec_lines.data[line_index];
            Dqn_String8SplitAllocResult line_parts = Dqn_String8_SplitAlloc(inner_scratch.allocator, line, DQN_STRING8(";"));
            DQN_ASSERTF(line_parts.size == 2, "Line must have 2 parts in the sprite specification\n"
                                              "\n"
                                              "<animation name>;<frames per second>\\n\n"
                                              "\n"
                                              "Line was '%.*s' loaded from '%.*s'", DQN_STRING_FMT(line), sprite_spec_path);

            Dqn_String8 anim_name                    = line_parts.data[0];
            Dqn_String8ToU64Result frames_per_second = Dqn_String8_ToU64(line_parts.data[1], 0);
            DQN_ASSERTF(frames_per_second.success, "Frames per second was not a convertible number, line was '%.*s'", line);

            Dqn_DSMapResult<SpriteSpecification> slot = Dqn_DSMap_MakeKeyString8Copy(&sprite_spec_table, scratch.allocator, anim_name);
            slot.value->frames_per_second             = DQN_CAST(uint16_t)frames_per_second.value;
        }
    }

    // NOTE: Get the list of files =================================================================
    Dqn_List<Dqn_String8> file_list = Dqn_List_Init<Dqn_String8>(scratch.arena, 128);
    for (Dqn_Win_FolderIterator it = {}; Dqn_Win_FolderIterate(dir, &it); ) {
        if (Dqn_String8_EndsWithInsensitive(it.file_name, DQN_STRING8(".png"))) {
            Dqn_String8 *item = Dqn_List_Make(&file_list, Dqn_ZeroMem_Yes);
            *item             = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/%.*s", DQN_STRING_FMT(dir), DQN_STRING_FMT(it.file_name));
        }
    }

    // NOTE: Setup the rect-pack state =============================================================
    int atlas_size             = 2048;
    stbrp_node *nodes          = Dqn_Arena_NewArray(scratch.arena, stbrp_node, file_list.count, Dqn_ZeroMem_Yes);
    stbrp_context pack_context = {};
    stbrp_init_target(&pack_context, atlas_size, atlas_size, nodes, DQN_CAST(int)file_list.count);

    // NOTE: Load the sprites to determine their dimensions for rect packing =======================
    Dqn_SArray<stbrp_rect> rects = Dqn_SArray_Init<stbrp_rect>(scratch.arena, file_list.count, Dqn_ZeroMem_Yes);
    for (Dqn_ListIterator<Dqn_String8> it = {}; Dqn_List_Iterate<Dqn_String8>(&file_list, &it, 0);) {
        int      x = 0, y = 0, channels_in_file = 0;
        stbi_uc *pixels = stbi_load(it.data->data, &x, &y, &channels_in_file, 4 /*desired_channels*/);
        DQN_ASSERT(pixels);
        stbi_image_free(pixels);

        // NOTE: Add a rect to the packing context =================================================
        stbrp_rect *rect = Dqn_SArray_Make(&rects, Dqn_ZeroMem_Yes);
        rect->w          = x;
        rect->h          = y;

        // NOTE: Enumerate the number of frames for this animation =================================
        Dqn_String8 file_name                   = Dqn_String8_FileNameFromPath(*it.data);
        Dqn_String8 file_name_without_extension = Dqn_String8_BinarySplit(file_name, DQN_STRING8(".")).lhs;
        Dqn_String8BinarySplitResult split      = Dqn_String8_BinarySplitReverse(file_name_without_extension, DQN_STRING8("_"));
        Dqn_String8 anim_prefix                 = split.lhs;

        Dqn_DSMapResult<SpriteSpecification> slot = Dqn_DSMap_FindKeyString8(&sprite_spec_table, anim_prefix);
        DQN_ASSERTF(slot.found,
                    "\n\nSprite frame loaded from file\n"
                    "  '%.*s'\n"
                    "\n"
                    "however there's no corresponding entry in the specification file loaded at\n"
                    "  '%.*s'\n"
                    "\n"
                    "Add a line in format of <animation name>;<frames_per_second> to the file, e.g.\n"
                    "  %.*s;8\n",
                    DQN_STRING_FMT(*it.data),
                    DQN_STRING_FMT(sprite_spec_path),
                    DQN_STRING_FMT(anim_prefix));

        slot.value->frame_count++;
        Dqn_Log_InfoF("Packing sprite: %.*s", DQN_STRING_FMT(*it.data));
    }

    // NOTE: Pack the rects ========================================================================
    if (stbrp_pack_rects(&pack_context, rects.data, DQN_CAST(int)rects.size) != 1) {
        Dqn_Log_ErrorF("STB rect pack failed to pack font rects into rectangle [width=%d, height=%d, num_rects=%d]",
                       atlas_size,
                       atlas_size,
                       file_list.count);
        return -1;
    }

    // NOTE: Load the files once more and generate the final image =================================
    int final_bpp          = 4;
    int final_image_stride = atlas_size * final_bpp;
    char *final_image      = Dqn_Arena_NewArray(scratch.arena, char, atlas_size * final_image_stride, Dqn_ZeroMem_Yes);
    Dqn_String8 atlas_path = Dqn_String8_InitF(scratch.allocator, "%.*s.png", DQN_STRING_FMT(dir));

    // NOTE: Generate the meta file ================================================================
    Dqn_String8 meta_path = Dqn_String8_InitF(scratch.allocator, "%.*s.txt", DQN_STRING_FMT(dir));
    Dqn_FsFile meta_file  = Dqn_Fs_OpenFile(meta_path, Dqn_FsFileOpen_CreateAlways, Dqn_FsFileAccess_Write);
    Dqn_Fs_WriteFileF(&meta_file,
                      "@file;%.*s;%d;%d\n",
                      DQN_STRING_FMT(Dqn_String8_FileNameFromPath(atlas_path)),
                      DQN_CAST(int) file_list.count,
                      DQN_CAST(int) sprite_spec_table.occupied - 1);

    Dqn_Log_InfoF("Generating meta file: %.*s", DQN_STRING_FMT(meta_path));
    Dqn_String8 anim_prefix = {};
    for (Dqn_ListIterator<Dqn_String8> it = {}; Dqn_List_Iterate<Dqn_String8>(&file_list, &it, 0);) {
        int      w, h, channels_in_file;
        stbi_uc *loaded_image = stbi_load(it.data->data, &w, &h, &channels_in_file, 4 /*desired_channels*/);

        stbrp_rect packed_rect = rects.data[it.index];
        char *src              = DQN_CAST(char *)loaded_image;

        // NOTE: Generate the image ================================================================
        for (int y = 0; y < packed_rect.h; y++) {
            char *row = final_image + ((packed_rect.y + y) * final_image_stride) + (packed_rect.x * final_bpp);
            for (int x = 0; x < packed_rect.w; x++) {
                *row++ = *src++;
                *row++ = *src++;
                *row++ = *src++;
                *row++ = *src++;
            }
        }

        stbi_image_free(loaded_image);

        // NOTE: Write the sprite and the rects to the sheet
        Dqn_String8 file_name                   = Dqn_String8_FileNameFromPath(*it.data);
        Dqn_String8 file_name_without_extension = Dqn_String8_BinarySplit(file_name, DQN_STRING8(".")).lhs;

        // NOTE: Detect what animation we are currently processing =================================
        if (anim_prefix.size == 0 || !Dqn_String8_StartsWith(file_name_without_extension, anim_prefix)) {
            // NOTE: Anim prefix is different, we are starting a new animation- mark it accordingly
            Dqn_String8BinarySplitResult split = Dqn_String8_BinarySplitReverse(file_name_without_extension, DQN_STRING8("_"));
            anim_prefix                        = split.lhs;

            Dqn_DSMapResult<SpriteSpecification> slot = Dqn_DSMap_FindKeyString8(&sprite_spec_table, anim_prefix);
            Dqn_Fs_WriteFileF(&meta_file, "@anim;%.*s;%u;%u\n", DQN_STRING_FMT(anim_prefix), slot.value->frames_per_second, slot.value->frame_count);
        }

        // NOTE: Write the sprite rectangles in ====================================================
        Dqn_Fs_WriteFileF(&meta_file, "%d;%d;%d;%d", packed_rect.x, packed_rect.y, packed_rect.w, packed_rect.h);
        if (it.index != (file_list.count - 1))
            Dqn_Fs_WriteFileF(&meta_file, "\n");
    }

    Dqn_Log_InfoF("Generating atlas: %.*s", DQN_STRING_FMT(atlas_path));
    stbi_write_png(atlas_path.data, atlas_size, atlas_size, 4, final_image, final_image_stride);
    return 0;
}
