import React, { useState, useEffect, useRef } from 'react'
import { useAuth } from './auth/AuthContext'
import LoadingError from './LoadingError'
import './Wishlist.css'

const API_URL = '/api/wishlist'
const CACHE_KEY = 'wishlist_cache'
const CACHE_COMPLETED_KEY = 'wishlist_completed_cache'

function Wishlist({ onNavigate, refreshTrigger = 0, isActive = false }) {
  const { authFetch } = useAuth()
  const [items, setItems] = useState([])
  const [completed, setCompleted] = useState([])
  const [completedCount, setCompletedCount] = useState(0)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState('')
  const [completedExpanded, setCompletedExpanded] = useState(false)
  const [completedLoading, setCompletedLoading] = useState(false)
  const [selectedItem, setSelectedItem] = useState(null)
  const fetchingRef = useRef(false)
  const fetchingCompletedRef = useRef(false)
  const initialFetchDoneRef = useRef(false)
  const prevIsActiveRef = useRef(isActive)

  // Проверка наличия кэша
  const hasCache = () => {
    try {
      return localStorage.getItem(CACHE_KEY) !== null
    } catch (err) {
      return false
    }
  }

  // Загрузка основных данных из кэша
  const loadFromCache = () => {
    try {
      const cached = localStorage.getItem(CACHE_KEY)
      if (cached) {
        const data = JSON.parse(cached)
        setItems(data.items || [])
        setCompletedCount(data.completedCount || 0)
        return true
      }
    } catch (err) {
      console.error('Error loading from cache:', err)
    }
    return false
  }

  // Загрузка завершённых из кэша
  const loadCompletedFromCache = () => {
    try {
      const cached = localStorage.getItem(CACHE_COMPLETED_KEY)
      if (cached) {
        const data = JSON.parse(cached)
        setCompleted(data || [])
        return true
      }
    } catch (err) {
      console.error('Error loading completed from cache:', err)
    }
    return false
  }

  // Сохранение основных данных в кэш
  const saveToCache = (itemsData, count) => {
    try {
      localStorage.setItem(CACHE_KEY, JSON.stringify({
        items: itemsData,
        completedCount: count,
        timestamp: Date.now()
      }))
    } catch (err) {
      console.error('Error saving to cache:', err)
    }
  }

  // Сохранение завершённых в кэш
  const saveCompletedToCache = (completedData) => {
    try {
      localStorage.setItem(CACHE_COMPLETED_KEY, JSON.stringify(completedData))
    } catch (err) {
      console.error('Error saving completed to cache:', err)
    }
  }

  // Загрузка основного списка
  const fetchWishlist = async () => {
    if (fetchingRef.current) return
    fetchingRef.current = true
    
    try {
      const hasDataInState = items.length > 0 || completedCount > 0
      const cacheExists = hasCache()
      if (!hasDataInState && !cacheExists) {
        setLoading(true)
      }
      
      const response = await authFetch(API_URL)
      
      if (!response.ok) {
        throw new Error('Ошибка при загрузке желаний')
      }
      
      const data = await response.json()
      const allItems = [...(data.unlocked || []), ...(data.locked || [])]
      const count = data.completed_count || 0
      
      setItems(allItems)
      setCompletedCount(count)
      saveToCache(allItems, count)
      setError('')
    } catch (err) {
      setError(err.message)
      if (!hasCache()) {
        setItems([])
        setCompletedCount(0)
      }
    } finally {
      setLoading(false)
      fetchingRef.current = false
    }
  }

  // Загрузка завершённых
  const fetchCompleted = async () => {
    if (fetchingCompletedRef.current) return
    fetchingCompletedRef.current = true
    
    try {
      setCompletedLoading(true)
      const response = await authFetch(`${API_URL}/completed`)
      
      if (!response.ok) {
        throw new Error('Ошибка при загрузке завершённых желаний')
      }
      
      const data = await response.json()
      const completedData = Array.isArray(data) ? data : []
      setCompleted(completedData)
      saveCompletedToCache(completedData)
    } catch (err) {
      console.error('Error fetching completed items:', err)
      setCompleted([])
    } finally {
      setCompletedLoading(false)
      fetchingCompletedRef.current = false
    }
  }

  // Первая инициализация
  useEffect(() => {
    if (!initialFetchDoneRef.current) {
      initialFetchDoneRef.current = true
      
      // Загружаем из кэша
      const cacheLoaded = loadFromCache()
      if (cacheLoaded) {
        setLoading(false)
      }
      
      // Загружаем свежие данные
      fetchWishlist()
      
      // Если список завершённых раскрыт - загружаем их тоже
      if (completedExpanded) {
        loadCompletedFromCache()
        fetchCompleted()
      }
    }
  }, [])

  // Обработка активации/деактивации таба
  useEffect(() => {
    const wasActive = prevIsActiveRef.current
    prevIsActiveRef.current = isActive
    
    // Пропускаем первую инициализацию (она обрабатывается отдельно)
    if (!initialFetchDoneRef.current) return
    
    // Когда таб становится видимым
    if (isActive && !wasActive) {
      // Показываем кэш, если есть данные
      const hasDataInState = items.length > 0 || completedCount > 0
      if (!hasDataInState) {
        const cacheLoaded = loadFromCache()
        if (cacheLoaded) {
          setLoading(false)
        } else {
          setLoading(true)
        }
      }
      
      // Всегда загружаем свежие данные основного списка
      fetchWishlist()
      
      // Если список завершённых раскрыт - загружаем их тоже
      if (completedExpanded && completedCount > 0) {
        fetchCompleted()
      }
    }
  }, [isActive])

  // Обновляем данные при изменении refreshTrigger
  useEffect(() => {
    if (refreshTrigger > 0) {
      fetchWishlist()
      if (completedExpanded && completedCount > 0) {
        fetchCompleted()
      }
    }
  }, [refreshTrigger])

  const handleToggleCompleted = () => {
    const newExpanded = !completedExpanded
    setCompletedExpanded(newExpanded)
    
    // При раскрытии загружаем завершённые
    if (newExpanded && completedCount > 0) {
      // Показываем из кэша если есть
      if (completed.length === 0) {
        loadCompletedFromCache()
      }
      // Загружаем свежие данные
      fetchCompleted()
    }
  }

  const handleAddClick = () => {
    onNavigate?.('wishlist-form', { wishlistId: undefined })
  }

  const handleItemClick = (item) => {
    onNavigate?.('wishlist-detail', { wishlistId: item.id })
  }

  const handleMenuClick = (item, e) => {
    e.stopPropagation()
    setSelectedItem(item)
  }

  const handleEdit = () => {
    if (selectedItem) {
      onNavigate?.('wishlist-form', { wishlistId: selectedItem.id })
      setSelectedItem(null)
    }
  }

  const handleDelete = async () => {
    if (!selectedItem) return

    try {
      const response = await authFetch(`${API_URL}/${selectedItem.id}`, {
        method: 'DELETE',
      })

      if (!response.ok) {
        throw new Error('Ошибка при удалении')
      }

      setSelectedItem(null)
      await fetchWishlist()
      if (completedExpanded) {
        await fetchCompleted()
      }
    } catch (err) {
      setError(err.message)
      setSelectedItem(null)
    }
  }

  const handleComplete = async () => {
    if (!selectedItem) return

    try {
      const response = await authFetch(`${API_URL}/${selectedItem.id}/complete`, {
        method: 'POST',
      })

      if (!response.ok) {
        throw new Error('Ошибка при завершении')
      }

      setSelectedItem(null)
      await fetchWishlist()
      if (completedExpanded) {
        await fetchCompleted()
      }
    } catch (err) {
      setError(err.message)
      setSelectedItem(null)
    }
  }

  const handleCopy = async () => {
    if (!selectedItem) return

    try {
      const response = await authFetch(`${API_URL}/${selectedItem.id}/copy`, {
        method: 'POST',
      })

      if (!response.ok) {
        throw new Error('Ошибка при копировании')
      }

      const newItem = await response.json()

      setSelectedItem(null)
      onNavigate?.('wishlist-form', { wishlistId: newItem.id })
    } catch (err) {
      setError(err.message)
      setSelectedItem(null)
    }
  }

  const formatPrice = (price) => {
    return new Intl.NumberFormat('ru-RU', {
      style: 'currency',
      currency: 'RUB',
      minimumFractionDigits: 0,
      maximumFractionDigits: 0,
    }).format(price)
  }

  const findFirstUnmetCondition = (item) => {
    if (!item.unlock_conditions || item.unlock_conditions.length === 0) {
      return null
    }

    for (const condition of item.unlock_conditions) {
      let isMet = false

      if (condition.type === 'task_completion') {
        isMet = condition.task_completed === true
      } else if (condition.type === 'project_points') {
        const currentPoints = condition.current_points || 0
        const requiredPoints = condition.required_points || 0
        isMet = currentPoints >= requiredPoints
      }

      if (!isMet) {
        return condition
      }
    }

    return null
  }

  const renderUnlockCondition = (item) => {
    if (item.completed) return null

    const condition = findFirstUnmetCondition(item)
    if (!condition) return null

    let conditionText = ''
    if (condition.type === 'task_completion') {
      conditionText = condition.task_name || 'Задача'
    } else {
      const points = condition.required_points || 0
      const project = condition.project_name || 'Проект'
      let dateText = ''
      if (condition.start_date) {
        const date = new Date(condition.start_date + 'T00:00:00')
        dateText = ` с ${date.toLocaleDateString('ru-RU')}`
      } else {
        dateText = ' за всё время'
      }
      conditionText = `${points} в ${project}${dateText}`
    }

    return (
      <div className="unlock-condition-wrapper">
        <div className="unlock-condition-line">
          <div className="unlock-condition">
            <svg className="lock-icon" width="12" height="12" viewBox="0 0 24 24" fill="currentColor">
              <path d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zM9 6c0-1.66 1.34-3 3-3s3 1.34 3 3v2H9V6zm9 14H6V10h12v10zm-6-3c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2z"/>
            </svg>
            <span className="condition-text">{conditionText}</span>
          </div>
        </div>
      </div>
    )
  }

  const renderItem = (item) => {
    const isFaded = (!item.unlocked && !item.completed) || item.completed

    return (
      <div
        key={item.id}
        className={`wishlist-card ${isFaded ? 'faded' : ''}`}
        onClick={() => handleItemClick(item)}
      >
        <button
          className="card-menu-button"
          onClick={(e) => handleMenuClick(item, e)}
          title="Меню"
        >
          ⋮
        </button>

        <div className="card-image">
          {item.image_url ? (
            <img src={item.image_url} alt={item.name} />
          ) : (
            <div className="placeholder">
              <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                <rect x="3" y="3" width="18" height="18" rx="2" ry="2"/>
                <circle cx="8.5" cy="8.5" r="1.5"/>
                <polyline points="21 15 16 10 5 21"/>
              </svg>
            </div>
          )}
        </div>

        <div className="card-name">{item.name}</div>

        {(() => {
          const unmetCondition = findFirstUnmetCondition(item)
          if (unmetCondition && !item.completed) {
            return renderUnlockCondition(item)
          }
          if (item.price) {
            return <div className="card-price">{formatPrice(item.price)}</div>
          }
          return null
        })()}
      </div>
    )
  }

  if (loading) {
    return (
      <div className="wishlist">
        <div className="fixed inset-0 bottom-20 flex justify-center items-center">
          <div className="flex flex-col items-center">
            <div className="w-12 h-12 border-4 border-indigo-200 border-t-indigo-600 rounded-full animate-spin mb-4"></div>
            <div className="text-gray-600 font-medium">Загрузка...</div>
          </div>
        </div>
      </div>
    )
  }

  if (error) {
    return (
      <div className="wishlist">
        <LoadingError onRetry={() => fetchWishlist()} />
      </div>
    )
  }

  return (
    <div className="wishlist">
      {/* Основной список (разблокированные и заблокированные вместе) */}
      <div className="wishlist-grid">
        {items.map(renderItem)}
        <button 
          onClick={handleAddClick} 
          className="add-wishlist-button"
        >
          <div className="add-wishlist-icon">+</div>
        </button>
      </div>

      {/* Завершённые - показываем только если есть завершённые желания */}
      {completedCount > 0 && (
        <>
          <div className="section-divider">
            <button
              className="completed-toggle"
              onClick={handleToggleCompleted}
            >
              <span className="completed-toggle-icon">
                {completedExpanded ? '▼' : '▶'}
              </span>
              <span>Завершённые</span>
            </button>
          </div>
          {completedExpanded && (
            <>
              {completedLoading ? (
                <div className="loading-completed">
                  <div className="w-8 h-8 border-4 border-indigo-200 border-t-indigo-600 rounded-full animate-spin"></div>
                </div>
              ) : (
                <div className="wishlist-grid">
                  {completed.map(renderItem)}
                </div>
              )}
            </>
          )}
        </>
      )}

      {/* Модальное окно для действий */}
      {selectedItem && (
        <div className="wishlist-modal-overlay" onClick={() => setSelectedItem(null)}>
          <div className="wishlist-modal" onClick={(e) => e.stopPropagation()}>
            <div className="wishlist-modal-header">
              <h3>{selectedItem.name}</h3>
            </div>
            <div className="wishlist-modal-actions">
              <button className="wishlist-modal-edit" onClick={handleEdit}>
                Редактировать
              </button>
              <button className="wishlist-modal-copy" onClick={handleCopy}>
                Копировать
              </button>
              <button className="wishlist-modal-delete" onClick={handleDelete}>
                Удалить
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  )
}

export default Wishlist

