from typing import Tuple
import random

from entity.Piece import Piece
from util.ConfigurationManager import ConfigurationManager

"""
    TODO Add link that goes through random piece generation
"""
class PieceGenerator:
    
    __bucket = []

    @classmethod
    def get_piece(cls, position: Tuple) -> Piece:
        if len(cls.__bucket) == 0:
            cls.__generate_bucket()

        base_color, inner_border_color, border_color = cls.__get_piece_color()
        return Piece(cls.__get_piece_shape(cls.__bucket.pop()), position, base_color, inner_border_color, border_color)

    @classmethod
    def __generate_bucket(cls) -> None:
        piece_types = list(range(7))
        while len(cls.__bucket) != 7:
            random_number = random.randint(0, 6 - len(cls.__bucket))
            cls.__bucket.append(piece_types.pop(random_number))

    def __get_piece_shape(piece_number: int) -> Tuple:
        if piece_number == 0:
            return Piece.I_SHAPE
        if piece_number == 1:
            return Piece.J_SHAPE
        if piece_number == 2:
            return Piece.L_SHAPE
        if piece_number == 3:
            return Piece.O_SHAPE
        if piece_number == 4:
            return Piece.S_SHAPE
        if piece_number == 5:
            return Piece.T_SHAPE
        if piece_number == 6:
            return Piece.Z_SHAPE

        return None

    def __get_piece_color() -> Tuple:
        random_number = random.randint(1, 3)

        base_color = ConfigurationManager.configuration["color"]["piece-" + str(random_number)]
        inner_border_color = None if random_number != 3 else ConfigurationManager.configuration["color"]["piece-inner-border-1"]
        border_color = ConfigurationManager.configuration["color"]["piece-border-1"]

        return (base_color, inner_border_color, border_color)
