'use client' import { useState, useEffect } from 'react' // import { useTranslations } from 'next-intl' import { Box, Card, CardContent, Typography, Select, MenuItem, FormControl, InputLabel, Button, Alert, CircularProgress, Chip, Paper, Divider, IconButton, Tooltip } from '@mui/material' import { CloudOff, Wifi, WifiOff, NavigateBefore, NavigateNext, MenuBook, Download } from '@mui/icons-material' import { offlineStorage, type BibleVersion, type BibleBook, type BibleChapter } from '@/lib/offline-storage' interface OfflineBibleReaderProps { onRequestDownload?: () => void } export function OfflineBibleReader({ onRequestDownload }: OfflineBibleReaderProps) { // const t = useTranslations('bible') const [isOnline, setIsOnline] = useState(true) const [versions, setVersions] = useState([]) const [selectedVersion, setSelectedVersion] = useState('') const [books, setBooks] = useState([]) const [selectedBook, setSelectedBook] = useState('') const [currentChapter, setCurrentChapter] = useState(null) const [selectedChapterNum, setSelectedChapterNum] = useState(1) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { checkOnlineStatus() loadOfflineVersions() // Listen for online/offline events const handleOnline = () => setIsOnline(true) const handleOffline = () => setIsOnline(false) window.addEventListener('online', handleOnline) window.addEventListener('offline', handleOffline) return () => { window.removeEventListener('online', handleOnline) window.removeEventListener('offline', handleOffline) } }, []) useEffect(() => { if (selectedVersion) { loadBooksForVersion(selectedVersion) } }, [selectedVersion]) useEffect(() => { if (selectedBook && selectedVersion) { loadChapter(selectedVersion, selectedBook, selectedChapterNum) } }, [selectedBook, selectedChapterNum, selectedVersion]) const checkOnlineStatus = () => { setIsOnline(navigator.onLine) } const loadOfflineVersions = async () => { try { setLoading(true) const offlineVersions = await offlineStorage.getAllVersions() setVersions(offlineVersions) if (offlineVersions.length > 0 && !selectedVersion) { setSelectedVersion(offlineVersions[0].id) } } catch (error) { console.error('Failed to load offline versions:', error) setError('Failed to load offline Bible versions') } finally { setLoading(false) } } const loadBooksForVersion = async (versionId: string) => { try { const versionBooks = await offlineStorage.getBooksForVersion(versionId) setBooks(versionBooks.sort((a, b) => a.orderNum - b.orderNum)) if (versionBooks.length > 0 && !selectedBook) { setSelectedBook(versionBooks[0].id) setSelectedChapterNum(1) } } catch (error) { console.error('Failed to load books:', error) setError('Failed to load books for this version') } } const loadChapter = async (versionId: string, bookId: string, chapterNum: number) => { try { setError(null) const chapter = await offlineStorage.getChapter(versionId, bookId, chapterNum) if (chapter) { setCurrentChapter(chapter) } else { setError(`Chapter ${chapterNum} not found in offline storage`) } } catch (error) { console.error('Failed to load chapter:', error) setError('Failed to load chapter from offline storage') } } const navigateChapter = (direction: 'prev' | 'next') => { if (!currentChapter || !selectedBook) return const currentBook = books.find(b => b.id === selectedBook) if (!currentBook) return let newChapterNum = selectedChapterNum let newBookId = selectedBook if (direction === 'next') { if (selectedChapterNum < currentBook.chaptersCount) { newChapterNum = selectedChapterNum + 1 } else { // Move to next book const currentBookIndex = books.findIndex(b => b.id === selectedBook) if (currentBookIndex < books.length - 1) { newBookId = books[currentBookIndex + 1].id newChapterNum = 1 } } } else { if (selectedChapterNum > 1) { newChapterNum = selectedChapterNum - 1 } else { // Move to previous book const currentBookIndex = books.findIndex(b => b.id === selectedBook) if (currentBookIndex > 0) { const prevBook = books[currentBookIndex - 1] newBookId = prevBook.id newChapterNum = prevBook.chaptersCount } } } if (newBookId !== selectedBook) { setSelectedBook(newBookId) } setSelectedChapterNum(newChapterNum) } const getCurrentBookName = () => { return books.find(b => b.id === selectedBook)?.name || '' } const getVersionName = () => { return versions.find(v => v.id === selectedVersion)?.name || '' } if (loading) { return ( ) } if (versions.length === 0) { return ( No Offline Bible Versions You haven't downloaded any Bible versions for offline reading yet. ) } return ( {/* Header with status */} Offline Bible Reader : } label={isOnline ? 'Online' : 'Offline'} color={isOnline ? 'success' : 'warning'} size="small" /> {/* Controls */} Bible Version Book Chapter {/* Error Alert */} {error && ( {error} )} {/* Chapter Content */} {currentChapter && ( {/* Chapter Header */} {getCurrentBookName()} {selectedChapterNum} {getVersionName()} {/* Verses */} {currentChapter.verses.map((verse) => ( {verse.verseNum} {verse.text} ))} {/* Navigation */} navigateChapter('prev')} disabled={selectedChapterNum === 1 && books.findIndex(b => b.id === selectedBook) === 0} > {currentChapter.verses.length} verses navigateChapter('next')} disabled={ selectedChapterNum === (books.find(b => b.id === selectedBook)?.chaptersCount || 1) && books.findIndex(b => b.id === selectedBook) === books.length - 1 } > )} ) }