import PospacAnalysis as pa
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import mutils as m
from mutils.PosPacConst import *
import mutils.PosTypeConst as PosTypeConst
import SummaryPlots as sa
import gmplot

"""
This code plots 2D and 3D CDF comparisons. 
User inputs:
    - Directory (e.g. 2021-02-22-SantaNella-van-test)
    - Number of comparisons (i.e. how many lines will be itemized on the legend)
    - Name of comparison (this will show up in the filename (i.e. singlebase_vs_smartbase_2D-CDF-Compare.png)
"""


# Get user inputs
directory = pa.getDirFromUser(path = '.')
#Change this if we want to compare more things
comparisons = int(input("Number of comparisons for CDF comparison: "))
saveName = input("Name of comparison? ")

#SHow the list of directoires so the user can see the options for selection
plotDirList = list()
path = directory + '/Plots'

#Plot each comparison line one by one
for j in range(comparisons):
    plotDir = pa.getDirFromUser(path)
    plotDirList.append(plotDir)

#Create a 2D and a 3D plot
for option in range(2):
    plt.figure()
    if option == 0:
        plotType = '2D'
    else:
        plotType = '3D'

    #For each comparison, go through the plotting process
    for i in range(comparisons):
        path = directory + '/Plots'
        plotDir = plotDirList[i]
        path = directory + '/Plots/' + plotDir
        file = path + '/enu_errors.csv'
        df = pd.read_csv(file)
        sa.cdfComparisonPlot(df,plotDir,plotType,directory) #Use the name of the truth file and the directory name as the legend label and plot title

    m.save_compressed_png(directory + '/Summary Plots/' + saveName + '_' + plotType  + '-CDF-Compare.png', dpi=300)


"""
This commented out code is for getting user input about whether to run a CDF comparison or an error bar summary. For now, the code just runs
the CDF comparison, so this was commented out
"""
#cdf = input("CDF? (y/n)")

#Testing error bar

#errorBar = input("Error bar summary? (y/n)")
# if cdf == 'y':
#     cdfSummary()
# if errorBar == 'y':
#     errorBarSummary()
# def errorBarSummary():
#     plt.figure()

#     allTests = input("Compare all possible tests (y/n)?")

#     for option in range(2):
#         if option == 0:
#             plotType = '2D'
#         else:
#             plotType = '3D'

#         if allTests == 'y':

#             plt.figure()
#             dirList = os.listdir()
#             count = 0
#             for directory in dirList:
#                 if os.path.isdir(directory):
#                     if directory == '__pycache__':
#                         continue
#                     count += 1
#                     path = directory + '/Plots/' 
#                     if count < 2 and plotType == '2D':
#                         plotDir = pa.getDirFromUser(directory + '/Plots')
#                     file = path + plotDir + '/enu_errors.csv'
#                     if not os.path.exists(file):
#                         continue
#                     df = pd.read_csv(file)
#                     sa.barPlotComparison(df,directory,plotType,count)

#             #Save plot
#             m.save_compressed_png(plotDir + '-' + plotType + '-Error.png', dpi=300)
