#pragma once

#include "pxl8_gfx2d.h"
#include "pxl8_gfx3d.h"
#include "pxl8_hal.h"
#include "pxl8_colormap.h"
#include "pxl8_palette.h"
#include "pxl8_types.h"
#include "pxl8_gui_palette.h"

typedef struct pxl8_gfx pxl8_gfx;

typedef enum pxl8_gfx_effect {
    PXL8_GFX_EFFECT_GLOWS = 0,
} pxl8_gfx_effect;

#define PXL8_MAX_GLOWS 256

typedef enum pxl8_glow_shape {
    PXL8_GLOW_CIRCLE = 0,
    PXL8_GLOW_DIAMOND = 1,
    PXL8_GLOW_SHAFT = 2,
} pxl8_glow_shape;

typedef struct pxl8_glow_source {
    u8 color;
    u16 depth;
    u8 height;
    u16 intensity;
    u8 radius;
    pxl8_glow_shape shape;
    i16 x;
    i16 y;
} pxl8_glow_source;

static inline pxl8_glow_source pxl8_glow_create(i32 x, i32 y, u8 radius, u16 intensity, u8 color) {
    return (pxl8_glow_source){
        .color = color,
        .depth = 0xFFFF,
        .height = 0,
        .intensity = intensity,
        .radius = radius,
        .shape = PXL8_GLOW_CIRCLE,
        .x = (i16)x,
        .y = (i16)y,
    };
}

static inline pxl8_glow_source pxl8_glow_with_depth(pxl8_glow_source g, u16 depth) {
    g.depth = depth;
    return g;
}

static inline pxl8_glow_source pxl8_glow_with_shape(pxl8_glow_source g, pxl8_glow_shape shape) {
    g.shape = shape;
    return g;
}

static inline pxl8_glow_source pxl8_glow_with_height(pxl8_glow_source g, u8 height) {
    g.height = height;
    return g;
}

#ifdef __cplusplus
extern "C" {
#endif

pxl8_gfx* pxl8_gfx_create(const pxl8_hal* hal, void* platform_data, pxl8_pixel_mode mode, pxl8_resolution resolution);
void pxl8_gfx_destroy(pxl8_gfx* gfx);

void pxl8_gfx_present(pxl8_gfx* gfx);
void pxl8_gfx_update(pxl8_gfx* gfx, f32 dt);
void pxl8_gfx_upload_framebuffer(pxl8_gfx* gfx);

u8 pxl8_gfx_find_color(pxl8_gfx* gfx, u32 color);

pxl8_bounds pxl8_gfx_get_bounds(pxl8_gfx* gfx);
u8* pxl8_gfx_get_framebuffer_indexed(pxl8_gfx* gfx);
u16* pxl8_gfx_get_framebuffer_hicolor(pxl8_gfx* gfx);
i32 pxl8_gfx_get_height(const pxl8_gfx* gfx);
pxl8_palette* pxl8_gfx_palette(pxl8_gfx* gfx);
pxl8_colormap* pxl8_gfx_colormap(pxl8_gfx* gfx);
pxl8_pixel_mode pxl8_gfx_get_pixel_mode(pxl8_gfx* gfx);
i32 pxl8_gfx_get_width(const pxl8_gfx* gfx);

i32 pxl8_gfx_load_palette(pxl8_gfx* gfx, const char* filepath);
void pxl8_gfx_project(pxl8_gfx* gfx, f32 left, f32 right, f32 top, f32 bottom);
void pxl8_gfx_set_palette_colors(pxl8_gfx* gfx, const u32* colors, u16 count);
void pxl8_gfx_set_viewport(pxl8_gfx* gfx, pxl8_viewport vp);
pxl8_viewport pxl8_gfx_viewport(pxl8_bounds bounds, i32 width, i32 height);

void pxl8_gfx_clear_textures(pxl8_gfx* gfx);
pxl8_result pxl8_gfx_create_texture(pxl8_gfx* gfx, const u8* pixels, u32 width, u32 height);
pxl8_result pxl8_gfx_load_font_atlas(pxl8_gfx* gfx);
pxl8_result pxl8_gfx_load_sprite(pxl8_gfx* gfx, const char* path);

bool pxl8_gfx_push_target(pxl8_gfx* gfx);
void pxl8_gfx_pop_target(pxl8_gfx* gfx);

void pxl8_gfx_apply_effect(pxl8_gfx* gfx, pxl8_gfx_effect effect, const void* params, u32 count);
void pxl8_gfx_blend_tables_update(pxl8_gfx* gfx);
void pxl8_gfx_colormap_update(pxl8_gfx* gfx);
void pxl8_gfx_ensure_blend_tables(pxl8_gfx* gfx);

u8 pxl8_gfx_find_closest_color(pxl8_gfx* gfx, u8 r, u8 g, u8 b);
u8 pxl8_gfx_ui_color(pxl8_gfx* gfx, u8 index);

#ifdef __cplusplus
}
#endif
