import cv2
import mediapipe as mp
import websocket
import json
import threading
import time

# Configuração do WebSocket
ws_url = "ws://localhost:8585"  # Substitua pelo endereço do seu servidor WebSocket
ws = None

# Inicializar MediaPipe para mãos
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=2, min_detection_confidence=0.7)
mp_drawing = mp.solutions.drawing_utils

# Inicializar a captura de vídeo
cap = cv2.VideoCapture(0)

# Variáveis para rastrear movimento
x_inicial = None
movimento_detectado = None
last_gesture_time = 0  # Tempo do último gesto enviado
gesture_delay = 1.0    # Tempo mínimo entre gestos em segundos

# Função para conectar ao WebSocket
def connect_ws():
    global ws
    while True:
        try:
            ws = websocket.WebSocket()
            ws.connect(ws_url)
            print("Conectado ao servidor WebSocket")
            break
        except Exception as e:
            print(f"Erro ao conectar ao WebSocket: {e}. Tentando reconectar em 5 segundos...")
            time.sleep(5)

# Função para enviar mensagens via WebSocket
def send_message(message):
    global ws
    try:
        if ws and ws.connected:
            ws.send(json.dumps(message))
            print("Mensagem enviada para o WebSocket:", message)
        else:
            raise ConnectionError("Conexão com WebSocket perdida")
    except Exception as e:
        print(f"Erro ao enviar mensagem: {e}")
        connect_ws()

# Conectar ao WebSocket em uma nova thread para não bloquear o programa
ws_thread = threading.Thread(target=connect_ws)
ws_thread.start()

while cap.isOpened():
    success, frame = cap.read()
    if not success:
        break

    # Inverter a imagem para espelhar a visão da câmera
    frame = cv2.flip(frame, 1)
    # Converter a imagem para RGB
    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Processar a imagem e detectar mãos
    results = hands.process(frame_rgb)

    if results.multi_hand_landmarks:
        for hand_landmarks, hand_type in zip(results.multi_hand_landmarks, results.multi_handedness):
            # Desenhar pontos de referência e conexões
            mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)

            # Detectar gesto de slide
            x_atual = hand_landmarks.landmark[0].x  # Coordenada X normalizada
            if x_inicial is not None:
                deslocamento = x_atual - x_inicial
                current_time = time.time()
                if deslocamento > 0.05 and current_time - last_gesture_time > gesture_delay:
                    movimento_detectado = "slide_right"
                    last_gesture_time = current_time
                elif deslocamento < -0.05 and current_time - last_gesture_time > gesture_delay:
                    movimento_detectado = "slide_left"
                    last_gesture_time = current_time
            x_inicial = x_atual

    # Envia o movimento detectado via WebSocket
    if movimento_detectado:
        send_message({"gesture": movimento_detectado})
        movimento_detectado = None  # Reseta o estado para evitar múltiplos envios

    # Exibir a imagem
    cv2.imshow("Hand Gesture Detection", frame)

    # Sair do loop com a tecla 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Liberar os recursos
cap.release()
cv2.destroyAllWindows()
hands.close()
if ws:
    ws.close()
