import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle
} from '@/components/ui/dialog'
import { ScrollArea } from '@/components/ui/scroll-area'
import {
  Sheet,
  SheetContent,
  SheetDescription,
  SheetHeader,
  SheetTitle
} from '@/components/ui/sheet'
import { useScreenSize } from '@/providers/ScreenSizeProvider'
import postEditor from '@/services/post-editor.service'
import { ArrowLeft } from 'lucide-react'
import { Event } from 'nostr-tools'
import { Dispatch, useMemo, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import PostContent, { TPostContentHandle } from './PostContent'
import Title from './Title'

export default function PostEditor({
  defaultContent = '',
  parentStuff,
  open,
  setOpen,
  highlightedText
}: {
  defaultContent?: string
  parentStuff?: Event | string
  open: boolean
  setOpen: Dispatch<boolean>
  highlightedText?: string
}) {
  const { t } = useTranslation()
  const { isSmallScreen } = useScreenSize()
  const contentRef = useRef<TPostContentHandle>(null)

  const handleReset = () => {
    contentRef.current?.reset()
  }

  const content = useMemo(() => {
    return (
      <PostContent
        ref={contentRef}
        defaultContent={defaultContent}
        parentStuff={parentStuff}
        close={() => setOpen(false)}
        highlightedText={highlightedText}
      />
    )
  }, [highlightedText])

  if (isSmallScreen) {
    return (
      <Sheet open={open} onOpenChange={setOpen}>
        <SheetContent
          className="h-[100dvh] w-full p-0 border-none flex flex-col"
          side="bottom"
          hideClose
          onEscapeKeyDown={(e) => {
            if (postEditor.isSuggestionPopupOpen) {
              e.preventDefault()
              postEditor.closeSuggestionPopup()
            }
          }}
        >
          <div className="flex items-center h-12 border-b shrink-0">
            <button
              onClick={() => setOpen(false)}
              className="flex items-center justify-center w-10 h-full hover:bg-accent transition-colors"
              aria-label="Close"
            >
              <ArrowLeft className="w-5 h-5" />
            </button>
            <SheetHeader className="flex-1">
              <SheetTitle className="text-start text-base font-medium">
                {highlightedText ? t('Create Highlight') : <Title parentStuff={parentStuff} />}
              </SheetTitle>
              <SheetDescription className="hidden" />
            </SheetHeader>
            <button
              onClick={handleReset}
              className="flex items-center justify-center w-10 h-full hover:bg-accent transition-colors"
              aria-label="Reset"
            >
              🧹
            </button>
          </div>
          <div className="flex flex-col flex-1 min-h-0 px-4 py-4">{content}</div>
        </SheetContent>
      </Sheet>
    )
  }

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogContent
        className="p-0 max-w-2xl"
        withoutClose
        onEscapeKeyDown={(e) => {
          if (postEditor.isSuggestionPopupOpen) {
            e.preventDefault()
            postEditor.closeSuggestionPopup()
          }
        }}
      >
        <ScrollArea className="px-4 h-full max-h-screen">
          <div className="space-y-4 px-2 py-6">
            <DialogHeader className="flex flex-row items-center justify-between">
              <DialogTitle>
                {highlightedText ? t('Create Highlight') : <Title parentStuff={parentStuff} />}
              </DialogTitle>
              <button
                onClick={handleReset}
                className="flex items-center justify-center w-8 h-8 rounded hover:bg-accent transition-colors"
                aria-label="Reset"
              >
                🧹
              </button>
              <DialogDescription className="hidden" />
            </DialogHeader>
            {content}
          </div>
        </ScrollArea>
      </DialogContent>
    </Dialog>
  )
}
