import py5
import cv2
import time
import random

from gaze_tracking import GazeTracking


class floater:
    def __init__(self,x,y, xspeed, yspeed, image):
        self.image=image
        self.x = x
        self.y = y
        self.vitesse= py5.Py5Vector(xspeed, yspeed)

    def update(self, accelvector):

    # limiter accelvector avant
        accelvector.set_limit(400)
        accelvector.set_heading(accelvector.heading + random.uniform(-0.1, 0.1))
        accelvector.set_mag(accelvector.mag * random.uniform(0.9, 1.1))

        self.vitesse.x += accelvector.x
    # ajouter la gravité
        self.vitesse.y += (accelvector.y + 0.1)

        self.x += self.vitesse.x
        self.y += self.vitesse.y


     # faire converger self.vitesse vers zéro
        self.vitesse.set_mag(self.vitesse.mag * 0.99)

        if self.x > 1.5 * py5.width:
            self.x = self.x - (2 * py5.width)
        if self.x < - 0.5 * py5.width :
            self.x = 2 *py5.width + self.x
        if self.y > 1.5 * py5.height:
            self.y = self.y - (2 * py5.height)
        if self.y < - 0.5 * py5.height :
            self.y = 2 *py5.height + self.y



    def display(self):
        py5.image(self.image, self.x, self.y, self.image.width, self.image.height)


gaze_direction = None
dernier_gaze_direction_non_nul = py5.Py5Vector2D(0 , 0)

def cv_thread(webcam, gaze):

    global gaze_direction

    _, frame = webcam.read()
    gaze.refresh(frame)
    try :
        gaze_direction = py5.Py5Vector2D(0.5 - gaze.horizontal_ratio() , gaze.vertical_ratio() - 0.5)
        gaze_direction.mag = 100 * (gaze_direction.mag ** 2)
    except Exception as e :
        print('Error')

def setup():

    py5.full_screen()

    global fl1
    global fl2
    global fl3
    global fl4
    global fl5
    global fl6
    global fl7
    global fl8
    global fl9

    global liste_floaters

    global img

    global gaze
    global webcam





    gaze = GazeTracking()
    webcam = cv2.VideoCapture(0)

    #good tout à gauche tout en haut
    floater1 = py5.load_image("resources/microscopeitsrev00carp_0705_A.png")
    fl1 = floater(-400,-400, 0 , 0, floater1)

    #good en bas à droite
    floater2 = py5.load_image("resources/microscopeitsrev00carp_0705_B.png")
    fl2 = floater(1250,1040, 0 , 0, floater2)

    #good tout à gauche au milieu
    floater3 = py5.load_image("resources/microscopeitsrev00carp_0707.png")
    fl3 = floater(-600,420, 0 , 0, floater3)

    #good en haut à droite
    floater4 = py5.load_image("resources/microscopeitsrev00carp_0722c.png")
    fl4 = floater(1200, 250, 0 , 0, floater4)

    #good au milieu horizontalement, tout en haut
    floater5 = py5.load_image("resources/microscopeitsrev00carp_0723.png")
    fl5 = floater(750,-400, 0 , 0, floater5)

    #good à gauche au milieu
    floater6 = py5.load_image("resources/naturethroughmic00kerrrich_0049.png")
    fl6 = floater(100,600, 0 , 0, floater6)

    #good en haut à droite
    floater7 = py5.load_image("resources/naturethroughmic00kerrrich_0101.png")
    fl7 = floater(1540, 84, 0 , 0, floater7)

    #good tout à droite au milieu
    floater8 = py5.load_image("resources/naturethroughmic00kerrrich_0179.png")
    fl8 = floater(2100, 800, 0 , 0, floater8)

    #good à gauche au milieu
    floater9 = py5.load_image("resources/naturethroughmic00kerrrich_0205.png")
    fl9 = floater(150, 960, 0 , 0, floater9)

    #good au milieu au milieu
    floater10 = py5.load_image("resources/Bourbon_virus_filament_EID_2015_Fig_2a.png")
    fl10 = floater(500, 700, 0 , 0, floater10)

    #test
    floater11 = py5.load_image("resources/test.png")
    fl11 = floater(0, 0, 0 , 0, floater11)





    liste_floaters = [fl1 , fl2, fl3, fl4, fl5, fl6, fl7, fl8, fl9, fl10, fl11]

    img = py5.load_image("resources/SDIM1109.JPG")



    py5.launch_repeating_thread(cv_thread, name='cv thread', args= (webcam, gaze), time_delay=0.05)



#def predraw_update():




def draw():

    global dernier_gaze_direction_non_nul

    py5.image(img, 0, 0, py5.width, py5.height)

    accel = py5.Py5Vector2D(0 , 0)

    if gaze_direction != None :
        accel = gaze_direction - dernier_gaze_direction_non_nul
        dernier_gaze_direction_non_nul = gaze_direction



    for p in liste_floaters :
        p.update(accel)
        p.display()





    print('gaze_direction actuel' + str(gaze_direction))
    print('dernier gaze_direction non-nul' + str(dernier_gaze_direction_non_nul))
    #print(py5.get_frame_rate())


def exiting():
    webcam.release()
    py5.stop_all_threads()


py5.run_sketch()

