"""
Demonstration of the GazeTracking library.
Check the README.md for complete documentation.
"""

import cv2
from gaze_tracking import GazeTracking
import time
import winsound
import datetime
frequency = 1000  # Set Frequency To 2500 Hertz
duration = 100  # Set Duration To 1000 ms == 1 second

gaze = GazeTracking()
webcam = cv2.VideoCapture(0)

strat_time=time.time()
i=0
file = open("log.txt", "a")
while True:
    # We get a new frame from the webcam
    _, frame = webcam.read()

    # We send this frame to GazeTracking to analyze it
    gaze.refresh(frame)

    frame = gaze.annotated_frame()
    text = ""
      
    
    if gaze.is_blinking():
        counter=0
        text = "Blinking"
        file.write(f"{datetime.datetime.now().second} - Number: 0\n")

    elif gaze.is_right():
        counter=0
        text = "Looking right"
        #winsound.Beep(frequency, duration)
        file.write(f"{datetime.datetime.now().second} - Number: 1\n")

    elif gaze.is_left():
        counter=0
        text = "Looking left"
        #winsound.Beep(frequency, duration)
        file.write(f"{datetime.datetime.now().second} - Number: 2\n")


    elif gaze.is_center():
        counter=0
        text = "Looking center"
        file.write(f"{datetime.datetime.now().second} - Number: 0\n")

    else:
        winsound.Beep(frequency, duration)
        file.write(f"{datetime.datetime.now().second} - Number: -4\n")

    
    #start_time = time.time()


    cv2.putText(frame, text, (90, 60), cv2.FONT_HERSHEY_DUPLEX, 1.6, (147, 58, 31), 2)

    left_pupil = gaze.pupil_left_coords()
    right_pupil = gaze.pupil_right_coords()
    cv2.putText(frame, "Left pupil:  " + str(left_pupil), (90, 130), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)
    cv2.putText(frame, "Right pupil: " + str(right_pupil), (90, 165), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)

    cv2.imshow("Demo", frame)
    '''
    start_time = time.time()
    while(text == "Looking right" or text == "Looking left" or counter==4 ):
        if(time.time()-start_time>1):
            winsound.Beep(frequency, duration)
        else:
            break
    '''
    if cv2.waitKey(1) == 27:
        break
   
webcam.release()
cv2.destroyAllWindows()
