'use client';

// Time Picker — NiceUI (MIT) · https://niceui.dev/c/components/time-picker
// One self-contained file: needs only React and Tailwind CSS. Nothing else to install.

import { useEffect, useId, useRef, useState, type KeyboardEvent as ReactKeyboardEvent, createElement, type SVGProps } from 'react';

function cn(...classes: Array<string | false | null | undefined>) {
  return classes.filter(Boolean).join(' ');
}

// Icons from Lucide (ISC licence, lucide.dev), inlined so there is nothing to install.
type NiceIconProps = SVGProps<SVGSVGElement> & { size?: number | string; absoluteStrokeWidth?: boolean };
type LucideIcon = (props: NiceIconProps) => JSX.Element;
type LucideProps = NiceIconProps;
const niceIcon = (node: Array<[string, Record<string, string>]>): LucideIcon =>
  function Icon({ size = 24, strokeWidth = 2, absoluteStrokeWidth, children, ...rest }: NiceIconProps) {
    const sw = absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth;
    return createElement('svg', { xmlns: 'http://www.w3.org/2000/svg', width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: sw, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true, ...rest }, ...node.map(([tag, attrs], i) => createElement(tag, { key: i, ...attrs })), children);
  };
const Clock = niceIcon([["circle",{"cx":"12","cy":"12","r":"10"}],["polyline",{"points":"12 6 12 12 16 14"}]]);

// NiceUI animations and helper classes, injected once so no CSS setup is needed.
const NICEUI_CSS = ".focus-ring{outline:none}.focus-ring:focus-visible{box-shadow:0 0 0 2px #000,0 0 0 4px rgba(14,165,233,.6)}";
if (typeof document !== 'undefined' && !document.getElementById('niceui-css-10x759f')) {
  const el = document.createElement('style');
  el.id = 'niceui-css-10x759f';
  el.textContent = NICEUI_CSS;
  document.head.appendChild(el);
}

// ── components/controls/TimePicker.tsx ──────────────────────────
/** Minutes since midnight. */
export type TimeValue = number;

const pad = (n: number) => String(n).padStart(2, '0');
export function formatTime(v: TimeValue, hour12: boolean) {
  const h = Math.floor(v / 60);
  const m = v % 60;
  if (!hour12) return `${pad(h)}:${pad(m)}`;
  return `${h % 12 === 0 ? 12 : h % 12}:${pad(m)} ${h < 12 ? 'AM' : 'PM'}`;
}

type Col = { key: string; label: string; options: { v: number; t: string }[]; value: number; set: (v: number) => void };

function columns(value: TimeValue, onChange: (v: TimeValue) => void, hour12: boolean, step: number): Col[] {
  const h = Math.floor(value / 60);
  const m = value % 60;
  const pm = h >= 12;
  const cols: Col[] = [
    hour12
      ? { key: 'h', label: 'Hour', options: Array.from({ length: 12 }, (_, i) => ({ v: i === 0 ? 12 : i, t: String(i === 0 ? 12 : i) })).sort((a, b) => a.v - b.v), value: h % 12 === 0 ? 12 : h % 12, set: (v) => onChange(((v % 12) + (pm ? 12 : 0)) * 60 + m) }
      : { key: 'h', label: 'Hour', options: Array.from({ length: 24 }, (_, i) => ({ v: i, t: pad(i) })), value: h, set: (v) => onChange(v * 60 + m) },
    { key: 'm', label: 'Minute', options: Array.from({ length: 60 / step }, (_, i) => ({ v: i * step, t: pad(i * step) })), value: Math.round(m / step) * step, set: (v) => onChange(h * 60 + v) },
  ];
  if (hour12) cols.push({ key: 'p', label: 'AM or PM', options: [{ v: 0, t: 'AM' }, { v: 1, t: 'PM' }], value: pm ? 1 : 0, set: (v) => onChange(((h % 12) + (v ? 12 : 0)) * 60 + m) });
  return cols;
}

export type TimePickerProps = {
  value: TimeValue;
  onChange: (v: TimeValue) => void;
  hour12?: boolean;
  /** Minute step. */
  step?: 1 | 5 | 10 | 15 | 30;
  label?: string;
  className?: string;
};

/**
 * A time picker: a field that opens scrollable hour and minute columns (and AM/PM).
 * Left/Right move between columns, Up/Down change the value, Enter or Esc closes.
 */
export function TimePicker({ value, onChange, hour12 = true, step = 5, label = 'Time', className }: TimePickerProps) {
  const uid = useId();
  const [open, setOpen] = useState(false);
  const [col, setCol] = useState(0);
  const root = useRef<HTMLDivElement>(null);
  const btn = useRef<HTMLButtonElement>(null);
  const colRefs = useRef<Array<HTMLDivElement | null>>([]);
  const cols = columns(value, onChange, hour12, step);

  useEffect(() => {
    if (!open) return;
    const down = (e: PointerEvent) => !root.current?.contains(e.target as Node) && setOpen(false);
    window.addEventListener('pointerdown', down);
    return () => window.removeEventListener('pointerdown', down);
  }, [open]);

  // keep the selected option of each column in view
  useEffect(() => {
    if (!open) return;
    colRefs.current.forEach((c) => {
      const sel = c?.querySelector<HTMLElement>('[aria-selected="true"]');
      if (c && sel) c.scrollTo({ top: sel.offsetTop - c.clientHeight / 2 + sel.offsetHeight / 2, behavior: 'smooth' });
    });
  }, [open, value]);

  useEffect(() => {
    if (open) colRefs.current[col]?.focus();
  }, [open, col]);

  const onKey = (e: ReactKeyboardEvent, ci: number) => {
    const c = cols[ci];
    const at = c.options.findIndex((o) => o.v === c.value);
    if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
      e.preventDefault();
      const n = c.options[(at + (e.key === 'ArrowDown' ? 1 : -1) + c.options.length) % c.options.length];
      c.set(n.v);
    } else if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
      e.preventDefault();
      setCol((ci + (e.key === 'ArrowRight' ? 1 : -1) + cols.length) % cols.length);
    } else if (e.key === 'Enter' || e.key === 'Escape') {
      e.preventDefault();
      setOpen(false);
      btn.current?.focus();
    }
  };

  return (
    <div ref={root} className={cn('relative inline-block', className)}>
      <style>{`@keyframes tp-in{from{opacity:0;transform:translateY(-4px) scale(.98)}to{opacity:1;transform:none}}.tp-pop{animation:tp-in .16s cubic-bezier(.2,.8,.2,1) both}@media (prefers-reduced-motion:reduce){.tp-pop{animation:none}}`}</style>
      <button
        ref={btn}
        aria-haspopup="dialog"
        aria-expanded={open}
        aria-label={`${label}: ${formatTime(value, hour12)}`}
        onClick={() => {
          setCol(0);
          setOpen((o) => !o);
        }}
        onKeyDown={(e) => {
          if (e.key === 'ArrowDown' && !open) {
            e.preventDefault();
            setCol(0);
            setOpen(true);
          }
        }}
        className={cn('focus-ring flex h-11 min-w-[150px] items-center gap-3 rounded-xl border border-white/10 bg-[#0b0c0e] px-3.5 text-[14px] text-[#f3f4f6] shadow-[0_0_0_1px_rgba(255,255,255,0.06)] transition-colors hover:bg-[#1c1e22]', open && 'border-[#38bdf8]/50')}
      >
        <Clock className="h-4 w-4 text-[#8b9099]" />
        <span className="font-medium tabular-nums">{formatTime(value, hour12)}</span>
      </button>
      {open && (
        <div role="dialog" aria-label={`Choose ${label.toLowerCase()}`} className="tp-pop absolute left-0 top-[calc(100%+6px)] z-50 flex gap-1 rounded-xl border border-white/10 bg-[#0b0c0e]/95 p-1.5 shadow-[0_24px_60px_-20px_rgba(0,0,0,.9)] backdrop-blur-xl">
          {cols.map((c, ci) => (
            <div
              key={c.key}
              ref={(el) => (colRefs.current[ci] = el)}
              role="listbox"
              aria-label={c.label}
              aria-activedescendant={`${uid}-${c.key}-${c.value}`}
              tabIndex={col === ci ? 0 : -1}
              onKeyDown={(e) => onKey(e, ci)}
              onFocus={() => setCol(ci)}
              className={cn('h-56 w-14 overflow-y-auto rounded-lg py-1 outline-none [scrollbar-width:none] focus-visible:ring-1 focus-visible:ring-[#38bdf8]/50', c.key === 'p' && 'h-auto self-start')}
            >
              {c.options.map((o) => (
                <button
                  key={o.v}
                  id={`${uid}-${c.key}-${o.v}`}
                  role="option"
                  aria-selected={o.v === c.value}
                  tabIndex={-1}
                  onClick={() => {
                    c.set(o.v);
                    setCol(ci);
                  }}
                  className={cn('my-0.5 block w-full rounded-md py-1.5 text-center text-[13.5px] tabular-nums transition-colors', o.v === c.value ? 'bg-[#38bdf8] font-medium text-[#000000]' : 'text-[#a3a8b0] hover:bg-white/[.06] hover:text-[#f3f4f6]')}
                >
                  {o.t}
                </button>
              ))}
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

/* ——— inline wheel ——— */

const ROW = 36;

function Wheel({ options, value, onPick, label }: { options: { v: number; t: string }[]; value: number; onPick: (v: number) => void; label: string }) {
  const ref = useRef<HTMLDivElement>(null);
  const t = useRef(0);
  const idx = Math.max(0, options.findIndex((o) => o.v === value));
  const [scrollIdx, setScrollIdx] = useState(idx);

  useEffect(() => {
    const el = ref.current;
    if (el && Math.round(el.scrollTop / ROW) !== idx) el.scrollTo({ top: idx * ROW, behavior: 'smooth' });
    setScrollIdx(idx);
  }, [idx]);

  return (
    <div className="relative">
      <div
        ref={ref}
        role="spinbutton"
        aria-label={label}
        aria-valuenow={value}
        aria-valuetext={options[idx]?.t}
        tabIndex={0}
        onScroll={(e) => {
          const top = e.currentTarget.scrollTop;
          setScrollIdx(Math.round(top / ROW));
          window.clearTimeout(t.current);
          t.current = window.setTimeout(() => {
            const i = Math.min(options.length - 1, Math.max(0, Math.round(top / ROW)));
            if (options[i].v !== value) onPick(options[i].v);
          }, 120);
        }}
        onKeyDown={(e) => {
          if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return;
          e.preventDefault();
          const n = Math.min(options.length - 1, Math.max(0, idx + (e.key === 'ArrowDown' ? 1 : -1)));
          onPick(options[n].v);
        }}
        className="h-[180px] w-16 snap-y snap-mandatory overflow-y-auto rounded-lg outline-none [scrollbar-width:none] focus-visible:ring-1 focus-visible:ring-[#38bdf8]/50 [&::-webkit-scrollbar]:hidden"
        style={{ paddingBlock: ROW * 2, maskImage: 'linear-gradient(transparent, #000 30%, #000 70%, transparent)', WebkitMaskImage: 'linear-gradient(transparent, #000 30%, #000 70%, transparent)' }}
      >
        {options.map((o, i) => {
          const d = Math.min(2, Math.abs(i - scrollIdx));
          return (
            <button
              key={o.v}
              tabIndex={-1}
              onClick={() => onPick(o.v)}
              className="block w-full snap-center text-center tabular-nums transition-[color,transform] duration-150"
              style={{ height: ROW, fontSize: d === 0 ? 20 : 16, color: d === 0 ? '#f3f4f6' : `rgba(160,166,175,${1 - d * 0.3})`, transform: `perspective(200px) rotateX(${(i - scrollIdx) * 18}deg)` }}
            >
              {o.t}
            </button>
          );
        })}
      </div>
    </div>
  );
}

/** An inline wheel time picker: scroll, drag, click or use arrow keys on each wheel. */
export function WheelTimePicker({ value, onChange, hour12 = true, step = 1, className }: Omit<TimePickerProps, 'label'>) {
  const cols = columns(value, onChange, hour12, step);
  return (
    <div className={cn('relative flex items-center gap-1 rounded-2xl border border-white/[.08] bg-[#0b0c0e]/70 px-3', className)}>
      <span aria-hidden className="pointer-events-none absolute inset-x-2 top-1/2 h-9 -translate-y-1/2 rounded-lg border border-[#38bdf8]/25 bg-[#38bdf8]/[.07]" />
      {cols.map((c, i) => (
        <div key={c.key} className="flex items-center">
          <Wheel options={c.options} value={c.value} onPick={c.set} label={c.label} />
          {i === 0 && <span className="text-xl text-[#6b7079]">:</span>}
        </div>
      ))}
    </div>
  );
}

export function TimePickerDemo() {
  const [a, setA] = useState(9 * 60 + 30);
  const [b, setB] = useState(18 * 60 + 15);
  const [c, setC] = useState(7 * 60 + 45);
  const [h12, setH12] = useState(true);
  return (
    <div className="flex min-h-[340px] w-full max-w-xl flex-col items-center gap-8 sm:flex-row sm:items-start sm:justify-center">
      <div className="flex flex-col gap-4">
        <div className="flex items-center gap-2 rounded-xl bg-[#0b0c0e] p-1 shadow-[0_0_0_1px_rgba(255,255,255,0.06)]" role="group" aria-label="Clock format">
          {[true, false].map((v) => (
            <button key={String(v)} onClick={() => setH12(v)} aria-pressed={h12 === v} className={cn('flex-1 whitespace-nowrap rounded-lg px-3 py-1.5 text-[12px] font-medium transition-colors', h12 === v ? 'bg-[#30333a] text-[#f3f4f6]' : 'text-[#8b9099] hover:text-[#c9cdd3]')}>
              {v ? '12-hour' : '24-hour'}
            </button>
          ))}
        </div>
        <label className="flex flex-col gap-1.5 text-[12px] text-[#8b9099]">
          Meeting starts
          <TimePicker value={a} onChange={setA} hour12={h12} label="Meeting starts" />
        </label>
        <label className="flex flex-col gap-1.5 text-[12px] text-[#8b9099]">
          Reminder
          <TimePicker value={b} onChange={setB} hour12={h12} step={15} label="Reminder" />
        </label>
      </div>
      <div className="flex flex-col items-center gap-3">
        <p className="text-[12px] text-[#8b9099]">Alarm</p>
        <WheelTimePicker value={c} onChange={setC} hour12={h12} />
        <p className="text-[13px] tabular-nums text-[#c9cdd3]" aria-live="polite">
          Rings at {formatTime(c, h12)}
        </p>
      </div>
    </div>
  );
}
