import { type Component, createSignal, createEffect, For } from "solid-js";
import {
    Trash2,
    Type
} from "lucide-solid";
import { cn } from "@/lib/utils";
import type { Editor } from "@tiptap/core";

// Custom Icons
const AddRowAbove = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="3" y="8" width="12" height="7" rx="1.5" stroke="currentColor" stroke-width="1.5" />
        <path d="M9 2V6M7 4H11" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

const AddRowBelow = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="3" y="3" width="12" height="7" rx="1.5" stroke="currentColor" stroke-width="1.5" />
        <path d="M9 12V16M7 14H11" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

const AddColLeft = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="8" y="3" width="7" height="12" rx="1.5" stroke="currentColor" stroke-width="1.5" />
        <path d="M2 9H6M4 7V11" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

const AddColRight = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="3" y="3" width="7" height="12" rx="1.5" stroke="currentColor" stroke-width="1.5" />
        <path d="M12 9H16M14 7V11" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

const DeleteRow = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="2" y="7" width="14" height="4" rx="0.5" stroke="currentColor" stroke-width="1.5" />
        <path d="M4 3L14 15M14 3L4 15" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

const DeleteCol = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="7" y="2" width="4" height="14" rx="0.5" stroke="currentColor" stroke-width="1.5" />
        <path d="M3 4L15 14M15 4L3 14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

const MergeCells = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="2" y="4" width="14" height="10" rx="1" stroke="currentColor" stroke-width="1.5" />
        <path d="M6 9H12" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

const SplitCell = () => (
    <svg width="20" height="20" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
        <rect x="2" y="4" width="14" height="10" rx="1" stroke="currentColor" stroke-width="1.5" />
        <path d="M9 7V11" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
    </svg>
);

interface TableAction {
    label: string;
    icon: Component;
    command: () => void;
    iconClass?: string;
}

interface TableGroup {
    name: string;
    actions: TableAction[];
}

interface TableBubbleMenuProps {
    editor: Editor;
}

export const TableBubbleMenu: Component<TableBubbleMenuProps> = (props) => {
    const [isVisible, setIsVisible] = createSignal(false);

    createEffect(() => {
        const updateVisibility = () => {
            const editor = props.editor;
            if (!editor) return;
            setIsVisible(editor.isActive("table"));
        };

        props.editor.on("selectionUpdate", updateVisibility);
        props.editor.on("transaction", updateVisibility);

        return () => {
            props.editor.off("selectionUpdate", updateVisibility);
            props.editor.off("transaction", updateVisibility);
        };
    });

    const groups: TableGroup[] = [
        {
            name: "Rows",
            actions: [
                { label: "Insert Row Above", icon: AddRowAbove, command: () => props.editor.chain().focus().addRowBefore().run() },
                { label: "Insert Row Below", icon: AddRowBelow, command: () => props.editor.chain().focus().addRowAfter().run() },
                { label: "Delete Row", icon: DeleteRow, iconClass: "text-destructive", command: () => props.editor.chain().focus().deleteRow().run() },
            ]
        },
        {
            name: "Cols",
            actions: [
                { label: "Insert Column Left", icon: AddColLeft, command: () => props.editor.chain().focus().addColumnBefore().run() },
                { label: "Insert Column Right", icon: AddColRight, command: () => props.editor.chain().focus().addColumnAfter().run() },
                { label: "Delete Column", icon: DeleteCol, iconClass: "text-destructive", command: () => props.editor.chain().focus().deleteColumn().run() },
            ]
        },
        {
            name: "Cells",
            actions: [
                { label: "Merge Cells", icon: MergeCells, command: () => props.editor.chain().focus().mergeCells().run() },
                { label: "Split Cell", icon: SplitCell, command: () => props.editor.chain().focus().splitCell().run() },
                { label: "Header Row", icon: () => <Type size={18} />, command: () => props.editor.chain().focus().toggleHeaderRow().run() },
            ]
        },
        {
            name: "Table",
            actions: [
                { label: "Delete Table", icon: () => <Trash2 size={18} />, iconClass: "text-destructive", command: () => props.editor.chain().focus().deleteTable().run() },
            ]
        }
    ];

    return (
        <div
            class={cn(
                "relative z-[150] w-full bg-background/80 backdrop-blur-md border-b border-border transition-all duration-300 ease-in-out overflow-hidden flex flex-col",
                isVisible() ? "h-9 opacity-100" : "h-0 opacity-0 pointer-events-none"
            )}
            onMouseDown={(e) => {
                e.preventDefault();
                e.stopPropagation();
            }}
        >
            <div class="flex items-center justify-center h-full px-2 max-w-4xl mx-auto w-full">
                <div class="flex items-center gap-1">
                    <For each={groups}>
                        {(group, groupIdx) => (
                            <>
                                <div class="flex items-center gap-0 px-0.5">
                                    <For each={group.actions}>
                                        {(action) => (
                                            <button
                                                type="button"
                                                onClick={(e) => {
                                                    e.preventDefault();
                                                    e.stopPropagation();
                                                    action.command();
                                                }}
                                                class={cn(
                                                    "p-0.5 rounded-md hover:bg-muted transition-colors text-muted-foreground hover:text-foreground shrink-0 group relative",
                                                    action.iconClass
                                                )}
                                            >
                                                <action.icon />
                                                <span class="absolute top-full left-1/2 -translate-x-1/2 mt-2 px-2 py-1 text-[10px] font-bold bg-foreground text-background rounded opacity-0 group-hover:opacity-100 pointer-events-none transition-opacity whitespace-nowrap z-[200] shadow-lg">
                                                    {action.label}
                                                </span>
                                            </button>
                                        )}
                                    </For>
                                </div>
                                {groupIdx() < groups.length - 1 && (
                                    <div class="w-[1px] h-4 bg-border/50 mx-1" />
                                )}
                            </>
                        )}
                    </For>
                </div>
            </div>
        </div>
    );
};
