import { useState } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { FiDroplet, FiStar, FiMap, FiSettings, FiChevronLeft, FiChevronRight, FiDatabase, FiCloudRain } from 'react-icons/fi';
import { TbRipple } from 'react-icons/tb';
import { type Language, t } from '../translations';
import { useFavorites } from '../hooks/useFavorites';

interface Props {
  language: Language;
  onOpenSettings: () => void;
  isMobileMenuOpen?: boolean;
  onCloseMobileMenu?: () => void;
}

const Sidebar = ({ language, onOpenSettings, isMobileMenuOpen, onCloseMobileMenu }: Props) => {
  const [isCollapsed, setIsCollapsed] = useState(false);
  const navigate = useNavigate();
  const location = useLocation();
  const dict = t[language].sidebar;
  const { favorites } = useFavorites();

  const isOverview = location.pathname === '/';
  const isFavoritesPage = location.pathname === '/favorites';
  const isRiversPage = location.pathname === '/rivers';
  const isMap = location.pathname === '/map';
  const isRadar = location.pathname === '/radar';

  const handleNavigate = (path: string) => {
    navigate(path);
    if (onCloseMobileMenu) onCloseMobileMenu();
  };

  return (
    <div className={`sidebar ${isCollapsed ? 'collapsed' : ''} ${isMobileMenuOpen ? 'mobile-open' : ''}`}>
      <div className="sidebar-logo" style={{ alignItems: 'center', gap: '0.4rem' }}>
        <FiDroplet size={34} color="var(--color-cyan)" style={{ marginLeft: '-4px', flexShrink: 0 }} />
        <div className="sidebar-text" style={{ position: 'relative', alignItems: 'center' }}>
          <span style={{ fontWeight: 'bold', letterSpacing: '0.5px', fontSize: '1.15rem', lineHeight: 1 }}>HLADINATOR</span>
          <small style={{ position: 'absolute', top: '100%', left: '2px', marginTop: '6px', lineHeight: 1, fontSize: '0.75rem', fontWeight: 'bold', color: 'var(--text-muted)' }}>v1.0</small>
        </div>
      </div>

      {/* Toggle Button */}
      <div style={{ display: 'flex', justifyContent: isCollapsed ? 'center' : 'flex-end', marginBottom: '0.5rem', marginTop: '-1.5rem' }}>
        <button
          onClick={() => setIsCollapsed(!isCollapsed)}
          style={{
            background: 'var(--bg-card)', border: '1px solid var(--border-color)', color: 'var(--text-main)',
            borderRadius: '50%', width: '32px', height: '32px', display: 'flex', alignItems: 'center', justifyContent: 'center',
            cursor: 'pointer', zIndex: 10, boxShadow: '0 2px 5px rgba(0,0,0,0.2)'
          }}
        >
          {isCollapsed ? <FiChevronRight size={18} /> : <FiChevronLeft size={18} />}
        </button>
      </div>

      <div className="nav-links">
        {/* Favourites */}
        <div className={`nav-item ${isFavoritesPage ? 'active' : ''}`} onClick={() => handleNavigate('/favorites')} style={{ position: 'relative' }}>
          <div style={{ position: 'relative', display: 'flex' }}>
            <FiStar fill={favorites.length > 0 ? '#f59e0b' : 'none'} color={favorites.length > 0 ? '#f59e0b' : 'currentColor'} />
            {favorites.length > 0 && (
              <span style={{
                position: 'absolute',
                top: '-8px',
                right: '-12px',
                backgroundColor: '#f59e0b',
                color: '#000',
                borderRadius: '999px',
                fontSize: '0.65rem',
                fontWeight: 'bold',
                minWidth: '16px',
                height: '16px',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                padding: '0 4px',
                border: '2px solid var(--bg-card)'
              }}>
                {favorites.length}
              </span>
            )}
          </div>
          <span className="sidebar-text">{dict.favorites}</span>
        </div>

        {/* Lakes & Reservoirs */}
        <div className={`nav-item ${isOverview ? 'active' : ''}`} onClick={() => handleNavigate('/')}>
          <FiDatabase />
          <span className="sidebar-text">{dict.lakes}</span>
        </div>

        {/* Rivers & Streams */}
        <div className={`nav-item ${isRiversPage ? 'active' : ''}`} onClick={() => handleNavigate('/rivers')}>
          <TbRipple size={18} />
          <span className="sidebar-text">{dict.rivers}</span>
        </div>

        {/* Map */}
        <div className={`nav-item ${isMap ? 'active' : ''}`} onClick={() => handleNavigate('/map')}>
          <FiMap />
          <span className="sidebar-text">{dict.map}</span>
        </div>

        {/* Radar */}
        <div className={`nav-item ${isRadar ? 'active' : ''}`} onClick={() => handleNavigate('/radar')}>
          <FiCloudRain />
          <span className="sidebar-text">{dict.radar}</span>
        </div>
      </div>

      <div className="sidebar-footer">
        <div className="nav-item" onClick={onOpenSettings}>
          <FiSettings />
          <span className="sidebar-text">{dict.settings}</span>
        </div>
      </div>
    </div>
  );
};

export default Sidebar;
