import matplotlib.pyplot as plt


def get_points_csv(column: int) -> list[int]:

    with open(file="points.csv", mode="r") as points:
        res: list[int] = []

        for line in points:
            row = line.split()
            try:
                res.append(row[column - 1])
            except Exception as e:
                print("something went wrong")
                raise e

        return res[::-1]


def get_plot(
    columns: list[int], label: str, xlabel: str, ylabel: str, filename: str, title: str
) -> None:
    x = get_points_csv(column=columns[0])
    y = get_points_csv(column=columns[1])

    print(f"x: {x}")
    print(f"y: {y}")

    plt.plot(x, y, label=label, marker="o", markersize=6)

    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.xticks(rotation=45, fontsize=8)
    plt.yticks(fontsize=8)

    plt.title(title)
    plt.legend()

    plt.savefig(f"../assets/{filename}.png", bbox_inches="tight", dpi=300)
    plt.clf()


def main() -> None:
    get_plot(
        columns=[4, 7],
        label="B(H)",
        xlabel="H, А/м",
        ylabel="B, Тл",
        filename="b(h)",
        title="Кривая начального намагничивания",
    )

    get_plot(
        columns=[4, 8],
        label="mu(H)",
        xlabel="H, А/м",
        ylabel="mu",
        filename="mu(h)",
        title="Зависимость магнитной проницаемости от напряженности магнитного поля",
    )


if __name__ == "__main__":
    main()
