#include <vector>
#include "brick_grid.hpp"

BrickGrid::BrickGrid() {}
BrickGrid::BrickGrid(sf::FloatRect borders, int numBrickColumns, int numBrickRows) : 
    m_border(borders), 
    m_numBrickColumns(numBrickColumns), 
    m_numBrickRows(numBrickRows),
    m_numActiveBricks(numBrickColumns * numBrickRows)
{
    m_bricks.resize(m_numBrickColumns * m_numBrickRows, Brick{true});
    m_brickShape.setSize(getBrickSizes());
    m_brickShape.setOutlineColor(sf::Color::Black);
    m_brickShape.setOutlineThickness(0.5);
    m_brickShape.setFillColor(color);
}

sf::FloatRect BrickGrid::getBorder() const 
{
    return m_border;
}

sf::Vector2i BrickGrid::getGridSizes() const 
{
    return {m_numBrickColumns, m_numBrickRows};
}

sf::Vector2f BrickGrid::getBrickSizes() const 
{
    return {m_border.width / m_numBrickColumns, m_border.height / m_numBrickRows};
}

bool BrickGrid::isBrickActive(std::pair<int, int> indexes) const
{
    return m_bricks[indexes.first + indexes.second * m_numBrickColumns].isActive;
}

void BrickGrid::deactivateBrick(std::pair<int, int> indexes)
{
    m_bricks[indexes.first + indexes.second * m_numBrickColumns].isActive = false;
    m_numActiveBricks--;
}

int BrickGrid::getNumActiveBricks() const
{
    return m_numActiveBricks;
}

void BrickGrid::draw(sf::RenderWindow& window)
{
    auto [brickWidth, brickHeight] = getBrickSizes();

    for (int j = 0; j < m_numBrickRows; ++j) 
    {
        for (int i = 0; i < m_numBrickColumns; ++i) 
        {
            if (!isBrickActive({i, j}))
                continue;
            m_brickShape.setPosition({m_border.left + i * brickWidth, m_border.top + j * brickHeight});
            window.draw(m_brickShape);
        }
    }
}
