//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>

#include "../../truco.h"
#include "../../interfaces/truco_interfaz_comun.h"
#include "../../interfaces/truco_stdio.h"

//Screen dimension constants
#define ESCALA 3
#define ANCHO_PANTALLA 384
#define ALTURA_PANTALLA 216

#define MAX_INTERACTUABLES_MOUSE MAX_CARTAS_MANO

SDL_Window *ventana;
SDL_Renderer *renderer;

SDL_Texture *spritesheet;

SDL_Texture *texturas_cartas[40];

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;
}

typedef struct{
    SDL_Rect rect;
    uint8_t valor;
} InteractuableMouse;

#define ANCHO_CARTA 34
#define LARGO_CARTA 61

#define MARGEN_ANCHO_CARTA (ANCHO_CARTA + 10)
#define MARGEN_LARGO_CARTA (LARGO_CARTA + 5)

//La idea es tener solo w y h en el rect, crear un rect nuevo y pasarle x e y,
//que es lo que suele variar cuando se usa esta función.
void render_textura_rot(
    SDL_Texture *textura, SDL_Rect *rect, 
    uint16_t x, uint16_t y, 
    double rotacion, uint8_t flip
){

    SDL_Rect rect_local;

    rect_local.x = x;
    rect_local.y = y;
    rect_local.w = rect->w;
    rect_local.h = rect->h;

    SDL_RenderCopyEx(
        renderer, 
        textura,
        NULL,
        &rect_local,
        rotacion,
        NULL,
        flip
    );
}

void render_textura(
    SDL_Texture *textura, SDL_Rect *rect, 
    uint16_t x, uint16_t y, uint8_t flip
){
    render_textura_rot(textura, rect, x, y, 0, flip);
}

SDL_Texture *crear_textura_y_setear_render(uint16_t w, uint16_t h){
    SDL_Texture *textura = SDL_CreateTexture(
        renderer,
        SDL_PIXELFORMAT_RGBA8888,
        SDL_TEXTUREACCESS_TARGET,
        w,
        h
    );

    SDL_SetRenderTarget(renderer, textura);
    SDL_SetTextureBlendMode(textura, SDL_BLENDMODE_BLEND);

    return textura;
}


void desplazar_rect(SDL_Rect *rect, uint16_t x, uint16_t y){
    rect->x += x;
    rect->y += y;
}

void renderizar_de_sheet(
    uint16_t x, uint16_t y, 
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite,
    double rotacion, uint8_t flip
){
    SDL_Rect area_en_sheet = (SDL_Rect){
        .x = x_sprite,
        .y = y_sprite,
        .w = w_sprite,
        .h = h_sprite
    };

    SDL_Rect sprite_en_pantalla = (SDL_Rect){
        .x = x,
        .y = y,
        .w = w_sprite,
        .h = h_sprite
    };

    SDL_RenderCopyEx(
        renderer, spritesheet,
        &area_en_sheet,
        &sprite_en_pantalla,
        rotacion,
        NULL,
        flip
    );
}

void render_de_sheet_simple(uint16_t x, uint16_t y, 
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite
){
    renderizar_de_sheet(x, y, x_sprite, y_sprite, w_sprite, h_sprite, 0, SDL_FLIP_NONE);
}

void renderizar_espejo_sheet_ex(uint16_t x, uint16_t y, 
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite,
    uint8_t brecha, double rotacion,
    uint8_t flip
){
    renderizar_de_sheet(x, y, x_sprite, y_sprite, w_sprite, h_sprite, rotacion, SDL_FLIP_NONE | flip);
    renderizar_de_sheet(x+(w_sprite+brecha), y, x_sprite, y_sprite, w_sprite, h_sprite, rotacion, SDL_FLIP_HORIZONTAL | flip);
}

void renderizar_espejo_sheet_en_ancho(
    uint16_t x, uint16_t y,
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite,
    uint16_t ancho
){
    renderizar_espejo_sheet_ex(x, y, x_sprite, y_sprite, w_sprite, h_sprite, ancho - 2 * x - 2 * w_sprite, 0, SDL_FLIP_NONE);
}

void renderizar_espejo_sheet(
    uint16_t x, uint16_t y, 
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite
){
    renderizar_espejo_sheet_ex(x, y, x_sprite, y_sprite, w_sprite, h_sprite, 0, 0, SDL_FLIP_NONE);
}

void renderizar_espejo_espaciado_sheet_ex(
    uint16_t x, uint16_t y, 
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite,
    uint16_t x_sprite_intermedio, uint16_t y_sprite_intermedio,
    double rotacion, uint8_t flip
){
    renderizar_espejo_sheet_ex(x, y, x_sprite, y_sprite, w_sprite, h_sprite, 1, rotacion, flip);
    renderizar_de_sheet(x+w_sprite, y, x_sprite_intermedio, y_sprite_intermedio, 1, h_sprite, rotacion, flip);
}

void renderizar_espejo_espaciado_sheet(
    uint16_t x, uint16_t y, 
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite,
    uint16_t x_sprite_intermedio, uint16_t y_sprite_intermedio
){
    renderizar_espejo_espaciado_sheet_ex(x, y, x_sprite, y_sprite, w_sprite, h_sprite, 
    x_sprite_intermedio, y_sprite_intermedio, 0, SDL_FLIP_NONE);
}

// void renderizar_redondel_intermedio_sheet(
//     uint16_t x, uint16_t y, 
//     uint16_t x_sprite, uint16_t y_sprite,
//     uint16_t w_sprite, uint16_t h_sprite,
//     uint16_t x_sprite_intermedio, uint16_t y_sprite_intermedio
// ){
//     renderizar_espejo_espaciado_sheet(x, y, x_sprite, y_sprite, w_sprite, h_sprite, 
//     x_sprite_intermedio, y_sprite_intermedio);
//     renderizar_espejo_espaciado_sheet_ex(x, y+h_sprite, x_sprite, y_sprite, w_sprite, h_sprite, 
//     x_sprite_intermedio, y_sprite_intermedio, SDL_FLIP_VERTICAL);
// }

void renderizar_redondel_sheet(
    uint16_t x, uint16_t y, 
    uint16_t x_sprite, uint16_t y_sprite,
    uint16_t w_sprite, uint16_t h_sprite
){
    renderizar_espejo_sheet(x, y, x_sprite, y_sprite, w_sprite, h_sprite);
    renderizar_espejo_sheet_ex(x, y+h_sprite, x_sprite, y_sprite, w_sprite, h_sprite, 0, 
    0, SDL_FLIP_VERTICAL);
}

void render_valor_carta(uint8_t valor, uint16_t x, uint16_t y){
    uint8_t const numeros_y = 71;

    uint8_t const cero_x = 20;
    uint8_t const dos_x = 24;

    if(valor == 0) render_de_sheet_simple(x, y, cero_x, numeros_y, 3, 5);
    else if(valor == 1) render_de_sheet_simple(x, y, cero_x+3, numeros_y, 1, 5);
    else if(valor <= 9) render_de_sheet_simple(x, y, dos_x + (valor - 2) * 3, numeros_y, 3, 5);
    else{
        uint8_t mod = valor % 10;
        render_valor_carta(1, x, y);
        render_valor_carta(mod, x+2, y);
    }
}

void render_basto_de_sheet(uint16_t x, uint16_t y, double rotacion, uint8_t flip){
    renderizar_de_sheet(x, y, 0, 71, 9, 17, rotacion, flip);
}

void render_basto_diagonal_de_sheet(uint16_t x, uint16_t y, uint8_t flip){
    renderizar_de_sheet(x, y, 9, 71, 10, 17, 0, flip);
}

void render_seis_de_basto(){
    render_basto_de_sheet(1, 8, 0, SDL_FLIP_NONE);
    render_basto_de_sheet(10, 8, 0, SDL_FLIP_VERTICAL);
    render_basto_de_sheet(20, 8, 0, SDL_FLIP_HORIZONTAL);

    render_basto_de_sheet(1, 39, 0, SDL_FLIP_VERTICAL | SDL_FLIP_HORIZONTAL);
    render_basto_de_sheet(11, 39, 0, SDL_FLIP_NONE);
    render_basto_de_sheet(20, 39, 0, SDL_FLIP_VERTICAL);
}

typedef SDL_Texture *CreadorTextura();

SDL_Texture *crear_textura_setear_rect_y_resetear_render(
    SDL_Texture *textura_a_volver, CreadorTextura *func,
    SDL_Rect *rect
){

    SDL_Texture *textura = func();

    int w;
    int h;

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

    rect->w = w;
    rect->h = h;

    SDL_SetRenderTarget(renderer, textura_a_volver);

    return textura;
}

SDL_Texture *crear_textura_redondel(uint16_t pizza_x, uint16_t pizza_y, uint16_t pizza_w, uint16_t pizza_h){
    SDL_Texture *textura_redondel = crear_textura_y_setear_render(pizza_w * 2, pizza_h * 2);
    renderizar_redondel_sheet(0, 0, pizza_x, pizza_y, pizza_w, pizza_h);
    return textura_redondel;
}

SDL_Texture *crear_textura_espejo(uint16_t mitad_x, uint16_t mitad_y, uint16_t mitad_w, uint16_t mitad_h){
    SDL_Texture *textura_espejo = crear_textura_y_setear_render(mitad_w * 2, mitad_h);
    renderizar_espejo_sheet(0, 0, mitad_x, mitad_y, mitad_w, mitad_h);
    return textura_espejo;
}

SDL_Texture *crear_textura_espejo_espaciado(
    uint16_t mitad_x, uint16_t mitad_y, 
    uint16_t mitad_w, uint16_t mitad_h,
    uint16_t intermedio_x, uint16_t intermedio_y
){
    SDL_Texture *textura_espejo = crear_textura_y_setear_render((mitad_w * 2) + 1, mitad_h);
    renderizar_espejo_espaciado_sheet(0, 0, mitad_x, mitad_y, mitad_w, mitad_h,
    intermedio_x, intermedio_y);
    return textura_espejo;
}

#define ANCHO_ESPADON 10
#define ALTURA_ESPADON 54

SDL_Texture *crear_textura_espadon(){

    SDL_Texture *textura_espadon = crear_textura_y_setear_render(10, 54);

    SDL_Rect rect = (SDL_Rect){0};

    renderizar_espejo_sheet(rect.x, rect.y, 58, 52, 5, 21);
            
    desplazar_rect(&rect, 2, 20);

    for(uint8_t i = 0; i < 32; i++){
        render_de_sheet_simple(rect.x, rect.y, 60, 73, 6, 1);
        desplazar_rect(&rect, 0, 1);
    }

    desplazar_rect(&rect, 1, 0);
    render_de_sheet_simple(rect.x, rect.y, 61, 74, 4, 2);

    return textura_espadon;
}

SDL_Texture *crear_textura_espada_diagonal(){

    SDL_Texture *textura_espada_diagonal = crear_textura_y_setear_render(13, 13);

    SDL_Rect rect = (SDL_Rect){0};

    render_de_sheet_simple(rect.x, rect.y, 63, 52, 5, 5);
            
    desplazar_rect(&rect, 2, 2);

    for(uint8_t i = 0; i < 3; i++){
        render_de_sheet_simple(rect.x, rect.y, 63, 57, 3, 3);
        desplazar_rect(&rect, 1, 1);
    }

    render_de_sheet_simple(rect.x-2, rect.y+1, 63, 60, 4, 3);
    renderizar_de_sheet(rect.x, rect.y-2, 63, 60, 4, 3, 0, SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);

    desplazar_rect(&rect, 1, 1);

    for(uint8_t i = 0; i < 4; i++){
        render_de_sheet_simple(rect.x, rect.y, 63, 63, 3, 3);
        desplazar_rect(&rect, 1, 1);
    }

    render_de_sheet_simple(rect.x, rect.y, 63, 66, 3, 3);

    // desplazar_rect(&rect, 1, 0);
    // render_de_sheet_simple(rect.x, rect.y, 61, 74, 4, 2);

    return textura_espada_diagonal;
}

SDL_Texture *crear_textura_espada(){

    SDL_Texture *textura_espada = crear_textura_y_setear_render(9, 21);

    SDL_Rect rect = (SDL_Rect){0};

    renderizar_espejo_espaciado_sheet(rect.x, rect.y, 48, 63, 4, 10, 52, 63);
            
    desplazar_rect(&rect, 2, 9);

    for(uint8_t i = 0; i < 9; i++){
        renderizar_espejo_espaciado_sheet(rect.x, rect.y, 50, 73, 2, 1, 52, 73);
        desplazar_rect(&rect, 0, 1);
    }

    desplazar_rect(&rect, 1, 0);
    renderizar_espejo_espaciado_sheet(rect.x, rect.y, 51, 74, 1, 3, 52, 74);

    return textura_espada;
}

SDL_Texture *crear_textura_oro_grande(){
    return crear_textura_redondel(40, 46, 9, 9);
}

SDL_Texture *crear_textura_oro_mediano(){
    return crear_textura_redondel(49, 46, 6, 6);
}

SDL_Texture *crear_textura_copa_grande(){
    return crear_textura_espejo(40, 55, 8, 16);
}

SDL_Texture *crear_textura_copa_mediana(){
    return crear_textura_espejo_espaciado(49, 52, 5, 11, 54, 52);
}

SDL_Texture *crear_textura_y_render_seis_de_espada(
    SDL_Texture *textura_carta_dentro, SDL_Rect *rect
){
    SDL_Texture *textura_espada = crear_textura_setear_rect_y_resetear_render(
        textura_carta_dentro, 
        crear_textura_espada,
        rect
    );

    render_textura(textura_espada, rect, 1, 6, SDL_FLIP_NONE);
    render_textura(textura_espada, rect, 10, 6, SDL_FLIP_VERTICAL);
    render_textura(textura_espada, rect, 20, 6, SDL_FLIP_HORIZONTAL);

    render_textura(textura_espada, rect, 1, 35, SDL_FLIP_VERTICAL);
    render_textura(textura_espada, rect, 10, 35, SDL_FLIP_NONE);
    render_textura(textura_espada, rect, 20, 35, SDL_FLIP_VERTICAL | SDL_FLIP_HORIZONTAL);

    return textura_espada;
}

SDL_Texture *crear_textura_y_render_cuatro_de_oro(
    SDL_Texture *textura_carta_dentro, SDL_Rect *rect
){
    SDL_Texture *textura_oro_mediano = crear_textura_setear_rect_y_resetear_render(
        textura_carta_dentro, 
        crear_textura_oro_mediano,
        rect
    );

    render_textura(textura_oro_mediano, rect, 1, 7, SDL_FLIP_NONE);
    render_textura(textura_oro_mediano, rect, 17, 7, SDL_FLIP_NONE);
    render_textura(textura_oro_mediano, rect, 1, 44, SDL_FLIP_NONE);
    render_textura(textura_oro_mediano, rect, 17, 44, SDL_FLIP_NONE);

    return textura_oro_mediano;
}

SDL_Texture *crear_textura_y_render_cuatro_de_copa(
    SDL_Texture *textura_carta_dentro, SDL_Rect *rect
){
    SDL_Texture *textura_copa_mediana = crear_textura_setear_rect_y_resetear_render(
        textura_carta_dentro, 
        crear_textura_copa_mediana,
        rect
    );

    render_textura(textura_copa_mediana, rect, 2, 7, SDL_FLIP_NONE);
    render_textura(textura_copa_mediana, rect, 17, 7, SDL_FLIP_NONE);
    render_textura(textura_copa_mediana, rect, 2, 45, SDL_FLIP_NONE);
    render_textura(textura_copa_mediana, rect, 17, 45, SDL_FLIP_NONE);

    return textura_copa_mediana;
}


SDL_Texture *crear_textura_carta(uint8_t carta_i){

    SDL_Texture* textura_carta = crear_textura_y_setear_render(ANCHO_CARTA, LARGO_CARTA);

    SDL_Rect rect;

    const int marco_margen = 1;

    rect.x = 0;
    rect.y = 0;
    rect.w = ANCHO_CARTA;
    rect.h = LARGO_CARTA;

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
    SDL_RenderFillRect(renderer, &rect);

    rect.x = marco_margen;
    rect.y = marco_margen;
    rect.w = ANCHO_CARTA - marco_margen * 2;
    rect.h = LARGO_CARTA - marco_margen * 2;

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderDrawRect(renderer, &rect);

    SDL_Texture* textura_carta_dentro = crear_textura_y_setear_render(30, 57);

    carta_i = TRES_DE_BASTO;

    const uint8_t ancho_carta_dentro = 30;

    switch(carta_i){
        case UNO_DE_ESPADA: {
            render_de_sheet_simple(1, 6, 0, 0, 28, 46);
            break;
        }
        case UNO_DE_BASTO: {
            render_de_sheet_simple(1, 6, 28, 0, 29, 46);
            break;
        }
        case UNO_DE_COPA: {
            renderizar_espejo_sheet(1, 1, 57, 0, 14, 52);
            break;
        }
        case UNO_DE_ORO: {
            renderizar_espejo_sheet(1, 3, 71, 0, 14, 52);
            render_de_sheet_simple(9, 15, 0, 49, 12, 16);
            break;
        }
        case DOS_DE_ESPADA: {

            SDL_Texture *textura_espadon = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_espadon,
                &rect
            );

            render_textura(textura_espadon, &rect, 4, 2, SDL_FLIP_NONE);
            render_textura(textura_espadon, &rect, 16, 2, SDL_FLIP_VERTICAL);

            SDL_DestroyTexture(textura_espadon);
            break;
        }
        case TRES_DE_ESPADA: {

            SDL_Texture *textura_espadon = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_espadon,
                &rect
            );

            render_textura(textura_espadon, &rect, 5, 2, SDL_FLIP_NONE);
            render_textura(textura_espadon, &rect, 3 + ANCHO_ESPADON-1, 3, SDL_FLIP_VERTICAL);
            render_textura(textura_espadon, &rect, 3 + (ANCHO_ESPADON-1)*2, 3, SDL_FLIP_HORIZONTAL);

            SDL_DestroyTexture(textura_espadon);
            break;
        }
        case CUATRO_DE_ESPADA: {

            SDL_Texture *textura_espada_diagonal = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_espada_diagonal,
                &rect
            );

            render_textura(textura_espada_diagonal, &rect, 1, 9, SDL_FLIP_NONE);
            render_textura(textura_espada_diagonal, &rect, 16, 9, SDL_FLIP_HORIZONTAL);

            render_textura(textura_espada_diagonal, &rect, 1, 34, SDL_FLIP_VERTICAL);
            render_textura(textura_espada_diagonal, &rect, 16, 34, SDL_FLIP_VERTICAL | SDL_FLIP_HORIZONTAL);

            SDL_DestroyTexture(textura_espada_diagonal);
            break;
        }
        case CINCO_DE_ESPADA: {
            SDL_Texture *textura_espada = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_espada,
                &rect
            );
            
            render_textura(textura_espada, &rect, 1, 6, SDL_FLIP_NONE);
            render_textura(textura_espada, &rect, 20, 6, SDL_FLIP_HORIZONTAL);

            render_textura(textura_espada, &rect, 1, 35, SDL_FLIP_VERTICAL);
            render_textura(textura_espada, &rect, 20, 35, SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);

            render_textura_rot(textura_espada, &rect, 10, 21, 270, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_espada);
            break;
        }
        case SEIS_DE_ESPADA: {
            SDL_DestroyTexture(crear_textura_y_render_seis_de_espada(
                textura_carta_dentro,
                &rect
            ));
            break;
        }
        case SIETE_DE_ESPADA: {
            SDL_Texture *textura_espada = crear_textura_y_render_seis_de_espada(
                textura_carta_dentro,
                &rect
            );

            render_textura_rot(textura_espada, &rect, 10, 21, 270, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_espada);
            break;
        }
        case DOS_DE_BASTO: {

            const uint8_t x_sprite = 68;
            const uint8_t y_sprite = 52;
            const uint8_t w_sprite = 17;
            const uint8_t h_sprite = 48;

            renderizar_de_sheet(1, 4, x_sprite, y_sprite, w_sprite, h_sprite, 0, SDL_FLIP_NONE);
            renderizar_de_sheet(12, 4, x_sprite, y_sprite, w_sprite, h_sprite, 0, SDL_FLIP_VERTICAL | SDL_FLIP_HORIZONTAL);
            break;
        }
        case TRES_DE_BASTO: {

            renderizar_espejo_sheet_en_ancho(1, 6, 19, 76, 11, 29, ancho_carta_dentro);
            renderizar_espejo_sheet_en_ancho(3, 33, 30, 76, 9, 20, ancho_carta_dentro);

            renderizar_espejo_sheet(10, 6, 39, 76, 5, 17);
            renderizar_espejo_sheet(11, 32, 44, 76, 4, 21);

            render_de_sheet_simple(8, 18, 47, 77, 14, 18);
            break;
        }
        case CUATRO_DE_BASTO: {
            render_basto_diagonal_de_sheet(2, 8, SDL_FLIP_HORIZONTAL);
            render_basto_diagonal_de_sheet(18, 8, SDL_FLIP_NONE);

            render_basto_diagonal_de_sheet(2, 34, SDL_FLIP_VERTICAL | SDL_FLIP_HORIZONTAL);
            render_basto_diagonal_de_sheet(18, 34, SDL_FLIP_VERTICAL);
            break;
        }
        case CINCO_DE_BASTO: {
            render_basto_diagonal_de_sheet(2, 8, SDL_FLIP_VERTICAL | SDL_FLIP_HORIZONTAL);
            render_basto_diagonal_de_sheet(18, 8, SDL_FLIP_VERTICAL);

            render_basto_diagonal_de_sheet(2, 34, SDL_FLIP_HORIZONTAL);
            render_basto_diagonal_de_sheet(18, 34, SDL_FLIP_NONE);

            render_basto_de_sheet(10, 21, 270, SDL_FLIP_NONE);
            break;
        }
        case SEIS_DE_BASTO: {
            render_seis_de_basto();
            break;
        }
        case SIETE_DE_BASTO: {
            render_seis_de_basto();
            render_basto_de_sheet(10, 23, 270, SDL_FLIP_NONE);
            break;
        }
        case DOS_DE_COPA: {
            SDL_Texture *textura_copa = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_copa_grande,
                &rect
            );

            render_textura(textura_copa, &rect, 7, 2, SDL_FLIP_NONE);
            render_textura(textura_copa, &rect, 7, 39, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_copa);
            break;
        }
        case TRES_DE_COPA: {
            SDL_Texture *textura_copa = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_copa_grande,
                &rect
            );

            render_textura(textura_copa, &rect, 5, 6, SDL_FLIP_NONE);
            render_textura(textura_copa, &rect, 5, 40, SDL_FLIP_NONE);
            render_textura(textura_copa, &rect, 13, 23, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_copa);
            break;
        }
        case CUATRO_DE_COPA: {

            SDL_Texture *textura_copa_mediana = crear_textura_y_render_cuatro_de_copa(
                textura_carta_dentro, 
                &rect
            );

            SDL_DestroyTexture(textura_copa_mediana);
            break;
        }
        case CINCO_DE_COPA: {
            SDL_Texture *textura_copa_mediana = crear_textura_y_render_cuatro_de_copa(
                textura_carta_dentro, 
                &rect
            );

            SDL_DestroyTexture(textura_copa_mediana);

            SDL_Texture *textura_copa_grande = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_copa_grande,
                &rect
            );

            render_textura(textura_copa_grande, &rect, 7, 23, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_copa_grande);
            break;
        }
        case SEIS_DE_COPA: {
            SDL_Texture *textura_copa_mediana = crear_textura_y_render_cuatro_de_copa(
                textura_carta_dentro, 
                &rect
            );

            render_textura(textura_copa_mediana, &rect, 2, 26, SDL_FLIP_NONE);
            render_textura(textura_copa_mediana, &rect, 17, 26, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_copa_mediana);
            break;
        }
        case SIETE_DE_COPA: {
            SDL_Texture *textura_copa_mediana = crear_textura_y_render_cuatro_de_copa(
                textura_carta_dentro, 
                &rect
            );

            render_textura(textura_copa_mediana, &rect, 2, 19, SDL_FLIP_NONE);
            render_textura(textura_copa_mediana, &rect, 17, 19, SDL_FLIP_NONE);
            render_textura(textura_copa_mediana, &rect, 9, 31, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_copa_mediana);
            break;
        }
        case DOS_DE_ORO: {

            SDL_Texture *textura_oro_grande = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_oro_grande,
                &rect
            );

            render_textura(textura_oro_grande, &rect, 6, 7, SDL_FLIP_NONE);
            render_textura(textura_oro_grande, &rect, 6, 32, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_oro_grande);
            break;
        }
        case TRES_DE_ORO: {

            SDL_Texture *textura_oro_grande = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_oro_grande,
                &rect
            );

            render_textura(textura_oro_grande, &rect, 1, 4, SDL_FLIP_NONE);
            render_textura(textura_oro_grande, &rect, 11, 21, SDL_FLIP_NONE);
            render_textura(textura_oro_grande, &rect, 1, 38, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_oro_grande);
            break;
        }
        case CUATRO_DE_ORO: {

            SDL_Texture *textura_oro_mediano = crear_textura_y_render_cuatro_de_oro(
                textura_carta_dentro, 
                &rect
            );

            SDL_DestroyTexture(textura_oro_mediano);
            break;
        }
        case CINCO_DE_ORO: {
            SDL_Texture *textura_oro_mediano = crear_textura_y_render_cuatro_de_oro(
                textura_carta_dentro, 
                &rect
            );

            SDL_DestroyTexture(textura_oro_mediano);

            SDL_Texture *textura_oro_grande = crear_textura_setear_rect_y_resetear_render(
                textura_carta_dentro, 
                crear_textura_oro_grande,
                &rect
            );

            render_textura(textura_oro_grande, &rect, 6, 22, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_oro_grande);
            break;
        }
        case SEIS_DE_ORO: {
            SDL_Texture *textura_oro_mediano = crear_textura_y_render_cuatro_de_oro(
                textura_carta_dentro, 
                &rect
            );

            render_textura(textura_oro_mediano, &rect, 1, 25, SDL_FLIP_NONE);
            render_textura(textura_oro_mediano, &rect, 17, 25, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_oro_mediano);
            break;
        }
        case SIETE_DE_ORO: {
            SDL_Texture *textura_oro_mediano = crear_textura_y_render_cuatro_de_oro(
                textura_carta_dentro, 
                &rect
            );

            render_textura(textura_oro_mediano, &rect, 1, 20, SDL_FLIP_NONE);
            render_textura(textura_oro_mediano, &rect, 17, 20, SDL_FLIP_NONE);

            render_textura(textura_oro_mediano, &rect, 9, 32, SDL_FLIP_NONE);

            SDL_DestroyTexture(textura_oro_mediano);
            break;
        }
        default: {break;}
    }

    //pongo los numeros
    uint8_t valor_carta = cartas[carta_i].valor_carta;
    render_valor_carta(valor_carta, 1, 1);
    SDL_SetRenderTarget(renderer, textura_carta);

    //el rect se ubica dentro del marco de la carta
    rect.x = 2;
    rect.y = 2;
    rect.w = ancho_carta_dentro;
    rect.h = 57;

    render_textura(textura_carta_dentro, &rect, rect.x, rect.y, SDL_FLIP_NONE);

    SDL_DestroyTexture(textura_carta_dentro);

    return textura_carta;
}

SDL_Rect render_carta(uint16_t x, uint16_t y, uint8_t carta_i){

    SDL_Rect rect;

    rect.x = x;
    rect.y = y;
    rect.w = ANCHO_CARTA;
    rect.h = LARGO_CARTA;

    render_textura(texturas_cartas[carta_i], &rect, x, y, SDL_FLIP_NONE);
    
    return rect;
}

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(
        "Truco C (SDL)",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        ANCHO_PANTALLA * ESCALA, ALTURA_PANTALLA * ESCALA,
        0
    );

    renderer = SDL_CreateRenderer(ventana, -1, 0);
    SDL_RenderSetLogicalSize(renderer, ANCHO_PANTALLA, ALTURA_PANTALLA);


    spritesheet = cargar_textura_y_avisar_error("imagenes/spsheet.png");

    for(uint8_t i = 0; i < 40; i++){
        texturas_cartas[i] = crear_textura_carta(i);
    }

    SDL_DestroyTexture(spritesheet);


    SDL_SetRenderTarget(renderer, NULL);

    InteractuableMouse interactuables_mouse[MAX_INTERACTUABLES_MOUSE];
    uint8_t interactuables_mouse_i = 0;

    EstadoPartida partida;

    partida.numero_jugadores = 2;
    partida.max_puntos = 30;
    SET_BIT(partida.flags_partida, FLAG_PARTIDA_FLOR);

    iniciar_partida(&partida);

    bool seguir = true;

    while(seguir){

        SDL_Event evento;

        uint8_t jugador_turno_actual = obtener_jugador_turno_actual(&partida);

        while(SDL_PollEvent(&evento)){
            switch(evento.type){
                case SDL_MOUSEBUTTONUP: {
                    for(uint8_t i = 0; i < interactuables_mouse_i; i++){
                        InteractuableMouse *interactuable = &interactuables_mouse[i];

                        if(!(evento.button.x >= interactuable->rect.x &&
                        evento.button.x <= interactuable->rect.x + interactuable->rect.w &&
                        evento.button.y >= interactuable->rect.y  &&
                        evento.button.y <= interactuable->rect.y + interactuable->rect.h)) continue;

                        for(uint8_t i = 0; i < MAX_CARTAS_MANO; i++){
                            uint8_t carta_i = partida.jugadores_cartas[jugador_turno_actual][i];

                            if(carta_i != interactuable->valor) continue;

                            TCIC_jugar_baza(&partida, jugador_turno_actual, i);
                            break;
                        }
                    }
                    break;
                }
                case SDL_QUIT: {
                    seguir = false;
                    break;
                }
                default: break;
            }
        }

        SDL_SetRenderDrawColor(renderer, 0, 128, 0, SDL_ALPHA_OPAQUE);

        SDL_RenderClear(renderer);

        for(uint8_t i = 0; i < MAX_INTERACTUABLES_MOUSE; i++){
            interactuables_mouse[i] = (InteractuableMouse){
                .rect = (SDL_Rect){
                    .x = ANCHO_PANTALLA + 1,
                    .y = ALTURA_PANTALLA + 1,
                    .w = 0,
                    .h = 0,
                },
                .valor = CARTA_INVALIDA,
            };
        }

        interactuables_mouse_i = 0;

        uint8_t cartas_en_orden[MAX_CARTAS_MANO];
        uint8_t cartas_en_mano = 0;

        for(uint8_t i = 0; i < MAX_CARTAS_MANO; i++){
            uint8_t carta_i = partida.jugadores_cartas[jugador_turno_actual][i];
            if(carta_i != CARTA_INVALIDA){
                cartas_en_orden[cartas_en_mano++] = carta_i;
            }
        }


        for(uint8_t i = 0; i < cartas_en_mano; i++){

            uint8_t carta_i = cartas_en_orden[i];

            const int x = (ANCHO_PANTALLA - MARGEN_ANCHO_CARTA * cartas_en_mano) / 2 + i * MARGEN_ANCHO_CARTA;
            const int y = (ALTURA_PANTALLA - LARGO_CARTA - 15);

            interactuables_mouse[interactuables_mouse_i++] = (InteractuableMouse){
                .rect = render_carta(x, y, carta_i),
                .valor = carta_i,
            };
        }

        for(uint8_t i = 0; i <= partida.baza_actual; i++){
            
            for(uint8_t j = 0; j < partida.numero_jugadores; j++){

                uint8_t carta_i = partida.cartas_bazas[i][j];

                if(carta_i == CARTA_INVALIDA) continue;

                const int x = ((ANCHO_PANTALLA - MARGEN_ANCHO_CARTA) / 3) + (i * MARGEN_ANCHO_CARTA);
                const int y = (10 * partida.numero_jugadores) / 3 + j * MARGEN_LARGO_CARTA;

                render_carta(x, y, carta_i);
            }

        }

        SDL_RenderPresent(renderer);
        SDL_Delay(50);
    }


    return 0;
}

ResultadoJugada TCIC_jugar_baza(EstadoPartida *partida, uint8_t jugador_i, uint8_t carta_i){
    return jugar_baza(partida, jugador_i, carta_i);
};

ResultadoJugada TCIC_cantar(EstadoPartida *partida, uint8_t jugador_i, uint8_t canto){};

ResultadoJugada TCIC_cantar_tanto(EstadoPartida *partida, uint8_t jugador_i){};