import React, { useMemo, useState } from 'react';
import * as LucideIcons from 'lucide-react';
import Button from './Button';

export default function IconSelector({
  isOpen,
  onClose,
  onSelect,
  selectedIcon = '',
}) {
  const [searchTerm, setSearchTerm] = useState('');

  const excludedKeys = new Set([
    'Icon',
    'DynamicIcon',
    'createLucideIcon',
    'default',
    'icons',
  ]);

  const allIcons = Object.keys(LucideIcons).filter((key) => {
    // Exclure les utilitaires
    if (excludedKeys.has(key)) return false;
    return true;
  });

  const filteredIcons = useMemo(() => {
    if (!searchTerm) return allIcons;
    return allIcons.filter((iconName) =>
      iconName.toLowerCase().includes(searchTerm.toLowerCase())
    );
  }, [searchTerm, allIcons]);

  if (!isOpen) return null;

  const selectIcon = (iconName) => {
    onSelect(iconName);
    onClose();
  };

  return (
    <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
      <div className="bg-white rounded-lg p-6 max-w-4xl w-full mx-4 max-h-[85vh] overflow-y-auto">
        <div className="flex justify-between items-center mb-4">
          <h3 className="font-headline text-lg font-semibold">
            Choisir une icône ({filteredIcons.length} / {allIcons.length}{' '}
            icônes)
          </h3>
          <button
            onClick={onClose}
            className="text-gray-500 hover:text-gray-700 text-xl"
          >
            ✕
          </button>
        </div>

        {/* Barre de recherche */}
        <div className="mb-6">
          <div className="relative">
            <input
              type="text"
              placeholder="Rechercher une icône..."
              value={searchTerm}
              onChange={(e) => setSearchTerm(e.target.value)}
              className="w-full px-4 py-3 pl-10 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
            />
            <LucideIcons.Search
              className="absolute left-3 top-3.5 text-gray-400"
              size={18}
            />
            {searchTerm && (
              <button
                onClick={() => setSearchTerm('')}
                className="absolute right-3 top-3.5 text-gray-400 hover:text-gray-600"
              >
                <LucideIcons.X size={18} />
              </button>
            )}
          </div>
        </div>

        <div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-4">
          {filteredIcons.map((iconName) => {
            try {
              const IconComponent = LucideIcons[iconName];
              return (
                <button
                  key={iconName}
                  onClick={() => selectIcon(iconName)}
                  className={`
                    p-5 rounded-lg border-2 transition-all duration-200
                    hover:bg-blue-50 hover:border-blue-300 hover:shadow-md hover:scale-105
                    flex flex-col items-center justify-center gap-4 min-h-[140px] w-full
                    ${
                      selectedIcon === iconName
                        ? 'bg-blue-100 border-blue-500 shadow-md scale-105'
                        : 'bg-gray-50 border-gray-200'
                    }
                  `}
                  title={iconName}
                >
                  <IconComponent
                    size={32}
                    className="text-gray-700 flex-shrink-0"
                  />
                  <span className="text-xs text-gray-600 text-center leading-tight break-words px-1 overflow-hidden max-w-full">
                    {iconName}
                  </span>
                </button>
              );
            } catch (error) {
              // En cas d'erreur avec une icône spécifique, ne pas la rendre
              return null;
            }
          })}
        </div>

        <div className="mt-6 flex justify-between items-center">
          <p className="text-sm text-gray-500">
            {searchTerm ? (
              <>
                {filteredIcons.length} icône(s) trouvée(s) sur {allIcons.length}{' '}
                disponibles
              </>
            ) : (
              <>Total : {allIcons.length} icônes disponibles</>
            )}
          </p>
          <div className="flex gap-2">
            <Button
              text="Aucune icône"
              onClick={() => selectIcon('')}
              className="px-4 py-2 bg-gray-500 text-white rounded-md hover:bg-gray-600"
            />
            <Button
              text="Annuler"
              onClick={onClose}
              className="px-4 py-2 bg-red-500 text-white rounded-md hover:bg-red-600"
            />
          </div>
        </div>
      </div>
    </div>
  );
}
