#include "../../l2/allie/allie.c"

AliceGenericMeshPath AliceGenericMeshPath_for_log(SpanU8 root_dir, U64 w, U64 r, U64 k) {
    return (AliceGenericMeshPath){
        .topology_path = VecU8_fmt("%s/gen/l2/models/log_%u_%u_%u.AliceGenericMesh", root_dir, w, r, k),
        .diffuse_texture_path = VecU8_fmt("%s/src/l3/textures/log_%u_%u_%u_diffuse.png", root_dir, w, r, k),
        .normal_texture_path = VecU8_fmt("%s/gen/l2/textures/log_%u_%u_%u_NORMAL.png", root_dir, w, r, k),
        .specular_texture_path = VecU8_fmt("%s/src/l3/textures/log_%u_%u_%u_specular.png", root_dir, w, r, k),
    };
}

AliceGenericMeshPath AliceGenericMeshPath_for_puck(){
    return (AliceGenericMeshPath){
        .topology_path = VecU8_fmt("gen/l2/models/puck.AliceGenericMesh"),
        .diffuse_texture_path = VecU8_fmt("src/l3/textures/puck_diffuse.png"),
        .normal_texture_path = VecU8_fmt("gen/l2/textures/puck_NORMAL.png"),
        .specular_texture_path = VecU8_fmt("src/l3/textures/puck_specular.png"),
    };
}

typedef struct{
    Alice* alice;
    LucyFace* font_face;
    RBTreeNodeLucyFaceFixedSize* font_face_of_size_40;
} R4AlphaStuff;

void main_h_on_wayland_keyboard_key(void* data, U32 keysym, U32 act){
}

void main_h_on_another_frame(void* data, float fl){
    R4AlphaStuff *st = data;
    Alice* alice = st->alice;
    margaret_ns_time TIME = margaret_clock_gettime_monotonic_raw();
    printf("Updating text\n");
    LucyRenderer_clear(&alice->lucy_renderer);
    VecU8 text = VecU8_fmt("Time is %u.%u\nHave a good day sir\n", (U64)TIME.tv_sec, (U64)TIME.tv_nsec);
    LucyRenderer_add_text(&alice->lucy_renderer, st->font_face_of_size_40, (vec4){0.1f, 0.2f, 0, 1}, 0,
        VecU8_to_span(&text), (ivec2){100, 100});
}

int main(){
    R4AlphaStuff st;
    Alice* alice = Alice_new();
    st.alice = alice;
    st.alice->guest = &st;

    st.font_face = LucyFace_new(st.alice->ft_library, &st.alice->lucy_cache,
        VecU8_fmt("%s/src/l3/fonts/DMSerifText-Regular.ttf", cstr(".")));
    st.font_face_of_size_40 = LucyFace_of_size(st.font_face, 13);
    VecLucyGlyphCachingRequest lucy_requests = VecLucyGlyphCachingRequest_new();
    VecU32Segment ranges_needed = VecU32Segment_new();
    VecU32Segment_append(&ranges_needed, (U32Segment){.start = 32, .len = 126 - 32 + 1});
    VecLucyGlyphCachingRequest_append(&lucy_requests, (LucyGlyphCachingRequest){
        .sized_face = st.font_face_of_size_40, .codepoint_ranges = ranges_needed,
    });
    LucyGlyphCache_add_glyphs(lucy_requests);
    lucy_requests = VecLucyGlyphCachingRequest_new();
    ranges_needed = VecU32Segment_new();
    VecU32Segment_append(&ranges_needed, (U32Segment){.start = 0x430, .len = 0x44f - 0x430 + 1});
    VecLucyGlyphCachingRequest_append(&lucy_requests, (LucyGlyphCachingRequest){
        .sized_face = st.font_face_of_size_40, .codepoint_ranges = ranges_needed,
    });

    LucyGlyphCache_add_glyphs(lucy_requests);
    LucyRenderer_add_text(&st.alice->lucy_renderer, st.font_face_of_size_40, (vec4){1, 0, 0, 1}, 0,
        cstr("Bebra budet\notnyahana"), (ivec2){10, 10});

    ListNodeAliceGenericMeshHand* model_gen = Alice_add_generic_mesh(st.alice, AliceGenericMeshPath_for_log(cstr("."), 10, 2, 6));
    AliceGenericMeshHand_resize_instance_arr(st.alice, &model_gen->el, 1);

    for (int X = 0; X < 1; X++) {
        for (int Z = 0; Z < 1; Z++) {
            AliceGenericMeshHand_set_inst(&model_gen->el, X * 10 + Z, (GenericMeshInstanceInc){
                .model_t = marie_translation_mat4((vec3){11.f * (float)X, -6, 4.f * (float)Z}),
            });
        }
    }

    // ListNodeAliceShinyMeshHand *model_sh = Alice_add_shiny_mesh(st->alice, vcstr("./gen/l2/models/cube.AliceShinyMesh"));
    // AliceShinyMeshHand_resize_instance_arr(st->alice, &model_sh->el, 100);

    // for (int X = 0; X < 10; X++) {
    //     for (int Z = 0; Z < 10; Z++) {
    //         AliceShinyMeshHand_set_inst(&model_sh->el, X * 10 + Z, (ShinyMeshInstanceInc){
    //             .color_on = {0, 1, 0}, .color_off = {0.3f, 0.6f, 0.3f},
    //             .model_t = marie_translation_mat4((vec3){11.f * (float)X - 20, 10, 4.f * (float)Z - 10}),
    //         });
    //     }
    // }

    // Pipeline0UBO* ubo = (Pipeline0UBO*)MargaretSubbuf_get_mapped(&st->alice->pipeline0_ubo.staging);
    // assert(pipeline_0_ubo_point_light_max_count >= 100);
    // ubo->point_light_count = 100;
    // ubo->spotlight_count = 0;
    // for (int X = 0; X < 10; X++) {
    //     for (int Z = 0; Z < 10; Z++) {
    //         ubo->point_light_arr[X * 10 + Z] = (Pipeline0PointLight){
    //             .pos = (vec3){11.f * (float)X - 20, 10, 4.f * (float)Z - 10},
    //             .color = {5, 5, 5}
    //         };
    //     }
    // }
    // ubo->point_light_arr[0].color = (vec3){100, 100, 100};


    // ListNodeAliceGenericMeshHand* model_puck = Alice_add_generic_mesh(st->alice, AliceGenericMeshPath_for_puck());
    // AliceGenericMeshHand_resize_instance_arr(st->alice, &model_puck->el, 100);
    // for (int X = 0; X < 10; X++) {
    //     for (int Z = 0; Z < 10; Z++) {
    //         AliceGenericMeshHand_set_inst(&model_puck->el, X * 10 + Z, (GenericMeshInstanceInc){
    //             .model_t = marie_translation_mat4((vec3){11.f * (float)X - 20, -1, 4.f * (float)Z - 10}),
    //         });
    //     }
    // }
    Alice_mainloop(st.alice, &(AliceCallbacks){
        .on_wl_keyboard_key = main_h_on_wayland_keyboard_key,
        .on_another_frame = main_h_on_another_frame,
    });
    return 0;
}