import math
from matplotlib import pyplot as plt
import os

dynamicALL = []

def analyzeSectionTwo(nameID, dynamicPart, dynamicPartOneMove, fps, xLE, yLE, xRE, yRE, xLEcm, yLEcm, xREcm, yREcm):
    dynamicXLE = xLEcm[dynamicPart[0]:dynamicPart[1]]
    dynamicYLE = yLEcm[dynamicPart[0]:dynamicPart[1]]
    dynamicXRE = xREcm[dynamicPart[0]:dynamicPart[1]]
    dynamicYRE = yREcm[dynamicPart[0]:dynamicPart[1]]
    dynamicYRE = [x * -1 for x in dynamicYRE]
    
    dynamicXLEOneMove = xLE[dynamicPartOneMove[0]:dynamicPartOneMove[1] + fps]
    dynamicYLEOneMove = yLE[dynamicPartOneMove[0]:dynamicPartOneMove[1] + fps]
    dynamicXREOneMove = xRE[dynamicPartOneMove[0]:dynamicPartOneMove[1] + fps]
    dynamicYREOneMove = yRE[dynamicPartOneMove[0]:dynamicPartOneMove[1] + fps]
    
    timeDynamicPart = range(len(xLE))[dynamicPart[0]:dynamicPart[1]]
    
    if not os.path.exists("Outputs"):
        os.makedirs("Outputs")
        
    if not os.path.exists("Outputs/" + nameID):
        os.makedirs("Outputs/" + nameID)
        
    if not os.path.exists("Outputs/" + nameID + "/SectionTwo"):
        os.makedirs("Outputs/" + nameID + "/SectionTwo")
     
        
    ### EACH COORDINATE FOR EACH EYE OVER TIME    
     
    plt.plot(timeDynamicPart, dynamicXLE)
    plt.xlabel('Time')
    plt.ylabel('X')
    plt.title('X coordinate of Dynamic Part of Left Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicXLE_Time.pdf")
    plt.show()
    
    plt.plot(timeDynamicPart, dynamicYLE)
    plt.xlabel('Time')
    plt.ylabel('Y')
    plt.title('Y coordinate of Dynamic Part of Left Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicYLE_Time.pdf")
    plt.show()
    
    plt.plot(timeDynamicPart, dynamicXRE)
    plt.xlabel('Time')
    plt.ylabel('X')
    plt.title('X coordinate of Dynamic Part of Right Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicXRE_Time.pdf")
    plt.show()
    
    plt.plot(timeDynamicPart, dynamicYRE)
    plt.xlabel('Time')
    plt.ylabel('Y')
    plt.title('Y coordinate of Dynamic Part of Right Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicYRE_Time.pdf")
    plt.show()
    
    
    ### X COORDINATE OVER Y COORDINATE
    
    plt.plot(dynamicYLE, dynamicXLE)
    plt.xlabel('Y')
    plt.ylabel('X')
    plt.title('X and Y coordinates of Dynamic Part of Left Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicXLE_dynamicYLE.pdf")
    plt.show()
    
    plt.plot(dynamicXRE, dynamicYRE)
    plt.xlabel('X [cm]')
    plt.ylabel('Y [cm]')
    plt.title('X and Y coordinates of Dynamic Part of Right Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicXRE_dynamicYRE.pdf")
    plt.show()
    
    ### ROUTE, VELOCITY, ACCELERATION FOR DYNAMIC PART ONLY
    
    # ROUTE
    dynamicRouteLE = []
    dynamicRouteRE = []
    
    for i in range(len(dynamicXLE)-1):
        dynamicRouteLE.append(math.sqrt((dynamicXLE[i+1] - dynamicXLE[i]) ** 2 + (dynamicYLE[i+1] - dynamicYLE[i]) ** 2))
    
    for i in range(len(dynamicXRE)-1):
        dynamicRouteRE.append(math.sqrt((dynamicXRE[i+1] - dynamicXRE[i]) ** 2 + (dynamicYRE[i+1] - dynamicYRE[i]) ** 2))
    
    dynamicTimeRoute = range(len(dynamicRouteLE))
    
    plt.plot(dynamicTimeRoute, dynamicRouteLE)
    plt.xlabel('Time')
    plt.ylabel('Distance')
    plt.title('Route of the Left Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicRoute_Time_LeftEye.pdf")
    plt.show()
    
    plt.plot(dynamicTimeRoute, dynamicRouteRE)
    plt.xlabel('Time')
    plt.ylabel('Distance')
    plt.title('Route of the Right Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicRoute_Time_RightEye.pdf")
    plt.show()
    
    # VELOCITY
    dynamicVelocityLE = []
    dynamicVelocityRE = []
    deltaTime = 1 / fps
    
    for i in range(len(dynamicRouteLE)):
        dynamicVelocityLE.append(dynamicRouteLE[i] / deltaTime)
        
    for i in range(len(dynamicRouteRE)):
        dynamicVelocityRE.append(dynamicRouteRE[i] / deltaTime)
    
    plt.plot(dynamicTimeRoute, dynamicVelocityLE)
    plt.xlabel('Time')
    plt.ylabel('Velocity')
    plt.title('Velocity of the Left Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "DynamicVelocityLE_Time.pdf")
    plt.show()
    
    plt.plot(dynamicTimeRoute, dynamicVelocityRE)
    plt.xlabel('Time')
    plt.ylabel('Velocity')
    plt.title('Velocity of the Right Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "DynamicVelocityRE_Time.pdf")
    plt.show()
    
    ### ROUTE, VELOCITY, ACCELERATION FOR JUST ONE MOVE IN DYNAMIC PART
    
    # ROUTE
    dynamicRouteLEOneMove = []
    dynamicRouteREOneMove = []
    
    for i in range(len(dynamicXLEOneMove)-1):
        dynamicRouteLEOneMove.append(math.sqrt((dynamicXLEOneMove[i+1] - dynamicXLEOneMove[i]) ** 2 + (dynamicYLEOneMove[i+1] - dynamicYLEOneMove[i]) ** 2))
    
    for i in range(len(dynamicXREOneMove)-1):
        dynamicRouteREOneMove.append(math.sqrt((dynamicXREOneMove[i+1] - dynamicXREOneMove[i]) ** 2 + (dynamicYREOneMove[i+1] - dynamicYREOneMove[i]) ** 2))
    
    dynamicTimeRoute = range(len(dynamicRouteLEOneMove))
    
    plt.plot(dynamicTimeRoute, dynamicRouteLEOneMove)
    plt.xlabel('Time')
    plt.ylabel('Distance')
    plt.title('Route of the Left Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicRoute_Time_LeftEye_OneMove.pdf")
    plt.show()
    
    plt.plot(dynamicTimeRoute, dynamicRouteREOneMove)
    plt.xlabel('Time')
    plt.ylabel('Distance')
    plt.title('Route of the Right Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionTwo" + "/" + "dynamicRoute_Time_RightEye_OneMove.pdf")
    plt.show()
    
    # VELOCITY
    
    dynamicVelocityLEOneMove = []
    dynamicVelocityREOneMove = []
    deltaTime = 1 / fps
    
    for i in range(len(dynamicRouteLEOneMove)):
        dynamicVelocityLEOneMove.append(dynamicRouteLEOneMove[i] / deltaTime)
        
    for i in range(len(dynamicRouteREOneMove)):
        dynamicVelocityREOneMove.append(dynamicRouteREOneMove[i] / deltaTime)
    
    
    dynamicALL.append(dynamicVelocityLEOneMove)
    
    main_list = dynamicALL
    # Find the length of the shortest list
    min_length = min(len(sub_list) for sub_list in main_list)
    
    # Iterate over each list and truncate to the length of the shortest list
    for i in range(len(main_list)):
        main_list[i] = main_list[i][:min_length]
    
    # Remove extra elements from each list
    main_list = [sub_list[:min_length] for sub_list in main_list]
    
    # Print the modified main list
    print(main_list)
    
    min_length = min(len(sublist) for sublist in main_list)

    # Calculate the fps based on the length of the shortest sublist
    fps = 369  # Assuming a frame rate of 30 fps
    time = [frame / fps for frame in range(min_length)]
    
    # Truncate all sublists to the length of the shortest sublist
    truncated_data = [sublist[:min_length] for sublist in main_list]
    
    # Create a figure and axis
    fig, ax = plt.subplots()
    
    # Plot each curve using a loop, assigning different colors
    for i, curve in enumerate(truncated_data):
        ax.plot(time, curve, label=f'Subject {i+1}')
    
    # Add labels and legend
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Velocity [m/s]')
    ax.legend()
    plt.savefig("dynamicALL.pdf")
    # Display the plot
    plt.show()
    
    plt.plot(dynamicTimeRoute, dynamicVelocityLEOneMove)
    plt.xlabel('Time')
    plt.ylabel('Velocity')
    plt.title('Velocity of the Left Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "DynamicVelocityLE_Time_OneMove.pdf")
    plt.show()
    
    plt.plot(dynamicTimeRoute, dynamicVelocityREOneMove)
    plt.xlabel('Time')
    plt.ylabel('Velocity')
    plt.title('Velocity of the Right Eye in Dynamic Part')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "DynamicVelocityRE_Time_OneMove.pdf")
    plt.show()