'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, faHatWizard, faPlus, faTags} from '@fortawesome/free-solid-svg-icons';
import {useTranslations} from 'next-intl';

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

/**
 * SpellEditorList - Liste compacte pour ComposerRightBar
 * Mêmes fonctionnalités que SpellSettingsList, layout condensé
 */
export default function SpellEditorList({
    spells,
    tags,
    onSpellClick,
    onAddSpell,
    onManageTags,
}: SpellEditorListProps): React.JSX.Element {
    const t = useTranslations();
    const [searchQuery, setSearchQuery] = useState<string>('');
    const [selectedTagId, setSelectedTagId] = useState<string | null>(null);

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

    const filteredSpells: SpellListItem[] = getFilteredSpells();

    return (
        <div className="space-y-3">
            <div className="px-2">
                <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>

            {/* Tag filter + manage button */}
            <div className="px-2 flex items-center gap-2 flex-wrap">
                <button
                    onClick={function (): void { setSelectedTagId(null); }}
                    className={`px-2 py-1 text-xs rounded-full transition-colors ${
                        selectedTagId === null
                            ? 'bg-primary text-white'
                            : 'bg-secondary/50 text-text-secondary hover:bg-secondary'
                    }`}
                >
                    {t('spellList.allTags')}
                </button>
                {tags.slice(0, 4).map(function (tag: SpellTagProps): React.JSX.Element {
                    return (
                        <button
                            key={tag.id}
                            onClick={function (): void { setSelectedTagId(tag.id === selectedTagId ? null : tag.id); }}
                            className={`px-2 py-1 text-xs rounded-full transition-colors ${
                                selectedTagId === tag.id
                                    ? 'bg-primary text-white'
                                    : 'bg-secondary/50 text-text-secondary hover:bg-secondary'
                            }`}
                            style={selectedTagId === tag.id ? {} : {borderLeft: `3px solid ${tag.color}`}}
                        >
                            {tag.name}
                        </button>
                    );
                })}
                <button
                    onClick={onManageTags}
                    className="px-2 py-1 text-xs rounded-full bg-secondary/30 text-text-secondary hover:bg-secondary transition-colors flex items-center gap-1"
                >
                    <FontAwesomeIcon icon={faTags} className="w-3 h-3"/>
                    {t('spellList.manageTags')}
                </button>
            </div>

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

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

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