File size: 3,407 Bytes
84121fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react'
import { channelService } from '../services/channelService'
import { Channel, ChannelCategory } from '../types/channel'

interface ChannelContextType {
  channels: Channel[]
  categories: ChannelCategory[]
  currentChannel: Channel | null
  isLoading: boolean
  error: string | null
  favorites: string[]
  searchTerm: string
  selectedCategory: string
  viewerCount: number
  setCurrentChannel: (channel: Channel | null) => void
  setSearchTerm: (term: string) => void
  setSelectedCategory: (category: string) => void
  toggleFavorite: (channelId: string) => void
  refreshChannels: () => Promise<void>
}

const ChannelContext = createContext<ChannelContextType | undefined>(undefined)

export function ChannelProvider({ children }: { children: ReactNode }) {
  const [channels, setChannels] = useState<Channel[]>([])
  const [categories, setCategories] = useState<ChannelCategory[]>([])
  const [currentChannel, setCurrentChannel] = useState<Channel | null>(null)
  const [isLoading, setIsLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)
  const [favorites, setFavorites] = useState<string[]>([])
  const [searchTerm, setSearchTerm] = useState('')
  const [selectedCategory, setSelectedCategory] = useState('all')
  const [viewerCount, setViewerCount] = useState(0)

  useEffect(() => {
    const savedFavorites = localStorage.getItem('daddytv_favorites')
    if (savedFavorites) {
      setFavorites(JSON.parse(savedFavorites))
    }
  }, [])

  useEffect(() => {
    localStorage.setItem('daddytv_favorites', JSON.stringify(favorites))
  }, [favorites])

  const refreshChannels = async () => {
    setIsLoading(true)
    setError(null)
    try {
      const data = await channelService.getChannels()
      setChannels(data.channels)
      setCategories(data.categories)
    } catch (err) {
      setError('Error al cargar los canales. Inténtalo de nuevo.')
      console.error('Error loading channels:', err)
    } finally {
      setIsLoading(false)
    }
  }

  const toggleFavorite = (channelId: string) => {
    setFavorites(prev => 
      prev.includes(channelId) 
        ? prev.filter(id => id !== channelId)
        : [...prev, channelId]
    )
  }

  const handleSetCurrentChannel = async (channel: Channel | null) => {
    if (channel) {
      try {
        const result = await channelService.setViewing(channel.url)
        if (result.switched) {
          // Mostrar mensaje de cambio automático
          console.log('Canal cambiado automáticamente')
        }
        setViewerCount(result.viewers || 1)
      } catch (err) {
        console.error('Error setting current channel:', err)
      }
    }
    setCurrentChannel(channel)
  }

  return (
    <ChannelContext.Provider value={{
      channels,
      categories,
      currentChannel,
      isLoading,
      error,
      favorites,
      searchTerm,
      selectedCategory,
      viewerCount,
      setCurrentChannel: handleSetCurrentChannel,
      setSearchTerm,
      setSelectedCategory,
      toggleFavorite,
      refreshChannels
    }}>
      {children}
    </ChannelContext.Provider>
  )
}

export function useChannels() {
  const context = useContext(ChannelContext)
  if (context === undefined) {
    throw new Error('useChannels must be used within a ChannelProvider')
  }
  return context
}