GR :: #import "GetRect_LeftHanded"()(Type_Indicator = Ui_Type_Indicator);

Ui_Font :: struct {
    em_width: u32 = 1;
    character_height: u32 = 30;
}

Ui_Texture :: struct {
    name: string;
}

Ui_Rect :: struct {
    x, y, w, h: s32;  
};

Ui_Type_Indicator :: struct {
    Texture : Type : Ui_Texture;
    Window_Type: Type : s32;
    Font: Type: Ui_Font;
    Font_Effects: Type: u32;
};

Font :: Ui_Font;

defaultFont: Font;

ui_texture_counter : u32 = 0;

texture_load_from_memory :: (texture: *Ui_Texture, memory: []u8, srgb: bool, build_mipmaps: bool) -> bool {
    // texname := tprint("ui_tex_%", ui_texture_counter);
    // load_png_texture_from_memory(*GpuContext, copy_string(texname), memory);
    // ui_texture_counter += 1;
    // texture.name = copy_string(texname);
    return true;
}

gScissor : Ui_Rect;
gScissorActive : bool = false;

get_render_size :: () -> (s32, s32) {
    return 700, 700;
}

set_scissor :: (x0: s32, y0: s32, x1: s32, y1: s32)  {
    w,h := get_render_size();
    gScissor = .{x0, y0, x1 - x0, y1 - y0};
    gScissorActive = true;
}
clear_scissor :: () {
    gScissorActive = false;
}

gCurrentTexture : *Ui_Texture = null;

set_shader_for_color :: (enable_blend := false) {
    // immediate_flush(); // We want to do the flush so we can set the sampler for the entire set.
    // gCurrentTexture = null;
}


set_shader_for_images :: (texture: *Ui_Texture) {
    // if ENABLE_UI_DEBUG_LOGGING { print("Setting shader for textures.."); }
    // immediate_flush(); // We want to do the flush so we can set the sampler for the entire set.
    // gCurrentTexture = texture;
}

gPreppedText: string;
gPreppedTextWidth : s32;

prepare_text :: (font: *Ui_Type_Indicator.Font, text: string, effects: Ui_Type_Indicator.Font_Effects = 0) -> s64 {
    fonsSetFont(state.fons, state.font_default);
    fonsSetSize(state.fons, xx font.character_height);
    w := fonsTextBounds(state.fons, 0.0, 0.0, text.data, text.data + text.count, null);
    gPreppedText = text;
    gPreppedTextWidth = cast(s32) w;
    return cast(s64) w;
}
draw_prepared_text :: (font: *Ui_Type_Indicator.Font, x: s64, y: s64, text_color: Vector4, effects: Ui_Type_Indicator.Font_Effects = 0) {
    color := sfons_rgba(xx (255.0 * text_color.x), xx (255.0 * text_color.y), xx (255.0 * text_color.z), xx (255.0 * text_color.w));
    fonsSetColor(state.fons, color);
    result := cast(*u8) temporary_alloc(gPreppedText.count + 1);  // Add 1 for the zero.
    memcpy(result, gPreppedText.data, gPreppedText.count);
    result[gPreppedText.count] = 0;
    fonsDrawText(state.fons, xx x, xx y, result, null);
}
get_mouse_pointer_position :: (window: Ui_Type_Indicator.Window_Type, right_handed: bool) -> (x: int, y: int, success: bool) {
    return xx mpos.x, xx mpos.y, true;
}
get_font_at_size :: (memory: [] u8, pixel_height: int) -> *Font {
    f : *Font = New(Font);
    // f.character_height = cast(u32) pixel_height;
    // f.em_width = cast(u32) get_font_letter_width(109, cast (s32) pixel_height);
    return f;
}

// TODO: Figure out what to do with the normal?
immediate_triangle :: (p0: Vector3, p1: Vector3, p2: Vector3, c0 := Vector4.{1,1,1,1}, c1 := Vector4.{1,1,1,1}, c2 := Vector4.{1,1,1,1}, uv0 := Vector2.{}, uv1 := Vector2.{}, uv2 := Vector2.{}, normal := Vector3.{z=1}) {
    tri: Arb_Tri;
    tri.pos[0] = p0;
    tri.pos[1] = p1;
    tri.pos[2] = p2;

    tri.col[0] = c0;
    tri.col[1] = c1;
    tri.col[2] = c2;
    
    // This UV symbolizes that the sampler should not be used.
    nullUV : Vector2 = .{-4, -2};

    if gCurrentTexture == null {
        tri.uv[0] = nullUV;
        tri.uv[1] = nullUV;
        tri.uv[2] = nullUV;
    } else {
        tri.uv[0] = uv0;
        tri.uv[1] = uv2;
        tri.uv[2] = uv1;
    }

    arb_tri_add(tri);
}

immediate_quad :: (p0: Vector2, p1: Vector2, p2: Vector2, p3: Vector2, color := Vector4.{1,1,1,1}, uv0 := Vector2.{0,0}, uv1 := Vector2.{1,0}, uv2 := Vector2.{1,1}, uv3 := Vector2.{0, 1}) {
    to_3d_vec :: (v: Vector2) -> Vector3 {
        return .{v.x, v.y, 0.0};
    }
    immediate_triangle(to_3d_vec(p0), to_3d_vec(p1), to_3d_vec(p2), color, color, color, uv0, uv1, uv2);
    immediate_triangle(to_3d_vec(p0), to_3d_vec(p2), to_3d_vec(p3), color, color, color, uv0, uv2, uv3);
}

immediate_flush :: () {
    // arb_tri_flush();
}

init_ui :: () {
    dp : GR.Draw_Procs = .{
        texture_load_from_memory = texture_load_from_memory,
        set_scissor = set_scissor,
        clear_scissor = clear_scissor,
        set_shader_for_color = set_shader_for_color,
        set_shader_for_images = set_shader_for_images,
        prepare_text = prepare_text,
        draw_prepared_text = draw_prepared_text,
        get_mouse_pointer_position = get_mouse_pointer_position,
        get_font_at_size = get_font_at_size,
        immediate_triangle = immediate_triangle, // implemented
        immediate_quad = immediate_quad, // implemented
        immediate_flush = immediate_flush // implemented
    };

    GR.ui_init("", *dp);
}

tick_ui :: () {
    w,h :=  get_window_size();
    GR.ui_per_frame_update(1, xx w, xx h, get_time());
}

checkboxTest : bool = false;

get_font_at_size :: (pixel_height: int) -> *Font {
    list : []u8;
    return get_font_at_size(list, pixel_height);
}

idk : bool;

render_ui :: () {
    proc := GR.default_theme_procs[3];
    my_theme := proc();
    GR.set_default_theme(my_theme);  // Just in case we don't explicitly pass themes sometimes...!

    r := GR.get_rect(10, 10, 400, 30);
    pressed := GR.button(r, "GetRect render lfg!!", *my_theme.button_theme);

    r.y += 150;
    GR.base_checkbox(r, "CHECK!!!", idk, null);
}


