import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv("results.csv")

maze_order = ["small_10", "medium_50", "large_100", "empty", "no_path"]
strategy_order = ["BFS", "DFS", "AStar"]

maze_labels = {
    "small_10": "10×10",
    "medium_50": "50×50",
    "large_100": "100×100",
    "empty": "Пустой",
    "no_path": "Без выхода"
}

df["maze"] = pd.Categorical(df["maze"], categories=maze_order, ordered=True)
df["strategy"] = pd.Categorical(df["strategy"], categories=strategy_order, ordered=True)
df = df.sort_values(["maze", "strategy"])


def plot_grouped_bar(df, value_col, ylabel, title, filename):
    mazes = maze_order
    strategies = strategy_order

    x = np.arange(len(mazes))
    width = 0.25

    plt.figure(figsize=(11, 6))

    for i, strategy in enumerate(strategies):
        values = []

        for maze in mazes:
            row = df[(df["maze"] == maze) & (df["strategy"] == strategy)]
            values.append(row[value_col].values[0])

        plt.bar(x + (i - 1) * width, values, width, label=strategy)

    plt.xlabel("Лабиринт")
    plt.ylabel(ylabel)
    plt.title(title)

    plt.xticks(x, [maze_labels[m] for m in mazes], rotation=20)
    plt.legend(title="Стратегия")
    plt.grid(axis="y", alpha=0.3)

    plt.tight_layout()
    plt.savefig(filename, format="svg")
    plt.show()


plot_grouped_bar(
    df,
    value_col="time_ms",
    ylabel="Время, мс",
    title="Сравнение времени выполнения BFS, DFS и A*",
    filename="time_comparison.svg"
)

plot_grouped_bar(
    df,
    value_col="cells_visited",
    ylabel="Количество посещённых клеток",
    title="Сравнение количества посещённых клеток",
    filename="visited_cells_comparison.svg"
)

plot_grouped_bar(
    df,
    value_col="way_len",
    ylabel="Длина пути, клеток",
    title="Сравнение длины найденного пути",
    filename="path_length_comparison.svg"
)