#include "scoreboard.h"
#include <fstream>
#include <sstream>

// Constructor
ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board)
{
    // Obten punteros a objetos
    this->asset = asset;
    this->renderer = renderer;
    this->board = board;

    // Reserva memoria para los objetos
    texture = new LTexture(renderer, asset->get("player.png"));
    sprite = new Sprite({0, 0, 0, 0}, texture, renderer);
    text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer);

    // Crea la textura para dibujar el marcador
    layer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT);
    if (layer == nullptr)
        printf("Error: map_layer0 could not be created!\nSDL Error: %s\n", SDL_GetError());
    SDL_SetTextureBlendMode(layer, SDL_BLENDMODE_BLEND);

    // Inicializa las variables
    counter = 0;
    state = sb_hide;
}

// Destructor
ScoreBoard::~ScoreBoard()
{
    SDL_DestroyTexture(layer);

    delete texture;
    delete sprite;
    delete text;
}

// Dibuja el marcador en la textura
void ScoreBoard::fillTexture()
{
    // Cambia el puntero del renderizador a la textura y la limpia
    SDL_SetRenderTarget(renderer, layer);
    SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
    SDL_RenderClear(renderer);

    // Pinta el degradado
    const float num_lines = (SCOREBOARD_BOTTOM / 2) - SCOREBOARD_TOP;

    for (int i = SCOREBOARD_TOP; i < SCOREBOARD_TOP + num_lines; i++)
    {
        float step = ((float)i / num_lines);
        int alpha = 64 + ((0 - 64) * step);
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, alpha);
        SDL_RenderDrawLine(renderer, SCOREBOARD_LEFT, i, SCOREBOARD_RIGHT, i);
    }

    // Escribe los textos
    text->writeDX(TXT_CENTER | TXT_SHADOW, PLAY_AREA_CENTER_FIRST_QUARTER_X, 8, "-LIVES-");
    text->writeDX(TXT_CENTER | TXT_SHADOW, PLAY_AREA_CENTER_FIRST_QUARTER_X, 8 + text->getCharacterSize(), std::to_string(board->lives));
    text->writeDX(TXT_CENTER | TXT_SHADOW, PLAY_AREA_CENTER_THIRD_QUARTER_X, 8, "-DIAMONDS-");
    text->writeDX(TXT_CENTER | TXT_SHADOW, PLAY_AREA_CENTER_THIRD_QUARTER_X, 8 + text->getCharacterSize(), std::to_string(board->diamonds));

    // Vuelve a colocar el renderizador apuntando a la pantalla
    SDL_SetRenderTarget(renderer, nullptr);
}

// Pinta el objeto en pantalla
void ScoreBoard::render()
{
    if (state == sb_hide)
    {
        return;
    }
    // Dibuja la textura con el marcador en pantalla
    SDL_Rect rect = {SCOREBOARD_X, SCOREBOARD_Y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT};
    SDL_RenderCopy(renderer, layer, NULL, &rect);
}

// Actualiza las variables del objeto
void ScoreBoard::update()
{
    if (counter == 200)
    {
        state = sb_show;
    }
    else
    {
        counter++;
    }
}

// Recarga la textura
void ScoreBoard::reLoadTexture()
{
    texture->reLoad();
    text->reLoadTexture();
}

// Resetea el tiempo de aparición del marcador
void ScoreBoard::reset()
{
    counter = 0;
    state = sb_hide;
}