import { useAppState, type LoreEntry } from "../contexts/state";
import styles from '../assets/lore-editor.module.css';
import { ContentEditable } from "@common/components/ContentEditable";
import { useState } from "preact/hooks";

export const LoreEditor = () => {
    const { currentStory, dispatch } = useAppState();
    const [editingId, setEditingId] = useState<string | null>(null);
    const [newTitle, setNewTitle] = useState('');

    if (!currentStory) {
        return null;
    }

    const handleAddEntry = () => {
        if (!newTitle.trim()) return;

        dispatch({
            type: 'ADD_LORE_ENTRY',
            storyId: currentStory.id,
            entry: {
                id: crypto.randomUUID(),
                title: newTitle.trim(),
                text: '',
            },
        });
        setNewTitle('');
        setEditingId(null);
    };

    const handleEditEntry = (entryId: string, field: keyof LoreEntry, value: string) => {
        dispatch({
            type: 'EDIT_LORE_ENTRY',
            storyId: currentStory.id,
            entryId,
            updates: { [field]: value },
        });
    };

    const handleDeleteEntry = (entryId: string) => {
        dispatch({
            type: 'DELETE_LORE_ENTRY',
            storyId: currentStory.id,
            entryId,
        });
    };

    const handleMoveUp = (index: number) => {
        if (index === 0) return;
        const entryIds = currentStory.lore.map(e => e.id);
        [entryIds[index - 1], entryIds[index]] = [entryIds[index], entryIds[index - 1]];
        dispatch({
            type: 'REORDER_LORE_ENTRIES',
            storyId: currentStory.id,
            entryIds,
        });
    };

    const handleMoveDown = (index: number) => {
        if (index === currentStory.lore.length - 1) return;
        const entryIds = currentStory.lore.map(e => e.id);
        [entryIds[index], entryIds[index + 1]] = [entryIds[index + 1], entryIds[index]];
        dispatch({
            type: 'REORDER_LORE_ENTRIES',
            storyId: currentStory.id,
            entryIds,
        });
    };

    return (
        <div class={styles.loreEditor}>
            <div class={styles.header}>
                <h2>Lore</h2>
                <div class={styles.addEntry}>
                    <input
                        type="text"
                        class={styles.titleInput}
                        value={newTitle}
                        onInput={(e) => setNewTitle(e.currentTarget.value)}
                        onKeyDown={(e) => {
                            if (e.key === 'Enter') {
                                e.preventDefault();
                                handleAddEntry();
                            }
                        }}
                        placeholder="New lore entry title..."
                    />
                    <button class={styles.addButton} onClick={handleAddEntry}>
                        + Add Entry
                    </button>
                </div>
            </div>

            <div class={styles.list}>
                {currentStory.lore.length === 0 && (
                    <p class={styles.empty}>No lore entries yet. Add your first entry!</p>
                )}

                {currentStory.lore.map((entry, index) => (
                    <div key={entry.id} class={styles.entryCard}>
                        <div class={styles.cardHeader}>
                            <div class={styles.titleRow}>
                                {editingId === entry.id ? (
                                    <input
                                        type="text"
                                        class={styles.titleEditInput}
                                        value={entry.title}
                                        onInput={(e) => handleEditEntry(entry.id, 'title', e.currentTarget.value)}
                                        onBlur={() => setEditingId(null)}
                                        onKeyDown={(e) => {
                                            if (e.key === 'Enter') {
                                                e.preventDefault();
                                                setEditingId(null);
                                            } else if (e.key === 'Escape') {
                                                setEditingId(null);
                                            }
                                        }}
                                        autoFocus
                                    />
                                ) : (
                                    <h3
                                        class={styles.entryTitle}
                                        onClick={() => setEditingId(entry.id)}
                                        title="Click to edit"
                                    >
                                        {entry.title}
                                    </h3>
                                )}
                            </div>
                            <div class={styles.actions}>
                                <button
                                    class={styles.moveButton}
                                    onClick={() => handleMoveUp(index)}
                                    disabled={index === 0}
                                    title="Move up"
                                >
                                    ↑
                                </button>
                                <button
                                    class={styles.moveButton}
                                    onClick={() => handleMoveDown(index)}
                                    disabled={index === currentStory.lore.length - 1}
                                    title="Move down"
                                >
                                    ↓
                                </button>
                                <button
                                    class={styles.deleteButton}
                                    onClick={() => handleDeleteEntry(entry.id)}
                                    title="Delete"
                                >
                                    ×
                                </button>
                            </div>
                        </div>

                        <div class={styles.content}>
                            <ContentEditable
                                autoLines
                                class={styles.textarea}
                                value={entry.text}
                                onInput={(e) => handleEditEntry(entry.id, 'text', e.currentTarget.textContent || '')}
                                placeholder="Enter lore content..."
                            />
                        </div>
                    </div>
                ))}
            </div>
        </div>
    );
};
