//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
// #include <SDL2/SDL_ttf.h>

#include <stdio.h>
#include <stdbool.h>

//Screen dimension constants
#define ANCHO_PANTALLA 640
#define ALTURA_PANTALLA 480

uint16_t seed = 1816;

uint16_t rand16(void){
    seed ^= (uint16_t)(seed << 13);
    seed ^= (uint16_t)(seed >> 9);
    seed ^= (uint16_t)(seed << 7);
    return seed;
}

typedef struct{
    int16_t x;
    int16_t y;
    uint8_t vel;
    int8_t dir[2];
    uint8_t textura_i;
} Cabeza;

SDL_Window *ventana;
SDL_Renderer *renderer;

SDL_Texture *cargar_textura_y_avisar_error(char *fn){
    SDL_Texture *textura = IMG_LoadTexture(renderer, fn);
    if(!textura) printf("Error cargando textura: %s\n", IMG_GetError());
    return textura;
}

int main( int argc, char* args[] )
{
    if(SDL_Init(SDL_INIT_EVERYTHING)) { printf("Error inicializando SDL: %s\n", SDL_GetError()); return 1; }

    ventana = SDL_CreateWindow(
        "SDL Test",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        ANCHO_PANTALLA, ALTURA_PANTALLA,
        0
    );

    renderer = SDL_CreateRenderer(ventana, -1, 0);

    // cargar_textura_y_avisar_error("imagenes/kiki.png");


    bool seguir = true;

    SDL_Texture *cabezas_textura[] = {
        cargar_textura_y_avisar_error("imagenes/kiki_floating_head.png"),
        cargar_textura_y_avisar_error("imagenes/kiki_small_floating_head.png"),
    };

    Cabeza cabezas[1024];

    uint16_t cabezas_i = 0;

    while(seguir){
        SDL_Event evento;

        while(SDL_PollEvent(&evento)){
            switch(evento.type){
                case SDL_QUIT: {
                    seguir = false;
                    break;
                }
                case SDL_KEYDOWN: {

                    if(cabezas_i == 1024) break;

                    cabezas[cabezas_i++] = (Cabeza){
                        .x = rand16() % ANCHO_PANTALLA+1,
                        .y = rand16() % ALTURA_PANTALLA+1,
                        .vel = 1 + rand16() % 50,
                        .textura_i = rand16() % 2,
                        .dir = {1,1}
                    };
                    break;
                }
                default: break;
            }
        }

        SDL_RenderClear(renderer);
        SDL_SetRenderDrawColor(renderer, rand16() % 256, rand16() % 256, rand16() % 256, rand16() % 256);
        // SDL_RenderCopy(renderer, textura, NULL, NULL);

        for(uint16_t i = 0; i < cabezas_i; i++){
            Cabeza *cabeza = &cabezas[i];
            SDL_Texture *textura = cabezas_textura[cabeza->textura_i];

            int w, h;

            SDL_QueryTexture(textura, NULL, NULL, &w, &h);

            cabeza->x += cabeza->vel * cabeza->dir[0];
            cabeza->y += cabeza->vel * cabeza->dir[1];
            
            if(cabeza->x <= 0) cabeza->dir[0] = 1;
            if(cabeza->y <= 0) cabeza->dir[1] = 1;
            if(cabeza->x + w >= ANCHO_PANTALLA) cabeza->dir[0] = -1;
            if(cabeza->y + h >= ALTURA_PANTALLA) cabeza->dir[1] = -1;

            SDL_RenderCopy(renderer, textura, NULL, &(SDL_Rect){
                    .x = cabeza->x,
                    .y = cabeza->y,
                    .w = w,
                    .h = h,
                }
            );
        }

        SDL_RenderPresent(renderer);
        SDL_Delay(50);
    }


    return 0;
}