import { Check, Eraser } from "lucide-react";
import * as React from "react";
import { ElementError, getElementErrorAria } from "@/components/general/element-error";
import { ElementHeader } from "@/components/general/element-header";
import { Input } from "@/components/general/input";
import { SIGNATURE_INK_COLOR } from "@/lib/signature/signature-draw";
import { loadSignatureFont } from "@/lib/signature/signature-font";
import {
  SIGNATURE_ASPECT_RATIO,
  type SignatureMode,
  type SignatureValue,
  TYPED_NAME_MAX_LENGTH,
  clearSignature,
  getSignatureViewState,
  isTypedNameSigned,
  nextSignatureMode,
} from "@/lib/signature/signature-model";
import { type SignaturePad, createSignaturePad } from "@/lib/signature/signature-pad";
import { cn } from "@/lib/utils";

/** The strings the element renders. Defaults are English; the surveys runtime passes translations. */
interface SignatureLabels {
  /** Visible text of the Clear button */
  clear: string;
  /** Visible text of the Draw tab */
  draw: string;
  /** Visible text of the Type tab */
  type: string;
  /** Accessible name of the typed-name input, and its hint on the empty preview */
  typeYourName: string;
  /** Accessible name of the Draw / Type tab list */
  method: string;
  /** Accessible name of the drawing pad */
  padLabel: string;
  /** Hint drawn on an empty pad */
  signHere: string;
  /** Spoken when the element has a signature */
  signed: string;
  /** Spoken when it has none */
  notSigned: string;
  /** Shown instead of the pad when a signature was saved earlier and only its URL is left */
  saved: string;
  /** Shown over the pad while the image uploads */
  uploading: string;
}

const DEFAULT_LABELS: SignatureLabels = {
  clear: "Clear",
  draw: "Draw",
  type: "Type",
  typeYourName: "Type your name",
  method: "Signature method",
  padLabel: "Signature pad. Draw your signature with your finger, a stylus or a mouse.",
  signHere: "Sign here",
  signed: "Signed",
  notSigned: "Not signed",
  saved: "Your signature is saved. Clear it to sign again.",
  uploading: "Uploading…",
};

interface SignatureProps {
  /** Unique identifier for the element container */
  elementId: string;
  /** The main element or prompt text displayed as the headline */
  headline: string;
  /** Optional descriptive text displayed below the headline */
  description?: string;
  /** Base id for the element's controls (tabs, pad panel, name input, status) */
  inputId: string;
  /** What the respondent has drawn and typed so far. Held by the parent so it survives navigation. */
  value: SignatureValue;
  /** Called with the new value on every edit, including Clear. */
  onChange: (value: SignatureValue) => void;
  /** Called after Clear, so the parent can also drop a signature it saved earlier. */
  onClear?: () => void;
  /** Whether the respondent may type their name instead of drawing. When false there are no tabs. */
  allowTypedSignature?: boolean;
  /**
   * A signature was saved earlier and only its stored URL is left (the page was reloaded). The pad
   * shows that state, not the image: the stored file is private and only the survey's owner can open it.
   */
  hasSavedSignature?: boolean;
  /** The signature image is being uploaded: the pad and Clear are locked. */
  isUploading?: boolean;
  /**
   * Where to fetch the typed-signature font from. Requested the first time the respondent opens the
   * Type tab; without it, or if the request fails, typed names are drawn in the system cursive font.
   */
  typedFontUrl?: string;
  /** Replacement strings; any left out keep their English default. */
  labels?: Partial<SignatureLabels>;
  /** Whether the field is required (shows asterisk indicator) */
  required?: boolean;
  /** Custom label for the required indicator */
  requiredLabel?: string;
  /** Error message to display */
  errorMessage?: string;
  /** Text direction: 'ltr' (left-to-right), 'rtl' (right-to-left), or 'auto' (auto-detect from content) */
  dir?: "ltr" | "rtl" | "auto";
  /** Whether the element is disabled */
  disabled?: boolean;
  /** Image URL to display above the headline */
  imageUrl?: string;
  /** Video URL to display above the headline */
  videoUrl?: string;
  /** Alt text for the image */
  imageAltText?: string;
}

const CONTROL_BASE_CLASSES = cn(
  "inline-flex items-center justify-center gap-1.5 border px-3 py-1.5 transition-colors",
  "border-input-border bg-input-bg text-input-text font-input-weight",
  "[font-size:var(--fb-input-font-size)]",
  "disabled:cursor-not-allowed disabled:opacity-50 [&_svg]:size-4 [&_svg]:shrink-0"
);

const CONTROL_CLASSES = cn(CONTROL_BASE_CLASSES, "rounded-input");

/**
 * Draw and Type are one segmented control, not two buttons: the tabs touch and share one outline, so they
 * read as a switch and apart from Clear. `-ms-px` overlaps the shared border; the selected tab is raised so
 * its brand border shows whole. Logical corners keep the outline right in RTL.
 */
const SEGMENT_CLASSES = cn(CONTROL_BASE_CLASSES, "relative");
const SEGMENT_CORNER_CLASSES: Record<SignatureMode, string> = {
  draw: "rounded-s-input",
  type: "-ms-px rounded-e-input",
};

const getStatusText = (
  { isUploading, isSigned }: { isUploading: boolean; isSigned: boolean },
  text: SignatureLabels
): string => {
  if (isUploading) return text.uploading;
  return isSigned ? text.signed : text.notSigned;
};

/** What sits over the pad: the hint, the saved notice after a reload, and the busy veil while uploading. */
function PadOverlays({
  text,
  mode,
  showHint,
  hasSavedOnly,
  isUploading,
}: Readonly<{
  text: SignatureLabels;
  mode: SignatureMode;
  showHint: boolean;
  hasSavedOnly: boolean;
  isUploading: boolean;
}>): React.JSX.Element {
  return (
    <>
      {showHint ? (
        <span
          aria-hidden="true"
          className="text-input-placeholder opacity-input-placeholder pointer-events-none absolute inset-0 flex items-center justify-center text-sm">
          {mode === "type" ? text.typeYourName : text.signHere}
        </span>
      ) : null}
      {hasSavedOnly ? (
        <div className="bg-input-bg absolute inset-0 flex items-center justify-center gap-2 p-4 text-center text-sm [color:var(--fb-input-text-color,var(--fb-input-color))]">
          <Check aria-hidden="true" className="size-4 shrink-0" />
          <span>{text.saved}</span>
        </div>
      ) : null}
      {isUploading ? (
        <div className="absolute inset-0 flex items-center justify-center [background-color:color-mix(in_srgb,var(--fb-input-bg-color)_80%,transparent)] text-sm font-medium [color:var(--fb-input-text-color,var(--fb-input-color))] motion-safe:animate-pulse">
          {text.uploading}
        </div>
      ) : null}
    </>
  );
}

function Signature({
  elementId,
  headline,
  description,
  inputId,
  value,
  onChange,
  onClear,
  allowTypedSignature = true,
  hasSavedSignature = false,
  isUploading = false,
  typedFontUrl,
  labels,
  required = false,
  requiredLabel,
  errorMessage,
  dir = "auto",
  disabled = false,
  imageUrl,
  videoUrl,
  imageAltText,
}: Readonly<SignatureProps>): React.JSX.Element {
  const text = { ...DEFAULT_LABELS, ...labels };
  const canvasRef = React.useRef<HTMLCanvasElement>(null);
  const padRef = React.useRef<SignaturePad | null>(null);
  const [clearCount, setClearCount] = React.useState(0);
  // A stroke reaches `value` only when it ends; until then this keeps the hint off the ink being drawn.
  const [isStroking, setIsStroking] = React.useState(false);
  const nameRef = React.useRef<HTMLInputElement>(null);
  const padBoxRef = React.useRef<HTMLDivElement>(null);
  const tabRefs = React.useRef<Partial<Record<SignatureMode, HTMLButtonElement | null>>>({});

  // The pad reports strokes from pointer handlers created once; these refs keep them pointed at the latest props.
  const valueRef = React.useRef(value);
  const onChangeRef = React.useRef(onChange);
  React.useEffect(() => {
    valueRef.current = value;
    onChangeRef.current = onChange;
  });

  const {
    showModeToggle,
    mode,
    showNameInput,
    hasSavedOnly,
    isSigned,
    hasAnyInput,
    hasActiveInput,
    locked,
    drawingEnabled,
  } = getSignatureViewState({ value, allowTypedSignature, hasSavedSignature, disabled, isUploading });

  React.useEffect(() => {
    const canvas = canvasRef.current;
    if (!canvas) return;
    const pad = createSignaturePad({
      canvas,
      // The canvas carries the input text colour in CSS; the ink is read from it on every paint.
      getInkColor: () => getComputedStyle(canvas).color || SIGNATURE_INK_COLOR,
      onStrokesChange: (strokes) => {
        setIsStroking(false);
        onChangeRef.current({ ...valueRef.current, strokes });
      },
      onStrokeStart: () => {
        setIsStroking(true);
      },
    });
    padRef.current = pad;
    return () => {
      pad.destroy();
      padRef.current = null;
    };
  }, []);

  React.useEffect(() => {
    padRef.current?.setContent({ mode, strokes: value.strokes, typedName: value.typedName });
  }, [mode, value.strokes, value.typedName]);

  React.useEffect(() => {
    padRef.current?.setDrawingEnabled(drawingEnabled);
  }, [drawingEnabled]);

  // The font is requested the first time the Type tab is open, and the preview is repainted once it is in.
  React.useEffect(() => {
    if (mode !== "type" || !typedFontUrl) return;
    let cancelled = false;
    void loadSignatureFont(typedFontUrl).then(() => {
      if (!cancelled) padRef.current?.redraw();
    });
    return () => {
      cancelled = true;
    };
  }, [mode, typedFontUrl]);

  const errorAria = getElementErrorAria(inputId, errorMessage);
  const descriptionId = description ? `${inputId}-description` : undefined;
  const headlineId = `${inputId}-headline`;
  const panelId = `${inputId}-panel`;
  const nameId = `${inputId}-name`;
  const describedBy = [descriptionId, errorAria.ariaDescribedBy].filter(Boolean).join(" ") || undefined;
  // In Type mode the canvas is only a preview of the name typed below it, and in the saved state it is
  // blank: neither is something to announce.
  const isPadLabelled = mode === "draw" && !hasSavedOnly;
  // The canvas is a picture of the pad and is hidden from assistive technology; what a screen-reader user
  // needs to know about it (and the typed alternative) is read as the description of the whole group.
  const padLabelId = `${inputId}-pad-label`;
  const groupDescribedBy =
    [descriptionId, isPadLabelled ? padLabelId : undefined, errorAria.ariaDescribedBy]
      .filter(Boolean)
      .join(" ") || undefined;

  const selectMode = (next: SignatureMode): void => {
    if (next !== value.mode) onChange({ ...value, mode: next });
  };

  const handleTabKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>): void => {
    const next = nextSignatureMode(event.key, mode, dir === "rtl" ? "rtl" : "ltr");
    if (!next) return;
    event.preventDefault();
    selectMode(next);
    tabRefs.current[next]?.focus();
  };

  const handleClear = (): void => {
    onChange(clearSignature(value));
    onClear?.();
    setClearCount((count) => count + 1);
  };

  // Clear disables itself (nothing left to clear), which would drop focus to <body>. Once the cleared state
  // has rendered, focus moves to where the respondent carries on: the name input in Type mode, otherwise
  // the active tab, or the pad itself when there are no tabs.
  React.useLayoutEffect(() => {
    if (clearCount === 0) return;
    const target =
      mode === "type" && nameRef.current ? nameRef.current : (tabRefs.current[mode] ?? padBoxRef.current);
    target?.focus({ preventScroll: true });
    // Only a Clear press moves focus, not later changes of mode.
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [clearCount]);

  const renderTab = (tabMode: SignatureMode, label: string): React.JSX.Element => {
    const selected = mode === tabMode;
    return (
      <button
        key={tabMode}
        ref={(el) => {
          tabRefs.current[tabMode] = el;
        }}
        type="button"
        role="tab"
        id={`${inputId}-tab-${tabMode}`}
        aria-selected={selected}
        aria-controls={panelId}
        tabIndex={selected ? 0 : -1}
        onKeyDown={handleTabKeyDown}
        disabled={locked || hasSavedOnly}
        onClick={() => {
          selectMode(tabMode);
        }}
        className={cn(
          SEGMENT_CLASSES,
          SEGMENT_CORNER_CLASSES[tabMode],
          selected
            ? "border-brand-readable bg-input-selected-bg z-10 font-semibold"
            : "hover:bg-input-selected-bg"
        )}>
        {label}
      </button>
    );
  };

  const showHint = !isSigned && !hasActiveInput && !isUploading && !isStroking;
  const statusText = getStatusText({ isUploading, isSigned }, text);

  const clearButton = (
    <button
      type="button"
      onClick={handleClear}
      disabled={locked || !hasAnyInput}
      className={cn(CONTROL_CLASSES, "hover:bg-input-selected-bg")}>
      <Eraser aria-hidden="true" />
      {text.clear}
    </button>
  );

  return (
    <div className="w-full space-y-4" id={elementId} dir={dir}>
      <ElementHeader
        headline={headline}
        description={description}
        descriptionId={descriptionId}
        headlineId={headlineId}
        required={required}
        requiredLabel={requiredLabel}
        imageUrl={imageUrl}
        videoUrl={videoUrl}
        imageAltText={imageAltText}
      />

      <div className="relative" data-element-input>
        <ElementError errorMessage={errorMessage} dir={dir} id={errorAria.errorId} />

        <fieldset
          aria-labelledby={headlineId}
          aria-describedby={groupDescribedBy}
          className="m-0 min-w-0 space-y-2 border-0 p-0">
          {/* With tabs, Clear shares their row; without them it sits under the pad rather than alone on an
              empty row above it. */}
          {showModeToggle ? (
            <div className="flex items-center justify-between gap-2">
              <div role="tablist" aria-label={text.method} className="flex items-center">
                {renderTab("draw", text.draw)}
                {renderTab("type", text.type)}
              </div>
              {clearButton}
            </div>
          ) : null}

          <div
            id={panelId}
            role={showModeToggle ? "tabpanel" : undefined}
            aria-labelledby={showModeToggle ? `${inputId}-tab-${mode}` : undefined}
            className="space-y-2">
            {/* The pad follows the survey's theme and appearance like any other input: its background is the
                input background and the ink is the input text colour. Only the exported PNG is a fixed white
                page with near-black ink (see signature-export.ts). */}
            {/* The pad cancels pointerdown so a stroke never scrolls or selects, which also cancels the focus
                move a press makes. Focusing the pad by hand drops the ring the card's mount focus left on the
                Draw tab, so it does not linger while the respondent draws. */}
            <div
              ref={padBoxRef}
              tabIndex={-1}
              onPointerDown={() => {
                if (drawingEnabled) padBoxRef.current?.focus({ preventScroll: true });
              }}
              // A long press on the canvas must not open the browser's image menu; the event bubbles here.
              onContextMenu={(event) => {
                event.preventDefault();
              }}
              className="rounded-input border-input-border bg-input-bg relative w-full overflow-hidden border outline-none"
              style={{ aspectRatio: `${String(SIGNATURE_ASPECT_RATIO)} / 1` }}>
              {/* The canvas is a picture of the pad. It is hidden from assistive technology through this
                  wrapper, because the rule against aria-hidden on focusable elements counts a canvas as one. */}
              <div aria-hidden="true" className="absolute inset-0">
                {/* The ink is painted in JS, so a theme change made in CSS alone (the appearance flipping
                    to dark, new styling in the editor) would leave it in the old colour. The 1ms colour
                    transition turns that change into a transitionend event, which repaints the ink. */}
                <canvas
                  ref={canvasRef}
                  className="block h-full w-full touch-none [color:var(--fb-input-text-color,var(--fb-input-color))] select-none [transition:color_1ms]"
                  style={{ touchAction: "none", WebkitTouchCallout: "none" }}
                  onTransitionEnd={(event) => {
                    if (event.propertyName === "color") padRef.current?.redraw();
                  }}
                />
              </div>
              <PadOverlays
                text={text}
                mode={mode}
                showHint={showHint}
                hasSavedOnly={hasSavedOnly}
                isUploading={isUploading}
              />
            </div>

            {showNameInput ? (
              <Input
                id={nameId}
                ref={nameRef}
                type="text"
                aria-describedby={describedBy}
                value={value.typedName}
                maxLength={TYPED_NAME_MAX_LENGTH}
                placeholder={text.typeYourName}
                aria-label={text.typeYourName}
                aria-required={required}
                aria-invalid={errorAria.ariaInvalid && !isTypedNameSigned(value.typedName)}
                autoComplete="off"
                autoCapitalize="words"
                spellCheck={false}
                className="box-border"
                dir={dir}
                disabled={locked || hasSavedOnly}
                onChange={(event) => {
                  onChange({ ...value, typedName: event.target.value });
                }}
              />
            ) : null}
          </div>

          {showModeToggle ? null : <div className="flex justify-end">{clearButton}</div>}

          {isPadLabelled ? (
            <p id={padLabelId} className="sr-only">
              {text.padLabel}
            </p>
          ) : null}

          <p role="status" aria-live="polite" className="sr-only">
            {statusText}
          </p>
        </fieldset>
      </div>
    </div>
  );
}

export { Signature, DEFAULT_LABELS as DEFAULT_SIGNATURE_LABELS };
export type { SignatureProps, SignatureLabels };
