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

// Constructor
Item::Item(item_t item)
{
    const int itemSize = 8;

    // Obten punteros a objetos
    asset = item.asset;
    renderer = item.renderer;

    // Crea objetos
    texture = new LTexture(renderer, asset->get(item.tileset));
    sprite = new Sprite(item.x, item.y, itemSize, itemSize, texture, renderer);

    // Inicia variables
    sprite->setSpriteClip((item.tile % 10) * itemSize, (item.tile / 10) * itemSize, itemSize, itemSize);
    collider = sprite->getRect();
    colorChangeSpeed = 4;
    counter = item.counter * colorChangeSpeed;

    // Inicializa los colores
    color_t c = item.color1;
    color.push_back(c);
    color.push_back(c);

    c = item.color2;
    color.push_back(c);
    color.push_back(c);

    /*
    const std::vector<std::string> vColors = {"blue", "red", "magenta", "green", "cyan", "yellow"};
    for (auto v:vColors)
    {
        color.push_back(stringToColor(v));
    }
    */
}

// Destructor
Item::~Item()
{
    delete texture;
    delete sprite;
}

// Pinta el objeto en pantalla
void Item::render()
{
    const int index = (counter / colorChangeSpeed) % color.size();
    sprite->getTexture()->setColor(color[index].r, color[index].g, color[index].b);
    sprite->render();
    sprite->getTexture()->setColor(255, 255, 255);
}

// Actualiza las variables del objeto
void Item::update()
{
    ++counter;
}

// Obtiene el rectangulo de colision del objeto
SDL_Rect &Item::getCollider()
{
    return collider;
}

// Obtiene su ubicación
SDL_Point Item::getPos()
{
    const SDL_Point p = {sprite->getPosX(), sprite->getPosY()};
    return p;
}

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