import { BookmarkIcon, EyeOffIcon, MessageCircleMoreIcon } from "lucide-react";
import { Link } from "react-router-dom";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import i18n from "@/i18n";
import { cn } from "@/lib/utils";
import { Visibility } from "@/types/proto/api/v1/memo_service";
import { useTranslate } from "@/utils/i18n";
import { convertVisibilityToString } from "@/utils/memo";
import MemoActionMenu from "../../MemoActionMenu";
import { ReactionSelector } from "../../reactions";
import UserAvatar from "../../UserAvatar";
import VisibilityIcon from "../../VisibilityIcon";
import type { MemoHeaderProps } from "../types";

/**
 * MemoHeader component displays the top section of a memo card including:
 * - Creator info (avatar, name) when showCreator is true
 * - Display time (relative or absolute)
 * - Reaction selector
 * - Comment count link
 * - Visibility icon
 * - Pin indicator
 * - NSFW hide button
 * - Action menu
 */
const MemoHeader: React.FC<MemoHeaderProps> = ({
  memo,
  creator,
  showCreator,
  showVisibility,
  showPinned,
  isArchived,
  commentAmount,
  isInMemoDetailPage,
  parentPage,
  readonly,
  relativeTimeFormat,
  onEdit,
  onGotoDetail,
  onUnpin,
  onToggleNsfwVisibility,
  nsfw,
  showNSFWContent,
  reactionSelectorOpen,
  onReactionSelectorOpenChange,
}) => {
  const t = useTranslate();

  const displayTime = isArchived ? (
    memo.displayTime?.toLocaleString(i18n.language)
  ) : (
    <relative-time datetime={memo.displayTime?.toISOString()} lang={i18n.language} format={relativeTimeFormat}></relative-time>
  );

  return (
    <div className="w-full flex flex-row justify-between items-center gap-2">
      {/* Left section: Creator info or time */}
      <div className="w-auto max-w-[calc(100%-8rem)] grow flex flex-row justify-start items-center">
        {showCreator && creator ? (
          <CreatorDisplay creator={creator} displayTime={displayTime} onGotoDetail={onGotoDetail} />
        ) : (
          <TimeDisplay displayTime={displayTime} onGotoDetail={onGotoDetail} />
        )}
      </div>

      {/* Right section: Actions */}
      <div className="flex flex-row justify-end items-center select-none shrink-0 gap-2">
        {/* Reaction selector */}
        {!isArchived && (
          <ReactionSelector
            className={cn("border-none w-auto h-auto", reactionSelectorOpen && "block!", "hidden group-hover:block")}
            memo={memo}
            onOpenChange={onReactionSelectorOpenChange}
          />
        )}

        {/* Comment count link */}
        {!isInMemoDetailPage && (
          <Link
            className={cn(
              "flex flex-row justify-start items-center rounded-md p-1 hover:opacity-80",
              commentAmount === 0 && "invisible group-hover:visible",
            )}
            to={`/${memo.name}#comments`}
            viewTransition
            state={{ from: parentPage }}
          >
            <MessageCircleMoreIcon className="w-4 h-4 mx-auto text-muted-foreground" />
            {commentAmount > 0 && <span className="text-xs text-muted-foreground">{commentAmount}</span>}
          </Link>
        )}

        {/* Visibility icon */}
        {showVisibility && memo.visibility !== Visibility.PRIVATE && (
          <Tooltip>
            <TooltipTrigger>
              <span className="flex justify-center items-center rounded-md hover:opacity-80">
                <VisibilityIcon visibility={memo.visibility} />
              </span>
            </TooltipTrigger>
            <TooltipContent>
              {t(`memo.visibility.${convertVisibilityToString(memo.visibility).toLowerCase()}` as Parameters<typeof t>[0])}
            </TooltipContent>
          </Tooltip>
        )}

        {/* Pinned indicator */}
        {showPinned && memo.pinned && (
          <TooltipProvider>
            <Tooltip>
              <TooltipTrigger asChild>
                <span className="cursor-pointer">
                  <BookmarkIcon className="w-4 h-auto text-primary" onClick={onUnpin} />
                </span>
              </TooltipTrigger>
              <TooltipContent>
                <p>{t("common.unpin")}</p>
              </TooltipContent>
            </Tooltip>
          </TooltipProvider>
        )}

        {/* NSFW hide button */}
        {nsfw && showNSFWContent && onToggleNsfwVisibility && (
          <span className="cursor-pointer">
            <EyeOffIcon className="w-4 h-auto text-primary" onClick={onToggleNsfwVisibility} />
          </span>
        )}

        {/* Action menu */}
        <MemoActionMenu memo={memo} readonly={readonly} onEdit={onEdit} />
      </div>
    </div>
  );
};

/**
 * Creator display with avatar and name
 */
interface CreatorDisplayProps {
  creator: NonNullable<MemoHeaderProps["creator"]>;
  displayTime: React.ReactNode;
  onGotoDetail: () => void;
}

const CreatorDisplay: React.FC<CreatorDisplayProps> = ({ creator, displayTime, onGotoDetail }) => (
  <div className="w-full flex flex-row justify-start items-center">
    <Link className="w-auto hover:opacity-80 rounded-md transition-colors" to={`/u/${encodeURIComponent(creator.username)}`} viewTransition>
      <UserAvatar className="mr-2 shrink-0" avatarUrl={creator.avatarUrl} />
    </Link>
    <div className="w-full flex flex-col justify-center items-start">
      <Link
        className="block leading-tight hover:opacity-80 rounded-md transition-colors truncate text-muted-foreground"
        to={`/u/${encodeURIComponent(creator.username)}`}
        viewTransition
      >
        {creator.displayName || creator.username}
      </Link>
      <button
        type="button"
        className="w-auto -mt-0.5 text-xs leading-tight text-muted-foreground select-none cursor-pointer hover:opacity-80 transition-colors text-left"
        onClick={onGotoDetail}
      >
        {displayTime}
      </button>
    </div>
  </div>
);

/**
 * Simple time display without creator info
 */
interface TimeDisplayProps {
  displayTime: React.ReactNode;
  onGotoDetail: () => void;
}

const TimeDisplay: React.FC<TimeDisplayProps> = ({ displayTime, onGotoDetail }) => (
  <button
    type="button"
    className="w-full text-sm leading-tight text-muted-foreground select-none cursor-pointer hover:text-foreground transition-colors text-left"
    onClick={onGotoDetail}
  >
    {displayTime}
  </button>
);

export default MemoHeader;
