"use client";

import type { ReactNode } from "react";
import { useTranslation } from "@enode/core/translations/hooks";
import { MinusIcon, PlusIcon, TabletIcon, TabletPulseIcon } from "@enode/ui/icons";

/** Props of {@link StepperRow}. Pure presentation; the caller owns the logic. */
export interface StepperRowProps {
  /** What is counted, e.g. "Training stations". */
  title: string;
  /** The count; null while unknown (shows "–"). */
  value: number | null;
  /** A short reason why + or − is off; null hides it. */
  note: string | null;
  canAdd: boolean;
  canRemove: boolean;
  onAdd: () => void;
  onRemove: () => void;
  /** Spoken name of −, e.g. "One training station less". */
  removeLabel: string;
  /** Spoken name of +, e.g. "One training station more". */
  addLabel: string;
  /** Leading glyph (the rail's Stations menu); omitted in Settings. */
  icon?: ReactNode;
  testId: string;
}

/**
 * "Title − n +": a count with a round − / + stepper and a short reason when a
 * step is off. The shape of the Training stations and Feedback stations rows.
 */
export function StepperRow({
  title,
  value,
  note,
  canAdd,
  canRemove,
  onAdd,
  onRemove,
  removeLabel,
  addLabel,
  icon,
  testId,
}: StepperRowProps) {
  return (
    <div className="flex items-center gap-3 px-4 py-2" data-testid={testId}>
      {icon}
      <div className="min-w-0 flex-1">
        <p className="body-prominent text-foreground-default">{title}</p>
        {note && (
          <p className="body-small mt-0.5 text-foreground-more-muted">{note}</p>
        )}
      </div>
      <div className="clickable-small flex shrink-0 items-center gap-2.5">
        <StepButton label={removeLabel} disabled={!canRemove} onClick={onRemove}>
          <MinusIcon className="size-4" />
        </StepButton>
        <span className="min-w-3.5 text-center text-foreground-default" aria-live="polite">
          {value ?? "–"}
        </span>
        <StepButton label={addLabel} disabled={!canAdd} onClick={onAdd}>
          <PlusIcon className="size-4" />
        </StepButton>
      </div>
    </div>
  );
}

/** Props of {@link TrainingStationsRow}. Pure presentation; the page owns the logic. */
export interface TrainingStationsRowProps {
  /** This device's training stations; null while unknown (shows "–"). */
  value: number | null;
  /** A short reason why + or − is off; null hides it. */
  note: string | null;
  canAdd: boolean;
  canRemove: boolean;
  onAdd: () => void;
  onRemove: () => void;
  /** Leading tablet glyph (the rail's Stations menu). */
  withIcon?: boolean;
}

/**
 * "Training stations − n +": how many stations of this device save, with a
 * stepper. Used in the rail's Stations menu (iPad) and on Settings → This
 * device (every device).
 */
export function TrainingStationsRow({ withIcon = false, ...row }: TrainingStationsRowProps) {
  const { t } = useTranslation();
  return (
    <StepperRow
      {...row}
      title={t("Training stations")}
      removeLabel={t("One training station less")}
      addLabel={t("One training station more")}
      icon={withIcon ? <TabletIcon className="size-5 shrink-0 text-foreground-default" /> : undefined}
      testId="training-stations"
    />
  );
}

/** Props of {@link FeedbackStationsRow}. Pure presentation; the page owns the logic. */
export interface FeedbackStationsRowProps {
  /** The open stations that are feedback stations. */
  value: number;
  /** A short reason why + or − is off; null hides it. */
  note: string | null;
  canAdd: boolean;
  canRemove: boolean;
  onAdd: () => void;
  onRemove: () => void;
}

/**
 * "Feedback stations − n +": how many stations next to the training stations
 * only give live feedback, with a stepper and the feedback-station glyph.
 * Only in the rail's Stations menu (iPad); the phone has one station.
 */
export function FeedbackStationsRow(row: FeedbackStationsRowProps) {
  const { t } = useTranslation();
  return (
    <StepperRow
      {...row}
      title={t("Feedback stations")}
      removeLabel={t("One feedback station less")}
      addLabel={t("One feedback station more")}
      icon={<TabletPulseIcon className="size-5 shrink-0 text-foreground-default" />}
      testId="feedback-stations"
    />
  );
}

function StepButton({
  label,
  disabled,
  onClick,
  children,
}: {
  label: string;
  disabled: boolean;
  onClick: () => void;
  children: ReactNode;
}) {
  return (
    <button
      type="button"
      aria-label={label}
      disabled={disabled}
      onClick={onClick}
      // 36 px drawn, 48 px to tap (the pseudo-element).
      className="relative flex size-9 items-center justify-center rounded-full border border-surface-stroke-muted text-foreground-default after:absolute after:-inset-1.5 enabled:hover:bg-surface-base-muted disabled:cursor-default disabled:border-surface-stroke disabled:text-foreground-most-muted"
    >
      {children}
    </button>
  );
}
