'use client';
import React, {useState} from 'react';
import {WorldProps} from '@/lib/models/World';
import InputField from '@/components/form/InputField';
import TextInput from '@/components/form/TextInput';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import {faChevronRight, faGlobe, faPlus} from '@fortawesome/free-solid-svg-icons';
import {useTranslations} from 'next-intl';

interface WorldEditorListProps {
    worlds: WorldProps[];
    onWorldClick: (world: WorldProps) => void;
    onAddWorld: () => void;
}

/**
 * WorldEditorList - Liste des mondes pour ComposerRightBar
 * Version compacte avec liste cliquable (même pattern que CharacterEditorList)
 * PAS de scroll interne (géré par parent ComposerRightBar)
 */
export default function WorldEditorList({
    worlds,
    onWorldClick,
    onAddWorld,
}: WorldEditorListProps): React.JSX.Element {
    const t = useTranslations();
    const [searchQuery, setSearchQuery] = useState<string>('');

    function getFilteredWorlds(): WorldProps[] {
        return worlds.filter(function (world: WorldProps): boolean {
            return world.name.toLowerCase().includes(searchQuery.toLowerCase());
        });
    }

    const filteredWorlds: WorldProps[] = getFilteredWorlds();

    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('worldSetting.search')}
                        />
                    }
                    actionIcon={faPlus}
                    actionLabel={t('worldSetting.addWorldLabel')}
                    addButtonCallBack={async function (): Promise<void> {
                        onAddWorld();
                    }}
                />
            </div>

            <div className="px-2 space-y-2">
                {filteredWorlds.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={faGlobe} className="text-primary w-8 h-8"/>
                        </div>
                        <h3 className="text-text-primary font-semibold text-base mb-1">
                            {t('worldSetting.noWorldAvailable')}
                        </h3>
                        <p className="text-muted text-sm max-w-xs">
                            {t('worldSetting.noWorldDescription')}
                        </p>
                    </div>
                ) : (
                    filteredWorlds.map(function (world: WorldProps): React.JSX.Element {
                        return (
                            <div
                                key={world.id}
                                onClick={function (): void { onWorldClick(world); }}
                                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={faGlobe} 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">
                                        {world.name}
                                    </div>
                                    {world.history && (
                                        <div className="text-muted text-xs truncate">
                                            {world.history.substring(0, 50)}{world.history.length > 50 ? '...' : ''}
                                        </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>
    );
}
