# importing libraries
import cv2
import pyautogui
from cvzone.HandTrackingModule import HandDetector

# defining variables and objects
cap = cv2.VideoCapture(0)
detector = HandDetector(detectionCon=0.3, minTrackCon=0.7)
pyautogui.FAILSAFE = True
currentposset = False

######################Settings
MouseTrackingOn = True
MouseClickingOn = True
UpDownKeysOn = True
SwitchPagesOn = True
PauseControllOn = True
CUSTOM1 = True
##############################

while True:

    # read camera and flip image
    success, frames = cap.read()
    frames = cv2.flip(frames, 1)
    hands, img = detector.findHands(frames, flipType=False)

    if hands:  # check if there is a hand detected
        hand1 = hands[0]
        lmList1 = hand1["lmList"]
        # bbox1 = hand1["bbox"]
        center1 = hand1["center"]
        handType1 = hand1["type"]

        if handType1 == 'Right':  # check if its right hand (you can change)
            fingers1 = detector.fingersUp(hand1)  ## fingers1 stores a lift of the fingers up

            if MouseTrackingOn:  # if mouseTracking is True then execute the code for mouseTracking
                if not currentposset:
                    currentTipXPos, currentTipYPos = lmList1[8][0:2]
                    currentposset = True
                if fingers1[0] == 1 and fingers1[1] == 1 and fingers1[2] == 0 and fingers1[3] == 0 and fingers1[
                    4] == 0 and currentposset:
                    differX, differY = (lmList1[8][0] - currentTipXPos) / 5, (lmList1[8][1] - currentTipYPos) / 5
                    MousePos = pyautogui.position()
                    pyautogui.moveTo(MousePos[0] + differX, MousePos[1] + differY)

                else:
                    currentposset = False

            length, info, img = detector.findDistance(lmList1[8][0:2], lmList1[4][0:2], img, (255, 0, 0),
                                                      10)  # gets length between 2 points (lmList1[8][0:2] and lmList1[4][0:2].)
            if MouseClickingOn:  # if mouseClicking is True then execute the code for mouseClicking
                if length < 50 and fingers1[2] == 0 and fingers1[3] == 0 and fingers1[4] == 0:
                    pyautogui.click(interval=1, button='left')
                elif length < 50 and fingers1[2] == 1 and fingers1[3] == 1 and fingers1[4] == 1:
                    pyautogui.click(interval=1, button='right')

            if UpDownKeysOn:  # if upDownKeys is True then execute the code for scrolling up and down
                if fingers1[0] == 1 and fingers1[1] == 1 and fingers1[2] == 1 and fingers1[3] == 0 and fingers1[4] == 0:
                    pyautogui.press('up')
                elif fingers1[0] == 0 and fingers1[1] == 1 and fingers1[2] == 1 and fingers1[3] == 0 and fingers1[4] == 0:
                    pyautogui.press('down')

            if SwitchPagesOn:  # if SwitchPages is True then execute the code for Switching Pages (Mac Function)
                if fingers1[0] == 1 and fingers1[1] == 1 and fingers1[2] == 1 and fingers1[3] == 1 and fingers1[4] == 0:
                    cv2.waitKey(1000)
                    pyautogui.keyDown('ctrl')
                    pyautogui.press('left')
                    pyautogui.keyUp('ctrl')
                    cv2.waitKey(10)  # delay
                elif fingers1[0] == 0 and fingers1[1] == 1 and fingers1[2] == 1 and fingers1[3] == 1 and fingers1[4] == 0:
                    cv2.waitKey(1000)
                    pyautogui.keyDown('ctrl')
                    pyautogui.press('right')
                    pyautogui.keyUp('ctrl')
                    cv2.waitKey(10)  # delay

            if PauseControllOn:
                if fingers1[0] == 1 and fingers1[1] == 0 and fingers1[2] == 0 and fingers1[3] == 0 and fingers1[4] == 1:
                    pyautogui.press('k')
                    cv2.waitKey(10)  # delay

            if CUSTOM1:
                if fingers1[0] == 0 and fingers1[1] == 0 and fingers1[2] == 0 and fingers1[3] == 0 and fingers1[4] == 0:  # ADD YOUR OWN GESTURE
                    # pyautogui.
                    # Go to https://pyautogui.readthedocs.io/en/latest/quickstart.html#general-functions
                    #  and add your function
                    cv2.waitKey(10)  # delay

    print(fingers1)
    cv2.imshow("Camera", img)  # display image
    cv2.waitKey(1)
