"use client";

import { ArrowDownLeft, ArrowUpRight } from "lucide-react";
import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";

import { Button } from "@/components/ui/button";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import {
  TransferInPanel,
  TransferOutPanel,
} from "@/features/wallet/wallet-transfer-forms";
import { cn } from "@/lib/utils";

type BaseProps = {
  currency: string;
  onSuccess: () => Promise<void>;
  /** 避免同页多实例 input id 冲突 */
  idPrefix?: string;
  open?: boolean;
  onOpenChange?: (open: boolean) => void;
};

export function TransferInDialog({
  currency,
  lotteryMinor,
  mainMinor = null,
  onSuccess,
  idPrefix = "",
  triggerClassName,
  triggerVariant = "wallet",
  triggerLabel,
  open: controlledOpen,
  onOpenChange: controlledOnOpenChange,
}: BaseProps & {
  lotteryMinor: number;
  mainMinor?: number | null;
  triggerClassName?: string;
  triggerVariant?: "wallet" | "hall";
  triggerLabel?: string;
}) {
  const [uncontrolledOpen, setUncontrolledOpen] = useState(false);
  const isControlled = controlledOpen !== undefined;
  const open = isControlled ? controlledOpen : uncontrolledOpen;
  const setOpen = useCallback(
    (next: boolean) => {
      if (isControlled) {
        controlledOnOpenChange?.(next);
      } else {
        setUncontrolledOpen(next);
      }
    },
    [controlledOnOpenChange, isControlled],
  );
  const { t } = useTranslation("player");
  const resolvedTriggerLabel = triggerLabel ?? t("wallet.transferIn");

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <Button
        type="button"
        variant="default"
        className={cn(
          "inline-flex h-10 min-h-10 w-full min-w-0 flex-1 items-center justify-center gap-1.5 px-3 text-sm font-medium",
          triggerVariant === "hall" && "bg-[#07459f] text-white hover:bg-[#063b88]",
          triggerClassName,
        )}
        onClick={() => setOpen(true)}
      >
        <ArrowDownLeft className="size-4 shrink-0" />
        {resolvedTriggerLabel}
      </Button>
      <DialogContent showCloseButton className="gap-0 overflow-hidden p-0 sm:max-w-md">
        <DialogHeader className="space-y-1.5 border-b border-border px-4 py-3 text-left">
          <DialogTitle className="text-lg font-semibold">{t("wallet.transferInTitle")}</DialogTitle>
          <DialogDescription className="text-sm leading-relaxed">
            {t("wallet.dialogInDescription", { currency })}
          </DialogDescription>
        </DialogHeader>
        <div className="px-4 py-3">
          <TransferInPanel
            variant="dialog"
            currency={currency}
            lotteryMinor={lotteryMinor}
            mainMinor={mainMinor}
            idPrefix={idPrefix}
            onCancel={() => setOpen(false)}
            onSuccess={async () => {
              await onSuccess();
              setOpen(false);
            }}
          />
        </div>
      </DialogContent>
    </Dialog>
  );
}

export function TransferOutDialog({
  currency,
  availableMinor,
  onSuccess,
  idPrefix = "",
  triggerClassName,
  triggerVariant = "wallet",
  triggerLabel,
  open: controlledOpen,
  onOpenChange: controlledOnOpenChange,
}: BaseProps & {
  availableMinor: number;
  triggerClassName?: string;
  triggerVariant?: "wallet" | "hall";
  triggerLabel?: string;
}) {
  const [uncontrolledOpen, setUncontrolledOpen] = useState(false);
  const isControlled = controlledOpen !== undefined;
  const open = isControlled ? controlledOpen : uncontrolledOpen;
  const setOpen = useCallback(
    (next: boolean) => {
      if (isControlled) {
        controlledOnOpenChange?.(next);
      } else {
        setUncontrolledOpen(next);
      }
    },
    [controlledOnOpenChange, isControlled],
  );
  const { t } = useTranslation("player");
  const resolvedTriggerLabel = triggerLabel ?? t("wallet.transferOut");

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <Button
        type="button"
        variant="outline"
        className={cn(
          "inline-flex h-10 min-h-10 w-full min-w-0 flex-1 items-center justify-center gap-1.5 px-3 text-sm font-medium",
          triggerVariant === "hall" && "border-border bg-card hover:bg-muted",
          triggerClassName,
        )}
        onClick={() => setOpen(true)}
      >
        <ArrowUpRight className="size-4 shrink-0" />
        {resolvedTriggerLabel}
      </Button>
      <DialogContent showCloseButton className="gap-0 overflow-hidden p-0 sm:max-w-md">
        <DialogHeader className="space-y-1.5 border-b border-border px-4 py-3 text-left">
          <DialogTitle className="text-lg font-semibold">{t("wallet.transferOutTitle")}</DialogTitle>
          <DialogDescription className="text-sm leading-relaxed">
            {t("wallet.dialogOutDescription")}
          </DialogDescription>
        </DialogHeader>
        <div className="px-4 py-3">
          <TransferOutPanel
            variant="dialog"
            currency={currency}
            availableMinor={availableMinor}
            idPrefix={idPrefix}
            onCancel={() => setOpen(false)}
            onSuccess={async () => {
              await onSuccess();
              setOpen(false);
            }}
          />
        </div>
      </DialogContent>
    </Dialog>
  );
}
