import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

df500 = pd.read_csv("results/aaverage_timedata_500.csv") 
df1000 = pd.read_csv("results/aaverage_timedata_1000.csv") 
df2000 = pd.read_csv("results/aaverage_timedata_2000.csv") 
df5000 = pd.read_csv("results/aaverage_timedata_5000.csv") 
df10000 = pd.read_csv("results/aaverage_timedata_10000.csv") 


def select_data_list(ax):
    dfs = [df500, df1000, df2000, df5000, df10000]
    Nvals = [500, 1000, 2000, 5000, 10000]
    # delete, find, insert
    # список:
    valsSort = [list(arr[(arr['Структура'] == "linklist") & (arr['Режим'] == "sorted")]["Время (сек)"]) for arr in dfs]
    valsShuff = [list(arr[(arr['Структура'] == "linklist") & (arr['Режим'] == "shuffled")]["Время (сек)"]) for arr in dfs]
    # 0 - sorted 1 - shuffled
    # delete
    ax[0].plot(Nvals, [row[0] for row in valsSort], label="delete", color='red')
    ax[1].plot(Nvals, [row[0] for row in valsShuff], color='red')
    # find
    ax[0].plot(Nvals, [row[1] for row in valsSort], label="find", color='blue')
    ax[1].plot(Nvals, [row[1] for row in valsShuff], color='blue')
    # insert
    ax[0].plot(Nvals, [row[2] for row in valsSort], label="insert", color='green')
    ax[1].plot(Nvals, [row[2] for row in valsShuff], color='green')
    

def select_data_hasht(ax):
    dfs = [df500, df1000, df2000, df5000, df10000]
    Nvals = [500, 1000, 2000, 5000, 10000]
    # delete, find, insert
    # список:
    valsSort = [list(arr[(arr['Структура'] == "hashtable") & (arr['Режим'] == "sorted")]["Время (сек)"]) for arr in dfs]
    valsShuff = [list(arr[(arr['Структура'] == "hashtable") & (arr['Режим'] == "shuffled")]["Время (сек)"]) for arr in dfs]
    # 0 - sorted 1 - shuffled
    # delete
    ax[0].plot(Nvals, [row[0] for row in valsSort], label="delete", color='red')
    ax[1].plot(Nvals, [row[0] for row in valsShuff], color='red')
    # find
    ax[0].plot(Nvals, [row[1] for row in valsSort], label="find", color='blue')
    ax[1].plot(Nvals, [row[1] for row in valsShuff], color='blue')
    # insert
    ax[0].plot(Nvals, [row[2] for row in valsSort], label="insert", color='green')
    ax[1].plot(Nvals, [row[2] for row in valsShuff], color='green')


def select_data_tree(ax):
    dfs = [df500, df1000, df2000, df5000, df10000]
    Nvals = [500, 1000, 2000, 5000, 10000]
    # delete, find, insert
    # список:
    valsSort = [list(arr[(arr['Структура'] == "bintree") & (arr['Режим'] == "sorted")]["Время (сек)"]) for arr in dfs]
    valsShuff = [list(arr[(arr['Структура'] == "bintree") & (arr['Режим'] == "shuffled")]["Время (сек)"]) for arr in dfs]
    # 0 - sorted 1 - shuffled
    # delete
    ax[0].plot(Nvals, [row[0] for row in valsSort], label="delete", color='red')
    ax[1].plot(Nvals, [row[0] for row in valsShuff], color='red')
    # find
    ax[0].plot(Nvals, [row[1] for row in valsSort], label="find", color='blue')
    ax[1].plot(Nvals, [row[1] for row in valsShuff], color='blue')
    # insert
    ax[0].plot(Nvals, [row[2] for row in valsSort], label="insert", color='green')
    ax[1].plot(Nvals, [row[2] for row in valsShuff], color='green')

# построение графика
def design_show_graph(title, version, ymaxlim):
    fig, ax = plt.subplots(figsize=(10, 5), nrows=1, ncols=2)
    for i in range(2):
        match title:
            case "Tree":
                select_data_tree(ax)
            case "Linklist":
                select_data_list(ax)
            case "hasht":
                select_data_hasht(ax)
        ax[0].set_title(f"График сложностей для {title} (sort)")
        ax[1].set_title(f"График сложностей для {title} (shuff)")
        ax[i].set_xlabel("N")        
        ax[i].set_ylabel("сек * ")
        ax[i].grid(which="major", linewidth=1.5)
        ax[i].grid(which="minor", color="gray", linewidth=0.5)
        ax[i].xaxis.set_minor_locator(AutoMinorLocator())
        ax[i].yaxis.set_minor_locator(AutoMinorLocator())
        ax[i].legend()
        ax[i].set_ylim(0, ymaxlim)
    plt.savefig(f'graphics\{title}{version}.png', dpi=200)
    plt.savefig(f'graphics\T{title}{version}.eps', dpi=200)
    plt.show()


design_show_graph("hasht", 2, 0.4)