import matplotlib.pyplot as plt
import csv

H = []
V = []
actions = []

with open('output.txt', 'r', encoding='utf-8') as file:
    reader = csv.reader(file)
    path_section = False
    for row in reader:
        if not row:
            continue
        if row[0].strip() == 'H_start':
            path_section = True
            continue
        if path_section:
            if len(row) < 5:
                continue
            try:
                H_start, V_start, H_end, V_end, Action = row
                H.append(float(H_start))
                V.append(float(V_start))
                actions.append(Action)
            except ValueError:
                continue

plt.figure(figsize=(12, 8))

unique_H = sorted(list(set(H)))
unique_V = sorted(list(set(V)))

if len(unique_H) > 1:
    Hel = unique_H[1] - unique_H[0]
else:
    Hel = 1  
if len(unique_V) > 1:
    Vel = unique_V[1] - unique_V[0]
else:
    Vel = 1 

plt.xlim(min(H) - Hel, max(H) + Hel)
plt.ylim(min(V) - Vel, max(V) + Vel)

plt.xticks(unique_H)
plt.yticks(unique_V)
plt.grid(True, which='both', linestyle='--', linewidth=0.5)

action_styles = {
    "Разгон": {'color': 'blue', 'linestyle': '-'},
    "Подъем": {'color': 'green', 'linestyle': '--'},
    "Разгон-Подъем": {'color': 'red', 'linestyle': '-.'}
}

# print(actions)
for i in range(len(H) - 1):
    x = [H[i], H[i+1]]
    y = [V[i], V[i+1]]
    action = actions[i]
    style = action_styles.get(action, {'color': 'gray', 'linestyle': ':'})
    plt.plot(x, y, color=style['color'], linestyle=style['linestyle'], marker='o', label=action)

handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())

plt.title('Оптимальный Маршрут')
plt.xlabel('Высота (м)')
plt.ylabel('Скорость (км/ч)')

plt.show()
