from abc import ABC, abstractmethod
from Maze import Maze, Cell


class MazeBuilder(ABC):
    @abstractmethod
    def buildFromFile(self, filename):
        pass


class TextFileMazeBuilder(MazeBuilder):
    def __init__(self):
        self._maze = None

    @property
    def maze(self):
        return self._maze

    def buildFromFile(self, filename: str):

        with open(filename, mode='r', encoding='utf-8') as file:
            lines = file.read().splitlines()

        height = len(lines)
        width = len(lines[0])
        self._maze = Maze(height, width)

        for y, line in enumerate(lines):
            for x, char in enumerate(line):
                cell = self._maze.getCell(x, y)

                if char == '#':
                    cell.isWall = True
                elif char == 'S':
                    cell.isStart = True
                    self._maze.start = cell
                elif char == 'E':
                    cell.isExit = True
                    self._maze.exit = cell
        self._validate()
        return self._maze

    def _validate(self):
        if self._maze.start is None:
            raise "в лабиринте нет старта"
        if self._maze.exit is None:
            raise "в лабиринте нет начала"
