import numpy as np
import math
from matplotlib import pyplot as plt
import os

def analyzeSectionOne(nameID, fps, xLE, yLE, xRE, yRE, pupilSizeLE, pupilSizeRE, calibration, calibrationXLE):
    time = range(len(xLE))
    time = [x / fps for x in time]
    ### EACH COORDINATE FOR EACH EYE OVER TIME
    
    # if not os.path.exists("Outputs"):
    #     os.makedirs("Outputs")
        
    # if not os.path.exists("Outputs/SectionOne"):
    #     os.makedirs("Outputs/SectionOne")
        
    # if not os.path.exists("Outputs/SectionOne/" + nameID):
    #     os.makedirs("Outputs/SectionOne/" + nameID)
        
    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 + "/SectionOne"):
        os.makedirs("Outputs/" + nameID + "/SectionOne")

    plt.plot(time, xLE)
    plt.xlabel('Time')
    plt.ylabel('X')
    plt.title('X coordinate of Left Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "xLe_Time.pdf")
    plt.show()

    plt.plot(time, xRE)
    plt.xlabel('Time')
    plt.ylabel('X')
    plt.title('X coordinate of Right Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "xRe_Time.pdf")
    plt.show()

    plt.plot(time, yLE)
    plt.xlabel('Time')
    plt.ylabel('Y')
    plt.title('Y coordinate of Left Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "yLe_Time.pdf")
    plt.show()

    plt.plot(time, yRE)
    plt.xlabel('Time')
    plt.ylabel('Y')
    plt.title('Y coordinate of Right Pupil Center')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "yRe_Time.pdf")
    plt.show()

    # print(xLE)
    # print(yLE)
    # print(xRE)
    # print(yRE)
    print(len(xLE))
    print(len(yLE))
    print(len(xRE))
    print(len(yRE))
    
    
    ### ROUTE (DISPLACEMENT) OVER TIME
    
    #routeLE = [math.sqrt(xLE[i] ** 2 + yLE[i] ** 2) for i in range(len(xLE))]
    #routeRE = [math.sqrt(xRE[i] ** 2 + yRE[i] ** 2) for i in range(len(xRE))]
    
    routeLE = []
    routeRE = []
    
    for i in range(len(xLE)-1):
        routeLE.append(math.sqrt((xLE[i+1] - xLE[i]) ** 2 + (yLE[i+1] - yLE[i]) ** 2))
    
    for i in range(len(xRE)-1):
        routeRE.append(math.sqrt((xRE[i+1] - xRE[i]) ** 2 + (yRE[i+1] - yRE[i]) ** 2))
    
    timeRoute = range(len(routeLE))
    
    plt.plot(timeRoute, routeLE)
    plt.xlabel('Time')
    plt.ylabel('Route')
    plt.title('Route of the Left Eye')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "routeLE_Time.pdf")
    plt.show()
    
    plt.plot(timeRoute, routeRE)
    plt.xlabel('Time')
    plt.ylabel('Route')
    plt.title('Route of the Right Eye')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "routeRE_Time.pdf")
    plt.show()
    
    
    ### VELOCITY OVER TIME
    
    velocityLE = []
    velocityRE = []
    deltaTime = 1 / fps
    
    for i in range(len(routeLE)):
        velocityLE.append(routeLE[i] / deltaTime)
        
    for i in range(len(routeRE)):
        velocityRE.append(routeRE[i] / deltaTime)
    
    plt.plot(timeRoute, velocityLE)
    plt.xlabel('Time')
    plt.ylabel('Velocity')
    plt.title('Velocity of the Left Eye')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "velocityLE_Time.pdf")
    plt.show()
    
    plt.plot(timeRoute, velocityRE)
    plt.xlabel('Time')
    plt.ylabel('Velocity')
    plt.title('Velocity of the Right Eye')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "velocityRE_Time.pdf")
    plt.show()
    
    
    ### ACCELERATION OVER TIME
    
    accelerationLE = []
    accelerationRE = []
    
    for i in range(len(velocityLE)):
        accelerationLE.append(velocityLE[i] / deltaTime)
        
    for i in range(len(velocityRE)):
        accelerationRE.append(velocityRE[i] / deltaTime)
    
    plt.plot(timeRoute, accelerationLE)
    plt.xlabel('Time')
    plt.ylabel('Acceleration')
    plt.title('Acceleration of the Left Eye')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "accelerationLE_Time.pdf")
    plt.show()
    
    plt.plot(timeRoute, accelerationRE)
    plt.xlabel('Time')
    plt.ylabel('Acceleration')
    plt.title('Acceleration of the Right Eye')
    plt.savefig("Outputs/" + nameID + "/SectionOne" + "/" + "accelerationRE_Time.pdf")
    plt.show()
    
    calibrationXLE
    
    # Pupil Size Estimation
    calibPupilSizeLE = pupilSizeLE[calibration[0]:calibration[1]]
    calibPupilSizeRE = pupilSizeRE[calibration[0]:calibration[1]]
    
    calibPupilSizeLE = pupilSizeLE[calibration[0]:calibration[0] + 2 * fps]
    
    pupilMean = np.mean(calibPupilSizeLE)
    print(pupilMean)
    
    timePS = range(len(calibPupilSizeLE))
    timePS = [x / fps for x in timePS]
    
    plt.plot(timePS, calibPupilSizeLE)
    plt.xlabel('Time')
    plt.ylabel('Pupil Size [pixel counts]')
    plt.title('Pupil Size of Left Eye in Calibration Interval')
    plt.savefig("PupilSizeLE.pdf")
    plt.show()
    
    
    