from helpers.either import Either, Left, Right
from models.adjacency_matrix_model import (
    AdjacencyMatrixModel,
)
from models.error_string_model import ErrorStringModel
from repository.freecad_repository import (
    FreeCadRepository,
)
from usecases.get_first_detail_use_case import (
    GetFirstDetailUseCase,
)


class CheckObjectHasTouchesUseCase:
    """Check touches between objects and returns matrix

    Returns:
        dict: adjacency matrix
    """

    freeCadRepository = FreeCadRepository()

    def call(self, solidBodyPadding: float) -> Either:
        try:
            errorResult = []
            matrix = {}
            for part in self.freeCadRepository.getAllSolids():
                matrix[part.Label] = []
                touches = FreeCadRepository().objectHasTouches(
                    part=part, solidBodyPadding=solidBodyPadding
                )
                matrix[part.Label].extend(touches)
            if errorResult.__len__() == 0:
                return Left(
                    AdjacencyMatrixModel(
                        all_parts=self.freeCadRepository.getAllLabelsSolids(),
                        first_detail=GetFirstDetailUseCase().call(),
                        matrix=matrix,
                    )
                )
            else:
                return Right(
                    ErrorStringModel(
                        error="Solids bodies have no recounts: " + ",".join(errorResult)
                    )
                )
        except Exception as error:
            print(error)
            print("CheckObjectHasTouchesUseCase error")
            return Right(ErrorStringModel(error="CheckObjectHasTouchesUseCase error"))
