#include "Piece.h"

#include "Polyomino.h"
#include "Rotation.h"
#include "Block.h"
#include "Color.h"

#include <set>
#include <string>


Piece::Piece(Polyomino&& polyomino, Block blockType) :
    polyomino(polyomino),
    blockType(blockType),
    rotationState(NONE) {
}

void Piece::rotate(Rotation rotation) {
    if (rotation == CLOCKWISE)
        this->polyomino.rotateCW();
    if (rotation == DOUBLE)
        this->polyomino.rotate180();
    if (rotation == COUNTERCLOCKWISE)
        this->polyomino.rotateCCW();
    
    this->rotationState += rotation;
}

void Piece::defaultRotation() {
    if (this->rotationState == CLOCKWISE)
        this->polyomino.rotateCCW();
    if (this->rotationState == DOUBLE)
        this->polyomino.rotate180();
    if (this->rotationState == COUNTERCLOCKWISE)
        this->polyomino.rotateCW();

    this->rotationState = NONE;
}

const Polyomino& Piece::getPositions() const {
    return this->polyomino;
}

int Piece::getLength() const {
    return this->polyomino.getLength();
}

Block Piece::getBlockType() const {
    return this->blockType;
}

std::ostream& operator<<(std::ostream& os, const Piece& piece) {
    os << getConsoleColorCode(piece.blockType) << piece.polyomino << getResetConsoleColorCode();
    return os;
}

bool Piece::containsSquare(const Position& position) const {
    return polyomino.contains(position);
}