import csv
import matplotlib.pyplot as plt


class ChartBuilder:

    def __init__(
        self,
        csv_file
    ):
        self.csv_file = csv_file

    def _read(self):

        rows = []

        with open(
            self.csv_file,
            "r",
            encoding="utf-8"
        ) as file:

            reader = csv.DictReader(file)

            for row in reader:
                rows.append(row)

        return rows

    def buildTimeChart(self):

        rows = self._read()

        labels = []
        values = []

        for row in rows:

            labels.append(
                f"{row['maze']}\n"
                f"{row['strategy']}"
            )

            values.append(
                float(
                    row["time_ms"]
                )
            )

        plt.figure()

        plt.bar(
            labels,
            values
        )

        plt.title(
            "Search Time"
        )

        plt.ylabel(
            "Milliseconds"
        )

        plt.xticks(
            rotation=45
        )

        plt.tight_layout()

        plt.show()

    def buildVisitedChart(self):

        rows = self._read()

        labels = []
        values = []

        for row in rows:

            labels.append(
                f"{row['maze']}\n"
                f"{row['strategy']}"
            )

            values.append(
                float(
                    row[
                        "visited_cells"
                    ]
                )
            )

        plt.figure()

        plt.bar(
            labels,
            values
        )

        plt.title(
            "Visited Cells"
        )

        plt.ylabel(
            "Cells"
        )

        plt.xticks(
            rotation=45
        )

        plt.tight_layout()

        plt.show()

    def buildPathChart(self):

        rows = self._read()

        labels = []
        values = []

        for row in rows:

            labels.append(
                f"{row['maze']}\n"
                f"{row['strategy']}"
            )

            values.append(
                float(
                    row[
                        "path_length"
                    ]
                )
            )

        plt.figure()

        plt.bar(
            labels,
            values
        )

        plt.title(
            "Path Length"
        )

        plt.ylabel(
            "Cells"
        )

        plt.xticks(
            rotation=45
        )

        plt.tight_layout()

        plt.show()