'use client'

import { useState, useEffect } from 'react'
// import { useTranslations } from 'next-intl'
import {
  Box,
  Card,
  CardContent,
  Typography,
  Button,
  LinearProgress,
  List,
  ListItem,
  ListItemText,
  ListItemSecondaryAction,
  IconButton,
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
  Chip,
  Alert,
  Tooltip,
  CircularProgress
} from '@mui/material'
import {
  Download,
  Delete,
  CloudDownload,
  Storage,
  Wifi,
  WifiOff,
  CheckCircle,
  Error,
  Info,
  Refresh
} from '@mui/icons-material'
import { bibleDownloadManager, type BibleVersion, type DownloadProgress } from '@/lib/offline-storage'

interface OfflineDownloadManagerProps {
  availableVersions: BibleVersion[]
  onVersionDownloaded?: (versionId: string) => void
}

export function OfflineDownloadManager({ availableVersions, onVersionDownloaded }: OfflineDownloadManagerProps) {
  // const t = useTranslations('bible')
  const [downloadedVersions, setDownloadedVersions] = useState<BibleVersion[]>([])
  const [downloads, setDownloads] = useState<Record<string, DownloadProgress>>({})
  const [storageInfo, setStorageInfo] = useState({ used: 0, quota: 0, percentage: 0 })
  const [isOnline, setIsOnline] = useState(true)
  const [confirmDelete, setConfirmDelete] = useState<string | null>(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    loadDownloadedVersions()
    loadStorageInfo()
    checkOnlineStatus()

    // 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)
    }
  }, [])

  const loadDownloadedVersions = async () => {
    try {
      console.log('[OfflineDownloadManager] Loading downloaded versions...')
      const versions = await bibleDownloadManager.getDownloadedVersions()
      console.log('[OfflineDownloadManager] Downloaded versions:', versions)
      console.log('[OfflineDownloadManager] Number of versions:', versions.length)
      setDownloadedVersions(versions)
    } catch (error: unknown) {
      console.error('Failed to load downloaded versions:', error)
    } finally {
      setLoading(false)
    }
  }

  const loadStorageInfo = async () => {
    try {
      const info = await bibleDownloadManager.getStorageInfo()
      setStorageInfo(info)
    } catch (error: unknown) {
      console.error('Failed to load storage info:', error)
    }
  }

  const checkOnlineStatus = () => {
    setIsOnline(navigator.onLine)
  }

  const handleDownload = async (version: BibleVersion) => {
    if (!isOnline) {
      alert('You need an internet connection to download Bible versions')
      return
    }

    console.log(`Starting download for version: ${version.name} (${version.id})`)

    try {
      // Initialize download progress immediately
      setDownloads(prev => ({
        ...prev,
        [version.id]: {
          versionId: version.id,
          status: 'pending',
          progress: 0,
          totalBooks: 0,
          downloadedBooks: 0,
          totalChapters: 0,
          downloadedChapters: 0,
          startedAt: new Date().toISOString()
        }
      }))

      await bibleDownloadManager.downloadVersion(
        version.id,
        (progress: DownloadProgress) => {
          console.log(`Download progress for ${version.name}:`, progress)
          setDownloads(prev => ({
            ...prev,
            [version.id]: progress
          }))

          if (progress.status === 'completed') {
            console.log(`Download completed for ${version.name}`)
            loadDownloadedVersions()
            loadStorageInfo()
            onVersionDownloaded?.(version.id)

            // Remove from downloads state after completion
            setTimeout(() => {
              setDownloads(prev => {
                const { [version.id]: _, ...rest } = prev
                return rest
              })
            }, 3000)
          } else if (progress.status === 'failed') {
            console.error(`Download failed for ${version.name}:`, progress.error)
            alert(`Download failed: ${progress.error || 'Unknown error'}`)
          }
        }
      )
    } catch (error: unknown) {
      console.error('Download failed:', error)
      const errorMessage = (error as Error)?.message || 'Download failed'

      // Update downloads state to show failure
      setDownloads(prev => ({
        ...prev,
        [version.id]: {
          versionId: version.id,
          status: 'failed' as const,
          progress: 0,
          totalBooks: 0,
          downloadedBooks: 0,
          totalChapters: 0,
          downloadedChapters: 0,
          startedAt: new Date().toISOString(),
          error: errorMessage
        }
      }))

      alert(`Download failed: ${errorMessage}`)
    }
  }

  const handleDelete = async (versionId: string) => {
    try {
      await bibleDownloadManager.deleteVersion(versionId)
      setDownloadedVersions(prev => prev.filter(v => v.id !== versionId))
      loadStorageInfo()
      setConfirmDelete(null)
    } catch (error: unknown) {
      console.error('Delete failed:', error)
      alert('Failed to delete version')
    }
  }

  const formatBytes = (bytes: number): string => {
    if (bytes === 0) return '0 Bytes'
    const k = 1024
    const sizes = ['Bytes', 'KB', 'MB', 'GB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  }

  const getStatusIcon = (status: string) => {
    switch (status) {
      case 'completed':
        return <CheckCircle color="success" />
      case 'failed':
        return <Error color="error" />
      case 'downloading':
        return <CloudDownload color="primary" />
      default:
        return <Info color="info" />
    }
  }

  const isVersionDownloaded = (versionId: string) => {
    return downloadedVersions.some(v => v.id === versionId)
  }

  const isVersionDownloading = (versionId: string) => {
    const status = downloads[versionId]?.status
    return status === 'downloading' || status === 'pending'
  }

  if (loading) {
    return (
      <Box sx={{ p: 3 }}>
        <Typography>Loading offline storage...</Typography>
        <LinearProgress sx={{ mt: 2 }} />
      </Box>
    )
  }

  return (
    <Box sx={{ p: 3 }}>
      {/* Header */}
      <Box sx={{ mb: 3, display: 'flex', alignItems: 'center', gap: 2 }}>
        <Storage color="primary" />
        <Typography variant="h5">Offline Bible Storage</Typography>
        <Chip
          icon={isOnline ? <Wifi /> : <WifiOff />}
          label={isOnline ? 'Online' : 'Offline'}
          color={isOnline ? 'success' : 'error'}
          size="small"
        />
      </Box>

      {/* Downloaded Versions - Moved to top */}
      <Card sx={{ mb: 3 }}>
        <CardContent>
          <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
            <Typography variant="h6">
              Downloaded Versions ({downloadedVersions.length})
            </Typography>
            <Tooltip title="Refresh list">
              <IconButton onClick={loadDownloadedVersions} size="small">
                <Refresh />
              </IconButton>
            </Tooltip>
          </Box>
          {downloadedVersions.length === 0 ? (
            <Alert severity="info">
              No Bible versions downloaded yet. Download versions below to read offline.
            </Alert>
          ) : (
            <List dense>
              {downloadedVersions.map((version) => (
                <ListItem key={version.id}>
                  <ListItemText
                    primary={version.name}
                    secondary={
                      <Box>
                        <Typography variant="body2" color="text.secondary">
                          {version.abbreviation} - {version.language}
                        </Typography>
                        {version.downloadedAt && (
                          <Typography variant="caption" color="text.secondary">
                            Downloaded: {new Date(version.downloadedAt).toLocaleDateString()}
                          </Typography>
                        )}
                      </Box>
                    }
                  />
                  <ListItemSecondaryAction>
                    <Tooltip title="Delete from offline storage">
                      <IconButton
                        edge="end"
                        onClick={() => setConfirmDelete(version.id)}
                        color="error"
                        size="small"
                      >
                        <Delete />
                      </IconButton>
                    </Tooltip>
                  </ListItemSecondaryAction>
                </ListItem>
              ))}
            </List>
          )}
        </CardContent>
      </Card>

      {/* Storage Info */}
      <Card sx={{ mb: 3 }}>
        <CardContent>
          <Typography variant="h6" gutterBottom>
            Storage Usage
          </Typography>
          <Box sx={{ display: 'flex', alignItems: 'center', gap: 2, mb: 2 }}>
            <Box sx={{ flexGrow: 1 }}>
              <LinearProgress
                variant="determinate"
                value={storageInfo.percentage}
                sx={{ height: 8, borderRadius: 4 }}
              />
            </Box>
            <Typography variant="body2" color="text.secondary">
              {storageInfo.percentage.toFixed(1)}%
            </Typography>
          </Box>
          <Typography variant="body2" color="text.secondary">
            {formatBytes(storageInfo.used)} of {formatBytes(storageInfo.quota)} used
          </Typography>
          <Typography variant="body2" color="text.secondary">
            {downloadedVersions.length} Bible versions downloaded
          </Typography>
        </CardContent>
      </Card>

      {/* Active Downloads */}
      {Object.keys(downloads).length > 0 && (
        <Card sx={{ mb: 3 }}>
          <CardContent>
            <Typography variant="h6" gutterBottom>
              Downloads in Progress
            </Typography>
            <List dense>
              {Object.entries(downloads).map(([versionId, progress]) => {
                const version = availableVersions.find(v => v.id === versionId)
                return (
                  <ListItem key={versionId}>
                    <ListItemText
                      primary={
                        <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
                          {getStatusIcon(progress.status)}
                          <Typography>
                            {version?.name || versionId}
                          </Typography>
                        </Box>
                      }
                      secondary={
                        <Box sx={{ mt: 1 }}>
                          <LinearProgress
                            variant="determinate"
                            value={progress.progress}
                            sx={{ mb: 1 }}
                          />
                          <Typography variant="body2" color="text.secondary">
                            {progress.downloadedChapters} / {progress.totalChapters} chapters
                            ({progress.progress}%)
                          </Typography>
                        </Box>
                      }
                    />
                  </ListItem>
                )
              })}
            </List>
          </CardContent>
        </Card>
      )}

      {/* Available Versions for Download */}
      {isOnline && (
        <Card sx={{ mb: 3 }}>
          <CardContent>
            <Typography variant="h6" gutterBottom>
              Available for Download
            </Typography>
            <List dense>
              {availableVersions
                .filter(version => !isVersionDownloaded(version.id))
                .map((version) => {
                  const downloadProgress = downloads[version.id]
                  const isDownloading = isVersionDownloading(version.id)

                  return (
                    <ListItem key={version.id} sx={{ flexDirection: 'column', alignItems: 'stretch' }}>
                      <Box sx={{ display: 'flex', width: '100%', alignItems: 'center' }}>
                        <ListItemText
                          primary={version.name}
                          secondary={`${version.abbreviation} - ${version.language}`}
                        />
                        <Button
                          startIcon={isDownloading ? <CircularProgress size={16} /> : <Download />}
                          onClick={() => handleDownload(version)}
                          disabled={isDownloading}
                          variant="outlined"
                          size="small"
                          sx={{ ml: 2 }}
                        >
                          {isDownloading ? 'Downloading...' : 'Download'}
                        </Button>
                      </Box>
                      {isDownloading && downloadProgress && (
                        <Box sx={{ width: '100%', mt: 1 }}>
                          <LinearProgress
                            variant="determinate"
                            value={downloadProgress.progress}
                            sx={{ mb: 0.5 }}
                          />
                          <Typography variant="caption" color="text.secondary">
                            {downloadProgress.downloadedChapters} / {downloadProgress.totalChapters} chapters ({downloadProgress.progress}%)
                          </Typography>
                        </Box>
                      )}
                    </ListItem>
                  )
                })}
            </List>
            {availableVersions.filter(v => !isVersionDownloaded(v.id)).length === 0 && (
              <Typography color="text.secondary">
                All available versions are already downloaded
              </Typography>
            )}
          </CardContent>
        </Card>
      )}

      {/* Delete Confirmation Dialog */}
      <Dialog
        open={!!confirmDelete}
        onClose={() => setConfirmDelete(null)}
      >
        <DialogTitle>Delete Bible Version</DialogTitle>
        <DialogContent>
          <Typography>
            Are you sure you want to delete this Bible version from offline storage?
            You'll need to download it again to read offline.
          </Typography>
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setConfirmDelete(null)}>Cancel</Button>
          <Button
            onClick={() => confirmDelete && handleDelete(confirmDelete)}
            color="error"
            variant="contained"
          >
            Delete
          </Button>
        </DialogActions>
      </Dialog>
    </Box>
  )
}