'use client';
import React, {useState} from 'react';
import {SpellListItem, SpellTagProps} from '@/lib/models/Spell';
import InputField from '@/components/form/InputField';
import TextInput from '@/components/form/TextInput';
import SpellTagChip from '@/components/book/settings/spells/SpellTagChip';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import {faChevronRight, faCog, faHatWizard, faPlus} from '@fortawesome/free-solid-svg-icons';
import {useTranslations} from 'next-intl';

interface SpellSettingsListProps {
    spells: SpellListItem[];
    tags: SpellTagProps[];
    onSpellClick: (spell: SpellListItem) => void;
    onAddSpell: () => void;
    onManageTags: () => void;
}

/**
 * SpellSettingsList - Liste des sorts pour BookSetting/SerieSetting
 * Inclut recherche, filtre par tag, et gestion des tags
 * PAS de scroll interne (géré par parent)
 */
export default function SpellSettingsList({
    spells,
    tags,
    onSpellClick,
    onAddSpell,
    onManageTags,
}: SpellSettingsListProps): React.JSX.Element {
    const t = useTranslations();
    const [searchQuery, setSearchQuery] = useState<string>('');
    const [filterTag, setFilterTag] = useState<string>('all');

    function getFilteredSpells(): SpellListItem[] {
        return spells.filter(function (spell: SpellListItem): boolean {
            const matchesSearch: boolean = spell.name.toLowerCase().includes(searchQuery.toLowerCase());
            const matchesTag: boolean = filterTag === 'all' || spell.tags.some(function (tag: SpellTagProps): boolean {
                return tag.id === filterTag;
            });
            return matchesSearch && matchesTag;
        });
    }

    const filteredSpells: SpellListItem[] = getFilteredSpells();

    return (
        <div className="space-y-4">
            <div className="px-4 space-y-3">
                <InputField
                    input={
                        <TextInput
                            value={searchQuery}
                            setValue={function (e: React.ChangeEvent<HTMLInputElement>): void {
                                setSearchQuery(e.target.value);
                            }}
                            placeholder={t('spellList.search')}
                        />
                    }
                    actionIcon={faPlus}
                    actionLabel={t('spellList.add')}
                    addButtonCallBack={async function (): Promise<void> {
                        onAddSpell();
                    }}
                />

                <div className="flex flex-wrap gap-3 items-center">
                    <div className="flex-1 min-w-[150px]">
                        <select
                            value={filterTag}
                            onChange={function (e: React.ChangeEvent<HTMLSelectElement>): void {
                                setFilterTag(e.target.value);
                            }}
                            className="w-full text-text-primary bg-secondary/50 hover:bg-secondary px-4 py-2.5 rounded-xl border border-secondary/50 focus:border-primary focus:ring-4 focus:ring-primary/20 focus:bg-secondary hover:border-secondary outline-none transition-all duration-200 cursor-pointer"
                        >
                            <option value="all" className="bg-tertiary text-text-primary">
                                {t('spellList.allTags')}
                            </option>
                            {tags.map(function (tag: SpellTagProps): React.JSX.Element {
                                return (
                                    <option key={tag.id} value={tag.id} className="bg-tertiary text-text-primary">
                                        {tag.name}
                                    </option>
                                );
                            })}
                        </select>
                    </div>
                    <button
                        onClick={onManageTags}
                        className="flex items-center gap-2 px-4 py-2.5 bg-secondary/50 rounded-xl border border-secondary/50 hover:bg-secondary hover:border-secondary hover:shadow-md hover:scale-105 transition-all duration-200"
                    >
                        <FontAwesomeIcon icon={faCog} className="text-primary w-4 h-4"/>
                        <span className="text-text-primary text-sm font-medium">{t('spellList.manageTags')}</span>
                    </button>
                </div>
            </div>

            <div className="px-2">
                {filteredSpells.length === 0 ? (
                    <div className="flex flex-col items-center justify-center py-12 text-center">
                        <div className="w-20 h-20 rounded-full bg-primary/10 flex items-center justify-center mb-4">
                            <FontAwesomeIcon icon={faHatWizard} className="text-primary w-10 h-10"/>
                        </div>
                        <h3 className="text-text-primary font-semibold text-lg mb-2">
                            {t('spellList.noSpells')}
                        </h3>
                        <p className="text-muted text-sm max-w-xs">
                            {t('spellList.noSpellsDescription')}
                        </p>
                    </div>
                ) : (
                    <div className="space-y-2 p-2">
                        {filteredSpells.map(function (spell: SpellListItem): React.JSX.Element {
                            return (
                                <div
                                    key={spell.id}
                                    onClick={function (): void { onSpellClick(spell); }}
                                    className="group flex items-center p-4 bg-secondary/30 rounded-xl border-l-4 border-primary border border-secondary/50 cursor-pointer hover:bg-secondary hover:shadow-md hover:scale-102 transition-all duration-200 hover:border-primary/50"
                                >
                                    <div className="w-12 h-12 rounded-full border-2 border-primary overflow-hidden bg-secondary shadow-md group-hover:scale-110 transition-transform flex items-center justify-center">
                                        <FontAwesomeIcon icon={faHatWizard} className="text-primary w-6 h-6"/>
                                    </div>

                                    <div className="ml-4 flex-1 min-w-0">
                                        <div className="text-text-primary font-bold text-base group-hover:text-primary transition-colors">
                                            {spell.name}
                                        </div>
                                        <div className="text-text-secondary text-sm mt-0.5 truncate">
                                            {spell.description}
                                        </div>
                                        {spell.tags.length > 0 && (
                                            <div className="flex flex-wrap gap-1.5 mt-2">
                                                {spell.tags.slice(0, 3).map(function (tag: SpellTagProps): React.JSX.Element {
                                                    return <SpellTagChip key={tag.id} tag={tag} size="sm"/>;
                                                })}
                                                {spell.tags.length > 3 && (
                                                    <span className="text-muted text-xs px-2 py-0.5 bg-secondary/50 rounded-full">
                                                        +{spell.tags.length - 3}
                                                    </span>
                                                )}
                                            </div>
                                        )}
                                    </div>

                                    <div className="w-8 flex justify-center">
                                        <FontAwesomeIcon
                                            icon={faChevronRight}
                                            className="text-muted group-hover:text-primary group-hover:translate-x-1 transition-all w-4 h-4"
                                        />
                                    </div>
                                </div>
                            );
                        })}
                    </div>
                )}
            </div>
        </div>
    );
}
