import React, { useState, useEffect } from 'react';
import {
    Bold,
    Italic,
    Heading1,
    Heading2,
    List,
    ListOrdered,
    Link as LinkIcon,
    Quote,
    Code,
    Code2,
    Eye
} from 'lucide-react';

interface DarkRichTextEditorProps {
    value: string;
    onChange: (value: string) => void;
    placeholder?: string;
}

export function DarkRichTextEditor({ value, onChange, placeholder }: DarkRichTextEditorProps) {
    const [editorMode, setEditorMode] = useState<'visual' | 'code'>('visual');
    const [content, setContent] = useState(value || '');

    useEffect(() => {
        setContent(value || '');
    }, [value]);

    const handleContentChange = (newVal: string) => {
        setContent(newVal);
        onChange(newVal);
    };

    const insertTag = (tag: string, endTag: string = '') => {
        const textarea = document.getElementById('dark-editor-textarea') as HTMLTextAreaElement;
        if (!textarea) return;

        const start = textarea.selectionStart;
        const end = textarea.selectionEnd;
        const selectedText = content.substring(start, end);
        const replacement = `${tag}${selectedText}${endTag}`;

        const updated = content.substring(0, start) + replacement + content.substring(end);
        handleContentChange(updated);

        setTimeout(() => {
            textarea.focus();
            textarea.setSelectionRange(start + tag.length, end + tag.length);
        }, 50);
    };

    return (
        <div className="rounded-2xl border border-slate-800 bg-[#060B18] overflow-hidden focus-within:border-[#00F2FE] transition-all">
            {/* Toolbar */}
            <div className="flex flex-wrap items-center justify-between gap-1 p-2 bg-[#0B132B] border-b border-slate-800 text-slate-300 text-xs">
                <div className="flex flex-wrap items-center gap-1">
                    <button
                        type="button"
                        onClick={() => insertTag('<h2>', '</h2>')}
                        title="Heading 2"
                        className="p-1.5 rounded-lg hover:bg-[#00F2FE]/10 hover:text-[#00F2FE] transition-colors"
                    >
                        <Heading1 className="w-4 h-4" />
                    </button>
                    <button
                        type="button"
                        onClick={() => insertTag('<h3>', '</h3>')}
                        title="Heading 3"
                        className="p-1.5 rounded-lg hover:bg-[#00F2FE]/10 hover:text-[#00F2FE] transition-colors"
                    >
                        <Heading2 className="w-4 h-4" />
                    </button>
                    <div className="w-px h-4 bg-slate-800 mx-1" />
                    <button
                        type="button"
                        onClick={() => insertTag('<strong>', '</strong>')}
                        title="Bold"
                        className="p-1.5 rounded-lg hover:bg-[#00F2FE]/10 hover:text-[#00F2FE] transition-colors"
                    >
                        <Bold className="w-4 h-4" />
                    </button>
                    <button
                        type="button"
                        onClick={() => insertTag('<em>', '</em>')}
                        title="Italic"
                        className="p-1.5 rounded-lg hover:bg-[#00F2FE]/10 hover:text-[#00F2FE] transition-colors"
                    >
                        <Italic className="w-4 h-4" />
                    </button>
                    <div className="w-px h-4 bg-slate-800 mx-1" />
                    <button
                        type="button"
                        onClick={() => insertTag('<ul>\n  <li>', '</li>\n</ul>')}
                        title="Bullet List"
                        className="p-1.5 rounded-lg hover:bg-[#00F2FE]/10 hover:text-[#00F2FE] transition-colors"
                    >
                        <List className="w-4 h-4" />
                    </button>
                    <button
                        type="button"
                        onClick={() => insertTag('<ol>\n  <li>', '</li>\n</ol>')}
                        title="Numbered List"
                        className="p-1.5 rounded-lg hover:bg-[#00F2FE]/10 hover:text-[#00F2FE] transition-colors"
                    >
                        <ListOrdered className="w-4 h-4" />
                    </button>
                    <div className="w-px h-4 bg-slate-800 mx-1" />
                    <button
                        type="button"
                        onClick={() => insertTag('<p>', '</p>')}
                        title="Paragraph"
                        className="px-2 py-1 rounded-lg text-[10px] font-bold bg-slate-800 text-slate-300 hover:text-[#00F2FE]"
                    >
                        &lt;p&gt;
                    </button>
                    <button
                        type="button"
                        onClick={() => insertTag('<a href="#">', '</a>')}
                        title="Link"
                        className="p-1.5 rounded-lg hover:bg-[#00F2FE]/10 hover:text-[#00F2FE] transition-colors"
                    >
                        <LinkIcon className="w-4 h-4" />
                    </button>
                </div>

                <div className="flex items-center gap-1">
                    <button
                        type="button"
                        onClick={() => setEditorMode(editorMode === 'visual' ? 'code' : 'visual')}
                        className={`px-3 py-1 rounded-lg text-xs font-semibold flex items-center gap-1 transition-colors ${
                            editorMode === 'code' ? 'bg-[#00F2FE] text-[#060B18]' : 'bg-slate-800 text-slate-300'
                        }`}
                    >
                        {editorMode === 'code' ? <Eye className="w-3.5 h-3.5" /> : <Code2 className="w-3.5 h-3.5" />}
                        {editorMode === 'code' ? 'Visual Preview' : 'Source Code'}
                    </button>
                </div>
            </div>

            {/* Editor Area */}
            {editorMode === 'visual' ? (
                <div className="p-4 space-y-4">
                    <textarea
                        id="dark-editor-textarea"
                        rows={12}
                        value={content}
                        onChange={(e) => handleContentChange(e.target.value)}
                        placeholder={placeholder || 'Write HTML content here...'}
                        className="w-full bg-[#060B18] text-sm text-slate-100 placeholder-slate-600 font-sans focus:outline-none resize-y leading-relaxed"
                    />
                    <div className="pt-3 border-t border-slate-800/80">
                        <span className="text-[10px] font-bold text-slate-500 uppercase tracking-widest block mb-2">Live Preview Rendering:</span>
                        <div
                            className="glass-card rounded-xl p-4 prose prose-invert max-w-none text-xs text-slate-300 prose-headings:text-white prose-a:text-[#00F2FE]"
                            dangerouslySetInnerHTML={{ __html: content || '<p className="text-slate-600 italic">No content to preview.</p>' }}
                        />
                    </div>
                </div>
            ) : (
                <textarea
                    id="dark-editor-textarea"
                    rows={16}
                    value={content}
                    onChange={(e) => handleContentChange(e.target.value)}
                    className="w-full bg-[#03060E] p-4 text-xs font-mono text-[#00F2FE] focus:outline-none resize-y leading-relaxed"
                />
            )}
        </div>
    );
}
