import { Link, useRouterState } from "@tanstack/react-router";
import { useEffect, useState, type ReactNode } from "react";
import { Bell, Wallet, User, Menu, Landmark, Gamepad2, UserPlus, Gift, Headphones, CheckCircle2 } from "lucide-react";
import { useBalance, fmt } from "@/lib/wallet";
import { Logo } from "@/components/lotus/Loader";
import { SupportChat } from "@/components/lotus/SupportChat";

const NAV = [
  { label: "Menu", to: "/menu", Icon: Menu },
  { label: "Deposit", to: "/deposit", Icon: Landmark },
  { label: "Games", to: "/", Icon: Gamepad2 },
  { label: "Invite", to: "/invite", Icon: UserPlus },
  { label: "Promotions", to: "/promotions", Icon: Gift },
] as const;

/** Lobby header + bottom tab bar + floating support, shared by all lobby pages. */
export function LobbyShell({ children }: { children: ReactNode }) {
  const balance = useBalance();
  const [support, setSupport] = useState(false);
  const path = useRouterState({ select: (s) => s.location.pathname }).replace(/\/$/, "") || "/";
  return (
    <div className="app-frame bg-lobby pb-24">
      <header className="sticky top-0 z-20 flex items-center justify-between bg-lobby-deep px-3 py-2.5">
        <Link to="/"><Logo className="text-base" /></Link>
        <div className="flex items-center gap-2">
          <button aria-label="Notifications" className="grid size-9 place-items-center rounded-lg bg-lobby-card"><Bell className="size-4" /></button>
          <div className="flex h-9 items-center gap-1.5 rounded-lg bg-lobby-card px-2.5 text-sm font-bold">🇮🇳 {fmt(balance)} INR</div>
          <Link to="/deposit" aria-label="Wallet" className="grid size-9 place-items-center rounded-lg bg-gold text-gold-foreground"><Wallet className="size-4" /></Link>
          <Link to="/menu" aria-label="Profile" className="grid size-9 place-items-center rounded-lg bg-lobby-card"><User className="size-4" /></Link>
        </div>
      </header>
      <div key={path} className="animate-intro-fade">{children}</div>
      {support && <SupportChat onClose={() => setSupport(false)} />}
      <button onClick={() => setSupport(true)} aria-label="Support" className="fixed bottom-24 right-[max(1rem,calc(50%-200px))] z-30 grid size-12 place-items-center rounded-full bg-gold text-gold-foreground shadow-lg animate-floaty">
        <Headphones className="size-6" />
      </button>
      <nav aria-label="Footer" className="fixed bottom-0 left-1/2 z-30 grid h-16 w-full max-w-[430px] -translate-x-1/2 grid-cols-5 bg-footer pb-2 pt-1 text-[10px] text-muted-foreground">
        {NAV.map(({ label, to, Icon }) => {
          const center = label === "Games";
          const active = path === to;
          return (
            <Link key={label} to={to} className={`flex flex-col items-center gap-0.5 ${center ? "" : "pt-2"}`}>
              <span className={center ? "grid h-9 w-12 place-items-center rounded-xl bg-gold text-gold-foreground shadow-lg" : "grid size-6 place-items-center"}>
                <Icon className={center ? "size-6" : `size-4 ${active ? "text-gold" : ""}`} />
              </span>
              <span className={active ? "font-bold text-gold" : ""}>{label}</span>
            </Link>
          );
        })}
      </nav>
    </div>
  );
}

/** Small top toast like "Selected Channel: ZX PAY". */
export function useToast() {
  const [msg, setMsg] = useState<string | null>(null);
  useEffect(() => { if (!msg) return; const t = setTimeout(() => setMsg(null), 2000); return () => clearTimeout(t); }, [msg]);
  const node = msg ? (
    <div className="fixed left-1/2 top-2 z-50 flex w-[min(400px,92vw)] -translate-x-1/2 animate-pop items-center gap-2 rounded-lg border border-border bg-lobby-deep px-3 py-3 text-xs font-bold shadow-xl">
      <CheckCircle2 className="size-4 text-win" /> {msg}
    </div>
  ) : null;
  return { show: setMsg, node };
}

export function PageTitle({ a, b, sub, right }: { a: string; b: string; sub: string; right?: ReactNode }) {
  return (
    <div className="flex items-start justify-between border-b border-border px-3 pb-3 pt-5">
      <div>
        <h1 className="text-2xl font-extrabold leading-none">{a} <span className="text-gold">{b}</span></h1>
        <p className="mt-1.5 text-xs text-muted-foreground">{sub}</p>
      </div>
      {right}
    </div>
  );
}

export function Steps({ title, items, hiItems }: { title: string; items: ReactNode[]; hiItems: ReactNode[] }) {
  const [hi, setHi] = useState(false);
  return (
    <section className="mx-3 mt-6 rounded-lg bg-lobby-deep p-3">
      <div className="flex items-center justify-between">
        <h3 className="text-sm font-extrabold text-gold">{title}</h3>
        <button onClick={() => setHi((v) => !v)} className="rounded border border-border bg-foreground px-2 py-0.5 text-[11px] font-bold text-lobby-deep">{hi ? "View in English" : "हिंदी में देखें"}</button>
      </div>
      <ol className="mt-3 space-y-3 text-xs text-muted-foreground">
        {(hi ? hiItems : items).map((it, i) => (
          <li key={i} className="flex gap-2.5"><span className="grid size-5 shrink-0 place-items-center rounded-full border border-gold text-[10px] font-bold text-gold">{i + 1}</span><span>{it}</span></li>
        ))}
      </ol>
    </section>
  );
}

export const card = "rounded-lg border border-border bg-lobby-deep";
