import { useEffect } from 'react';
import { router } from 'expo-router';
import * as Haptics from 'expo-haptics';
import { eventBus, AppEvent } from '@/core/events/EventBus';
import { hrefVerificationGuide } from '@/navigation/hrefs';

let verificationModalShown = false;

export function EventSubscriber() {
  useEffect(() => {
    return eventBus.subscribe((event: AppEvent) => {
      switch (event.type) {
        case 'NAVIGATE':
          router.push(event.payload.path);
          break;

        case 'NAVIGATE_BACK':
          router.back();
          break;

        case 'SHOW_VERIFICATION_MODAL':
          if (verificationModalShown) return;
          verificationModalShown = true;
          router.push(hrefVerificationGuide());
          setTimeout(() => {
            verificationModalShown = false;
          }, 3000);
          break;

        case 'VIBRATE':
          Haptics.notificationAsync(
            event.payload.type === 'message'
              ? Haptics.NotificationFeedbackType.Success
              : event.payload.type === 'group_message'
                ? Haptics.NotificationFeedbackType.Warning
                : Haptics.NotificationFeedbackType.Error
          ).catch(() => {});
          break;
      }
    });
  }, []);

  return null;
}
