"use client";
import { type Editor, useEditorState } from "@tiptap/react";

import {
	Bold,
	BrushCleaning,
	Heading1,
	Heading2,
	Heading3,
	Heading4,
	Heading5,
	Heading6,
	Italic,
	List,
	ListOrdered,
	Quote,
	Redo,
	SeparatorHorizontal,
	StrikethroughIcon,
	Text,
	Underline,
	Undo,
} from "lucide-react";
import { Button } from "~/components/ui/button";
import { Toggle } from "~/components/ui/toggle";

function ToolBar({ editor }: { editor: Editor }) {
	const editorState = useEditorState({
		editor,
		selector: (ctx) => {
			return {
				isBold: ctx.editor.isActive("bold") ?? false,
				canBold: ctx.editor.can().chain().toggleBold().run() ?? false,
				isItalic: ctx.editor.isActive("italic") ?? false,
				canItalic: ctx.editor.can().chain().toggleItalic().run() ?? false,
				isUnderline: ctx.editor.isActive("underline") ?? false,
				canUnderline: ctx.editor.can().chain().toggleUnderline().run() ?? false,
				isStrike: ctx.editor.isActive("strike") ?? false,
				canStrike: ctx.editor.can().chain().toggleStrike().run() ?? false,
				canClearMarks: ctx.editor.can().chain().unsetAllMarks().run() ?? false,
				isParagraph: ctx.editor.isActive("paragraph") ?? false,
				isHeading1: ctx.editor.isActive("heading", { level: 1 }) ?? false,
				isHeading2: ctx.editor.isActive("heading", { level: 2 }) ?? false,
				isHeading3: ctx.editor.isActive("heading", { level: 3 }) ?? false,
				isHeading4: ctx.editor.isActive("heading", { level: 4 }) ?? false,
				isHeading5: ctx.editor.isActive("heading", { level: 5 }) ?? false,
				isHeading6: ctx.editor.isActive("heading", { level: 6 }) ?? false,
				isBulletList: ctx.editor.isActive("bulletList") ?? false,
				isOrderedList: ctx.editor.isActive("orderedList") ?? false,
				isBlockquote: ctx.editor.isActive("blockquote") ?? false,
				canUndo: ctx.editor.can().chain().undo().run() ?? false,
				canRedo: ctx.editor.can().chain().redo().run() ?? false,
			};
		},
	});

	return (
		<div className="border-input my-2 flex flex-wrap gap-3 rounded-lg border p-1">
			<Toggle
				onPressedChange={() => editor.chain().focus().setParagraph().run()}
				pressed={editorState.isParagraph}
			>
				<Text className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isHeading1}
				onPressedChange={() =>
					editor.chain().focus().toggleHeading({ level: 1 }).run()
				}
			>
				<Heading1 className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isHeading2}
				onPressedChange={() =>
					editor.chain().focus().toggleHeading({ level: 2 }).run()
				}
			>
				<Heading2 className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isHeading3}
				onPressedChange={() =>
					editor.chain().focus().toggleHeading({ level: 3 }).run()
				}
			>
				<Heading3 className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isHeading4}
				onPressedChange={() =>
					editor.chain().focus().toggleHeading({ level: 4 }).run()
				}
			>
				<Heading4 className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isHeading5}
				onPressedChange={() =>
					editor.chain().focus().toggleHeading({ level: 5 }).run()
				}
			>
				<Heading5 className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isHeading6}
				onPressedChange={() =>
					editor.chain().focus().toggleHeading({ level: 6 }).run()
				}
			>
				<Heading6 className="size-4" />
			</Toggle>

			<Toggle
				size="sm"
				pressed={editorState.isBold}
				disabled={!editorState.canBold}
				onPressedChange={() => editor.chain().focus().toggleBold().run()}
			>
				<Bold className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isItalic}
				disabled={!editorState.canItalic}
				onPressedChange={() => editor.chain().focus().toggleItalic().run()}
			>
				<Italic className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isUnderline}
				disabled={!editorState.canUnderline}
				onPressedChange={() => editor.chain().focus().toggleUnderline().run()}
			>
				<Underline className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isStrike}
				disabled={!editorState.canStrike}
				onPressedChange={() => editor.chain().focus().toggleStrike().run()}
			>
				<StrikethroughIcon className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isBulletList}
				onPressedChange={() => editor.chain().focus().toggleBulletList().run()}
			>
				<List className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isOrderedList}
				onPressedChange={() => editor.chain().focus().toggleOrderedList().run()}
			>
				<ListOrdered className="size-4" />
			</Toggle>
			<Toggle
				size="sm"
				pressed={editorState.isBlockquote}
				onPressedChange={() => editor.chain().focus().toggleBlockquote().run()}
			>
				<Quote className="size-4" />
			</Toggle>
			<Button
				variant="ghost"
				type="button"
				onClick={() => editor.chain().focus().setHorizontalRule().run()}
			>
				<SeparatorHorizontal className="size-4" />
			</Button>
			<Button
				variant="ghost"
				type="button"
				onClick={() => editor.chain().focus().setHardBreak().run()}
			>
				Hard break
			</Button>
			<Button
				variant="ghost"
				type="button"
				disabled={!editorState.canClearMarks}
				onClick={() => editor.chain().focus().unsetAllMarks().run()}
			>
				<BrushCleaning className="size-4" />
			</Button>

			<Button
				variant="ghost"
				type="button"
				disabled={!editorState.canUndo}
				onClick={() => editor.chain().focus().undo().run()}
			>
				<Undo className="size-4" />
			</Button>
			<Button
				variant="ghost"
				type="button"
				disabled={!editorState.canRedo}
				onClick={() => editor.chain().focus().redo().run()}
			>
				<Redo className="size-4" />
			</Button>
		</div>
	);
}

export default ToolBar;
