import { useEffect } from 'react'
import { useRouter } from 'expo-router'
import * as Haptics from 'expo-haptics'
import { useTimerEngine } from '@/src/features/timer'
import { useAudioEngine } from '@/src/features/audio'
import { TimerDisplay } from '@/src/features/timer/components/TimerDisplay'
import type { TimerEvent } from '@/src/features/timer/types'

export default function TimerScreen() {
  const router = useRouter()
  const timer = useTimerEngine()
  const audio = useAudioEngine()

  // Preload audio on mount
  useEffect(() => {
    audio.preloadAll()
    return () => {
      audio.unloadAll()
    }
  }, [])

  // Subscribe to timer events → trigger audio + haptics
  useEffect(() => {
    const unsubscribe = timer.addEventListener(async (event: TimerEvent) => {
      switch (event.type) {
        case 'PHASE_CHANGED':
          await handlePhaseChange(event.to)
          break

        case 'COUNTDOWN_TICK':
          await audio.playPhaseSound(
            event.secondsLeft === 1 ? 'count_1' : event.secondsLeft === 2 ? 'count_2' : 'count_3'
          )
          Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
          break

        case 'ROUND_COMPLETED':
          await audio.playPhaseSound('bell')
          Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success)
          break

        case 'SESSION_COMPLETE':
          await audio.playPhaseSound('fanfare')
          await audio.stopMusic(1000)
          Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success)
          break
      }
    })

    return unsubscribe
  }, [audio.isLoaded])

  async function handlePhaseChange(to: string) {
    switch (to) {
      case 'GET_READY':
        await audio.startMusic('LOW')
        Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium)
        break
      case 'WORK':
        await audio.playPhaseSound('beep_long')
        await audio.switchIntensity('HIGH')
        Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy)
        break
      case 'REST':
        await audio.playPhaseSound('beep_double')
        await audio.switchIntensity('LOW')
        Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
        break
    }
  }

  function handleStart() {
    timer.start()
  }

  function handleStop() {
    timer.stop()
    audio.stopMusic(300)
    router.back()
  }

  return (
    <TimerDisplay
      state={{
        phase: timer.phase,
        secondsLeft: timer.secondsLeft,
        currentRound: timer.currentRound,
        totalRounds: timer.totalRounds,
        currentCycle: timer.currentCycle,
        totalCycles: timer.totalCycles,
        isRunning: timer.isRunning,
        isPaused: timer.isPaused,
        totalElapsedSeconds: timer.totalElapsedSeconds,
      }}
      config={timer.config}
      exerciseName="Burpees"
      nextExerciseName="Squats"
      onStart={handleStart}
      onPause={timer.pause}
      onResume={timer.resume}
      onStop={handleStop}
      onSkip={timer.skip}
    />
  )
}
