'use client';

// Vertical Slider — NiceUI (MIT) · https://niceui.dev/c/components/vertical-slider
// One self-contained file: needs only React and Tailwind CSS. Nothing else to install.

import { useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type PointerEvent as RPointerEvent, 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 Volume1 = niceIcon([["path",{"d":"M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z"}],["path",{"d":"M16 9a5 5 0 0 1 0 6"}]]);
const Volume2 = niceIcon([["path",{"d":"M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z"}],["path",{"d":"M16 9a5 5 0 0 1 0 6"}],["path",{"d":"M19.364 18.364a9 9 0 0 0 0-12.728"}]]);
const VolumeX = niceIcon([["path",{"d":"M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z"}],["line",{"x1":"22","x2":"16","y1":"9","y2":"15"}],["line",{"x1":"16","x2":"22","y1":"9","y2":"15"}]]);

// 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/VerticalSlider.tsx ──────────────────────
export type VerticalSliderProps = {
  value: number;
  onChange: (v: number) => void;
  min?: number;
  max?: number;
  step?: number;
  /** Track height in px. */
  height?: number;
  label: string;
  /** Formats the bubble and aria-valuetext. */
  format?: (v: number) => string;
  /** Fill from the middle (for bipolar values like EQ gain). */
  centered?: boolean;
  /** Draw a tick for every n steps (0 = none). */
  ticks?: number;
  className?: string;
};

/**
 * A vertical slider for volume, EQ bands or levels. Drag, click the track, scroll
 * isn't hijacked; keyboard: arrows, PageUp/PageDown (×10) and Home/End.
 */
export function VerticalSlider({ value, onChange, min = 0, max = 100, step = 1, height = 180, label, format = (v) => String(v), centered, ticks = 0, className }: VerticalSliderProps) {
  const track = useRef<HTMLDivElement>(null);
  const [drag, setDrag] = useState(false);
  const snap = (v: number) => Math.min(max, Math.max(min, Math.round((v - min) / step) * step + min));
  const pct = (v: number) => ((v - min) / (max - min)) * 100;
  const p = pct(value);
  const mid = centered ? pct((min + max) / 2) : 0;

  const fromPointer = (e: RPointerEvent) => {
    const r = track.current!.getBoundingClientRect();
    const k = 1 - (e.clientY - r.top) / r.height;
    onChange(snap(min + k * (max - min)));
  };
  const onKey = (e: ReactKeyboardEvent) => {
    const big = step * 10;
    const map: Record<string, number> = { ArrowUp: value + step, ArrowRight: value + step, ArrowDown: value - step, ArrowLeft: value - step, PageUp: value + big, PageDown: value - big, Home: min, End: max };
    if (!(e.key in map)) return;
    e.preventDefault();
    onChange(snap(map[e.key]));
  };

  const nTicks = ticks ? Math.floor((max - min) / step / ticks) : 0;
  return (
    <div className={cn('group relative flex w-9 touch-none select-none justify-center', className)} style={{ height }}>
      <div
        ref={track}
        role="slider"
        aria-orientation="vertical"
        aria-label={label}
        aria-valuemin={min}
        aria-valuemax={max}
        aria-valuenow={value}
        aria-valuetext={format(value)}
        tabIndex={0}
        onKeyDown={onKey}
        onPointerDown={(e) => {
          e.currentTarget.setPointerCapture(e.pointerId);
          setDrag(true);
          fromPointer(e);
        }}
        onPointerMove={(e) => drag && fromPointer(e)}
        onPointerUp={() => setDrag(false)}
        onPointerCancel={() => setDrag(false)}
        className="peer relative h-full w-full cursor-pointer rounded-full outline-none"
      >
        <span className="absolute inset-y-0 left-1/2 w-1.5 -translate-x-1/2 rounded-full bg-white/[.08]" />
        {Array.from({ length: nTicks + 1 }, (_, i) => (
          <span key={i} className="absolute left-0 h-px w-1.5 bg-white/15" style={{ bottom: `${(i / nTicks) * 100}%` }} />
        ))}
        <span
          className={cn('absolute left-1/2 w-1.5 -translate-x-1/2 rounded-full bg-gradient-to-t from-[#0ea5e9] to-[#7dd3fc]', !drag && 'transition-all duration-200 motion-reduce:transition-none')}
          style={{ bottom: `${Math.min(p, mid)}%`, height: `${Math.abs(p - mid)}%` }}
        />
        <span
          className={cn(
            'absolute left-1/2 h-5 w-5 -translate-x-1/2 translate-y-1/2 rounded-full border-2 border-[#7dd3fc] bg-[#000000] shadow-[0_0_0_4px_rgba(56,189,248,.12)]',
            !drag && 'transition-[bottom] duration-200 motion-reduce:transition-none',
            'group-focus-within:shadow-[0_0_0_6px_rgba(56,189,248,.25)]',
            drag && 'scale-110',
          )}
          style={{ bottom: `${p}%` }}
        />
        <span
          className={cn(
            'pointer-events-none absolute left-[calc(50%+16px)] translate-y-1/2 whitespace-nowrap rounded-md bg-[#f3f4f6] px-1.5 py-0.5 text-[11px] font-medium tabular-nums text-[#000000] opacity-0 transition-opacity',
            (drag || undefined) && 'opacity-100',
            'group-hover:opacity-100 group-focus-within:opacity-100',
          )}
          style={{ bottom: `${p}%` }}
        >
          {format(value)}
        </span>
      </div>
    </div>
  );
}

const BANDS = ['60', '150', '400', '1k', '2.4k', '6k', '15k'];
const PRESETS: Record<string, number[]> = {
  Flat: [0, 0, 0, 0, 0, 0, 0],
  'Bass boost': [8, 6, 3, 0, -1, -1, 0],
  Vocal: [-3, -1, 2, 5, 4, 1, -1],
  Bright: [-2, -1, 0, 1, 4, 6, 7],
};

export function VerticalSliderDemo() {
  const [vol, setVol] = useState(64);
  const [muted, setMuted] = useState(false);
  const [eq, setEq] = useState(PRESETS['Bass boost']);
  const preset = Object.entries(PRESETS).find(([, v]) => v.every((x, i) => x === eq[i]))?.[0];
  const VolIcon = muted || vol === 0 ? VolumeX : vol < 50 ? Volume1 : Volume2;
  const W = 280;
  const H = 70;
  const pts = eq.map((g, i) => [(i / (eq.length - 1)) * W, H / 2 - (g / 12) * (H / 2 - 4)] as const);
  const path = pts.map(([x, y], i) => (i ? `S ${(pts[i - 1][0] + x) / 2} ${y} ${x} ${y}` : `M ${x} ${y}`)).join(' ');
  return (
    <div className="flex w-full max-w-2xl flex-col items-center gap-8 sm:flex-row sm:items-stretch sm:justify-center">
      <div className="flex flex-col items-center gap-3 rounded-2xl border border-white/[.07] bg-[#0b0c0e]/50 px-6 py-5">
        <VerticalSlider label="Volume" value={muted ? 0 : vol} onChange={(v) => (setMuted(false), setVol(v))} format={(v) => `${v}%`} height={200} ticks={10} />
        <button onClick={() => setMuted((m) => !m)} aria-label={muted ? 'Unmute' : 'Mute'} aria-pressed={muted} className="focus-ring grid h-9 w-9 place-items-center rounded-full text-[#a3a8b0] hover:bg-white/[.05] hover:text-[#f3f4f6]">
          <VolIcon className="h-4 w-4" />
        </button>
      </div>
      <div className="w-full max-w-[400px] rounded-2xl border border-white/[.07] bg-[#0b0c0e]/50 p-5">
        <div className="flex flex-wrap items-center justify-between gap-2">
          <p className="text-[13px] font-medium text-[#e5e7eb]">Equalizer</p>
          <div className="flex flex-wrap gap-1" role="group" aria-label="Presets">
            {Object.keys(PRESETS).map((k) => (
              <button key={k} onClick={() => setEq(PRESETS[k])} aria-pressed={preset === k} className={cn('rounded-full px-2.5 py-1 text-[11px] transition-colors', preset === k ? 'bg-[#38bdf8] text-[#000000]' : 'bg-white/[.05] text-[#a3a8b0] hover:text-[#f3f4f6]')}>
                {k}
              </button>
            ))}
          </div>
        </div>
        <svg viewBox={`0 0 ${W} ${H}`} className="mt-4 h-14 w-full" preserveAspectRatio="none" aria-hidden>
          <line x1="0" x2={W} y1={H / 2} y2={H / 2} stroke="rgba(255,255,255,.08)" strokeDasharray="3 4" />
          <path d={`${path} L ${W} ${H} L 0 ${H} Z`} fill="rgba(56,189,248,.08)" />
          <path d={path} fill="none" stroke="#7dd3fc" strokeWidth="1.8" vectorEffect="non-scaling-stroke" />
        </svg>
        <div className="mt-2 flex justify-between">
          {eq.map((g, i) => (
            <div key={i} className="flex flex-col items-center gap-2">
              <VerticalSlider
                label={`${BANDS[i]} Hz band`}
                value={g}
                min={-12}
                max={12}
                centered
                height={150}
                format={(v) => `${v > 0 ? '+' : ''}${v} dB`}
                onChange={(v) => setEq((e) => e.map((x, k) => (k === i ? v : x)))}
                className="w-7"
              />
              <span className="font-mono text-[10px] text-[#6b7079]">{BANDS[i]}</span>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}
