#include "font.h"
#include "font_table.h"
#include "application.h"
#include "resources.h"
#include "utils.h"

void Font_DrawNumber(SDL_Renderer *renderer, const int x, const int y, const int number, font_type_t type)
{
    SDL_Rect frame = font_frame_table[0];
    SDL_Rect draw  = font_draw_table[0];

    char* string;
    char  tmp;
    int   i;
    int   index = 0;
    int   value = number;
    int   posx  = x;

    string = (char *) malloc(10 * sizeof(char));

    while (value > 9)
    {
        string[index++] = (value % 10) + '0';
        value           = value / 10;
    }

    string[index++] = value + '0';
    string[index]   = '\0';

    for (i = 0; i < (index / 2); i++)
    {
        tmp                   = string[i];
        string[i]             = string[index - 1 - i];
        string[index - 1 - i] = tmp;
    }

    index = 0;

    while (string[index] != '\0')
    {
        switch(string[index])
        {
            case '0' : frame = font_frame_table[0]; draw = font_draw_table[0]; break;
            case '1' : frame = font_frame_table[1]; draw = font_draw_table[1]; break;
            case '2' : frame = font_frame_table[2]; draw = font_draw_table[2]; break;
            case '3' : frame = font_frame_table[3]; draw = font_draw_table[3]; break;
            case '4' : frame = font_frame_table[4]; draw = font_draw_table[4]; break;
            case '5' : frame = font_frame_table[5]; draw = font_draw_table[5]; break;
            case '6' : frame = font_frame_table[6]; draw = font_draw_table[6]; break;
            case '7' : frame = font_frame_table[7]; draw = font_draw_table[7]; break;
            case '8' : frame = font_frame_table[8]; draw = font_draw_table[8]; break;
            case '9' : frame = font_frame_table[9]; draw = font_draw_table[9]; break;
        }

        draw.x = posx;
        draw.y = y;

        switch(type)
        {
            case FNT_DARK  : SDL_RenderCopy(renderer, font_darkTex, &frame, &draw);  break;
            case FNT_LIGHT : SDL_RenderCopy(renderer, font_lightTex, &frame, &draw); break;
        }

        posx += (draw.w + 3);

        index++;
    }

    free(string);
}
