import type { ReactNode } from 'react';
import { Icon } from '../ui/Icon';

function Line({ className }: { className: string }) {
  return <span className={`block rounded-full ${className}`} />;
}

function WindowFrame({ children, className = '', dark = false }: { children: ReactNode; className?: string; dark?: boolean }) {
  return (
    <div className={`overflow-hidden rounded-xl border shadow-[0_24px_48px_-24px_rgba(2,6,38,0.35)] ${dark ? 'border-white/10 bg-[#0b1024]' : 'border-line bg-surface'} ${className}`}>
      <div className={`flex items-center gap-1.5 border-b px-3 py-2 ${dark ? 'border-white/10' : 'border-line'}`}>
        {['bg-[#ff5f57]', 'bg-[#febc2e]', 'bg-[#28c840]'].map((tone) => (
          <span key={tone} className={`h-2 w-2 rounded-full ${tone}`} />
        ))}
      </div>
      {children}
    </div>
  );
}

/** Stacked application windows: everything in one place. */
export function LayersIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[520px]">
      <WindowFrame className="absolute top-[18%] left-[2%] w-[46%] rotate-[-4deg]">
        <div className="space-y-2 p-3">
          <Line className="h-2 w-16 bg-ink/50" />
          <Line className="h-1.5 w-full bg-ink/10" />
          <Line className="h-1.5 w-[70%] bg-ink/10" />
          <div className="grid grid-cols-3 gap-1.5 pt-1">
            {['bg-[#ff8a6b]/50', 'bg-[#2b3585]/30', 'bg-[#16a34a]/40'].map((tone) => (
              <span key={tone} className={`h-8 rounded-md ${tone}`} />
            ))}
          </div>
        </div>
      </WindowFrame>
      <WindowFrame className="tcx-float absolute top-[4%] left-[28%] z-10 w-[48%]">
        <div className="grid grid-cols-[30%_1fr]">
          <div className="space-y-1.5 bg-[#020626] p-2.5">
            {['w-10', 'w-8', 'w-9', 'w-7'].map((width) => (
              <Line key={width} className={`h-1.5 bg-white/40 ${width}`} />
            ))}
          </div>
          <div className="space-y-2 p-3">
            <Line className="h-2 w-14 bg-ink/60" />
            {[0, 1, 2].map((row) => (
              <div key={row} className="flex items-center gap-2 rounded-md border border-line p-1.5">
                <span className={`h-3 w-3 rounded-full ${['bg-[#ec3f20]', 'bg-[#2b3585]', 'bg-[#f59e0b]'][row]}`} />
                <Line className="h-1.5 w-[60%] bg-ink/20" />
              </div>
            ))}
          </div>
        </div>
      </WindowFrame>
      <WindowFrame className="absolute top-[30%] right-[2%] w-[38%] rotate-[4deg]">
        <div className="space-y-2 p-3">
          <div className="flex items-center gap-2">
            <span className="h-6 w-6 rounded-full bg-[#ec3f20]" />
            <div className="space-y-1">
              <Line className="h-1.5 w-12 bg-ink/50" />
              <Line className="h-1.5 w-8 bg-ink/20" />
            </div>
          </div>
          <Line className="h-1.5 w-full bg-ink/10" />
          <Line className="h-1.5 w-[80%] bg-ink/10" />
          <span className="block h-5 w-16 rounded-md bg-gradient-to-r from-[#ff6b4a] to-[#c91c00]" />
        </div>
      </WindowFrame>
    </div>
  );
}

function Cursor({ name, color, className }: { name: string; color: string; className: string }) {
  return (
    <div className={`absolute flex items-start ${className}`}>
      <svg width="16" height="18" viewBox="0 0 16 18" aria-hidden="true">
        <path d="M1 1 15 8 8.5 9.5 5.5 16Z" fill={color} stroke="#ffffff" strokeWidth="1.2" strokeLinejoin="round" />
      </svg>
      <span className="mt-3 -ml-1 rounded-md px-1.5 py-0.5 text-[10px] font-semibold text-white" style={{ background: color }}>
        {name}
      </span>
    </div>
  );
}

/** A shared document with live cursors and a comment: built for teamwork. */
export function CollaborationIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[300px]">
      <WindowFrame className="absolute inset-x-[6%] top-[8%]">
        <div className="space-y-2 p-3.5">
          <Line className="h-2.5 w-24 bg-ink/60" />
          <Line className="h-1.5 w-full bg-ink/10" />
          <div className="rounded bg-[#ff8a6b]/20 py-0.5">
            <Line className="h-1.5 w-[75%] bg-[#ec3f20]/50" />
          </div>
          <Line className="h-1.5 w-[90%] bg-ink/10" />
          <Line className="h-1.5 w-[60%] bg-ink/10" />
        </div>
      </WindowFrame>
      <Cursor name="Ana" color="#ec3f20" className="tcx-float top-[34%] left-[56%]" />
      <Cursor name="Dev" color="#2b3585" className="tcx-float-alt top-[56%] left-[18%]" />
      <div className="tcx-glass tcx-float absolute right-0 bottom-[2%] flex items-center gap-2 rounded-xl px-2.5 py-2" style={{ animationDelay: '-2s' }}>
        <span className="h-5 w-5 rounded-full bg-[#f59e0b]" />
        <Line className="h-1.5 w-14 bg-ink/30" />
      </div>
    </div>
  );
}

/** An access panel with roles and a lock badge: private by design. */
export function AccessIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[300px]">
      <WindowFrame className="absolute inset-x-[4%] top-[10%]">
        <div className="space-y-2 p-3">
          {[
            { tone: 'bg-[#ec3f20]', width: 'w-12', enabled: true },
            { tone: 'bg-[#2b3585]', width: 'w-10', enabled: true },
            { tone: 'bg-[#f59e0b]', width: 'w-14', enabled: false },
          ].map(({ tone, width, enabled }) => (
            <div key={tone} className="flex items-center gap-2">
              <span className={`h-5 w-5 rounded-full ${tone}`} />
              <Line className={`h-1.5 bg-ink/30 ${width}`} />
              <span className={`relative ml-auto h-3.5 w-6 rounded-full ${enabled ? 'bg-[#16a34a]' : 'bg-ink/15'}`}>
                <span className={`absolute top-0.5 h-2.5 w-2.5 rounded-full bg-white ${enabled ? 'right-0.5' : 'left-0.5'}`} />
              </span>
            </div>
          ))}
        </div>
      </WindowFrame>
      <span className="tcx-float absolute right-[6%] bottom-[4%] grid h-14 w-14 place-items-center rounded-2xl bg-gradient-to-br from-[#ff6b4a] to-[#b31800] text-white shadow-[0_18px_30px_-12px_rgba(201,28,0,0.6)]">
        <Icon name="lock" size={22} strokeWidth={2} />
      </span>
    </div>
  );
}

/** A command palette with a highlighted result: fast and focused. */
export function CommandIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[460px]">
      <div className="absolute inset-x-[6%] top-[10%] overflow-hidden rounded-2xl border border-line bg-surface shadow-[0_30px_60px_-30px_rgba(2,6,38,0.4)]">
        <div className="flex items-center gap-2.5 border-b border-line px-4 py-3 text-muted">
          <Icon name="search" size={15} />
          <span className="flex items-center text-[13px] text-ink">
            Open project
            <span className="ml-0.5 inline-block h-4 w-px animate-pulse bg-primary" />
          </span>
          <span className="ml-auto rounded-md border border-line px-1.5 py-0.5 text-[10px] font-semibold">Ctrl K</span>
        </div>
        <div className="space-y-1 p-2">
          {[true, false, false].map((active, index) => (
            <div key={index} className={`flex items-center gap-3 rounded-lg px-3 py-2 ${active ? 'bg-[var(--tcx-primary-soft)]' : ''}`}>
              <span className={`h-4 w-4 rounded-md ${active ? 'bg-primary' : 'bg-ink/15'}`} />
              <Line className={`h-1.5 ${active ? 'w-32 bg-primary/60' : 'w-24 bg-ink/15'}`} />
              {active ? <Icon name="arrowRight" size={13} className="ml-auto text-primary" /> : null}
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

/** Overlapping light and dark windows: theme choice. */
export function ThemeIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[280px]">
      <div className="absolute top-[6%] left-0 w-[64%] rotate-[-5deg] overflow-hidden rounded-xl border border-white/30 bg-white p-3 shadow-[0_20px_40px_-20px_rgba(0,0,0,0.6)]">
        <Line className="h-2 w-12 bg-[#020626]/60" />
        <Line className="mt-2 h-1.5 w-full bg-[#020626]/10" />
        <Line className="mt-1.5 h-1.5 w-[70%] bg-[#020626]/10" />
        <span className="mt-3 block h-4 w-12 rounded-md bg-[#ec3f20]" />
      </div>
      <div className="tcx-float absolute top-[24%] right-0 w-[64%] rotate-[4deg] overflow-hidden rounded-xl border border-white/10 bg-[#111827] p-3 shadow-[0_20px_40px_-20px_rgba(0,0,0,0.8)]">
        <Line className="h-2 w-12 bg-white/70" />
        <Line className="mt-2 h-1.5 w-full bg-white/15" />
        <Line className="mt-1.5 h-1.5 w-[70%] bg-white/15" />
        <span className="mt-3 block h-4 w-12 rounded-md bg-[#ff7a5c]" />
      </div>
    </div>
  );
}

/** Laptop, tablet and phone outlines: works on any device. */
export function DevicesIllustration() {
  const screen = (
    <div className="space-y-1.5 p-2">
      <Line className="h-1.5 w-[45%] bg-[#ec3f20]/70" />
      <Line className="h-1 w-full bg-ink/15" />
      <Line className="h-1 w-[70%] bg-ink/15" />
      <div className="grid grid-cols-2 gap-1 pt-0.5">
        <span className="h-4 rounded bg-ink/10" />
        <span className="h-4 rounded bg-ink/10" />
      </div>
    </div>
  );

  return (
    <div aria-hidden="true" className="relative mx-auto flex h-full max-w-[460px] items-end justify-center gap-4 pb-2">
      <div className="w-[52%]">
        <div className="aspect-[16/10] overflow-hidden rounded-t-lg border-[5px] border-b-0 border-[#0b1024] bg-surface">{screen}</div>
        <div className="mx-[-8%] h-2 rounded-b-lg bg-gradient-to-b from-[#d7dbe4] to-[#aeb4c2]" />
      </div>
      <div className="tcx-float-alt aspect-[3/4] w-[24%] overflow-hidden rounded-xl border-[5px] border-[#0b1024] bg-surface">{screen}</div>
      <div className="tcx-float aspect-[9/18] w-[13%] overflow-hidden rounded-[14px] border-[4px] border-[#0b1024] bg-surface">{screen}</div>
    </div>
  );
}

/** An invitation email with a join button: step one of getting started. */
export function InviteIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[280px]">
      <WindowFrame className="absolute inset-x-[4%] top-[8%]">
        <div className="space-y-2.5 p-4">
          <div className="flex items-center gap-2">
            <span className="grid h-7 w-7 place-items-center rounded-lg bg-[var(--tcx-primary-soft)] text-primary">
              <Icon name="mail" size={14} />
            </span>
            <div className="space-y-1">
              <Line className="h-1.5 w-20 bg-ink/50" />
              <Line className="h-1.5 w-12 bg-ink/20" />
            </div>
          </div>
          <Line className="h-1.5 w-full bg-ink/10" />
          <Line className="h-1.5 w-[75%] bg-ink/10" />
          <span className="block h-6 w-24 rounded-lg bg-gradient-to-r from-[#ff6b4a] to-[#c91c00]" />
        </div>
      </WindowFrame>
      <span className="tcx-float absolute right-[2%] bottom-[6%] grid h-11 w-11 place-items-center rounded-2xl bg-[#2b3585] text-white shadow-[0_16px_30px_-12px_rgba(43,53,133,0.7)]">
        <Icon name="userPlus" size={18} />
      </span>
    </div>
  );
}

/** A password form with a strength meter: step two of getting started. */
export function PasswordIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[280px]">
      <WindowFrame className="absolute inset-x-[4%] top-[8%]">
        <div className="space-y-2.5 p-4">
          <Line className="h-1.5 w-14 bg-ink/40" />
          <div className="flex h-7 items-center gap-1 rounded-lg border border-line px-2.5">
            {Array.from({ length: 9 }, (_, index) => (
              <span key={index} className="h-1.5 w-1.5 rounded-full bg-ink/60" />
            ))}
          </div>
          <div className="grid grid-cols-4 gap-1">
            {['bg-[#16a34a]', 'bg-[#16a34a]', 'bg-[#16a34a]', 'bg-ink/10'].map((tone, index) => (
              <span key={index} className={`h-1 rounded-full ${tone}`} />
            ))}
          </div>
          <span className="block h-6 w-full rounded-lg bg-ink/85" />
        </div>
      </WindowFrame>
      <span className="tcx-float absolute right-[2%] bottom-[6%] grid h-11 w-11 place-items-center rounded-2xl bg-[#16a34a] text-white shadow-[0_16px_30px_-12px_rgba(22,163,74,0.7)]">
        <Icon name="key" size={18} />
      </span>
    </div>
  );
}

/** A workspace window with teammates present: step three of getting started. */
export function WorkspaceReadyIllustration() {
  return (
    <div aria-hidden="true" className="relative mx-auto h-full max-w-[280px]">
      <WindowFrame className="absolute inset-x-[4%] top-[8%]">
        <div className="grid grid-cols-[28%_1fr]">
          <div className="space-y-1.5 bg-[#020626] p-2.5">
            {['w-9', 'w-7', 'w-8', 'w-6'].map((width) => (
              <Line key={width} className={`h-1.5 bg-white/40 ${width}`} />
            ))}
          </div>
          <div className="space-y-2 p-3">
            <div className="flex items-center justify-between">
              <Line className="h-2 w-14 bg-ink/60" />
              <div className="flex -space-x-1.5">
                {['bg-[#ec3f20]', 'bg-[#2b3585]', 'bg-[#f59e0b]'].map((tone) => (
                  <span key={tone} className={`h-4 w-4 rounded-full border-2 border-surface ${tone}`} />
                ))}
              </div>
            </div>
            {[0, 1].map((row) => (
              <div key={row} className="flex items-center gap-2 rounded-md border border-line p-1.5">
                <span className={`h-2.5 w-2.5 rounded-full ${row === 0 ? 'bg-[#16a34a]' : 'border border-line-2'}`} />
                <Line className="h-1.5 w-[60%] bg-ink/20" />
              </div>
            ))}
          </div>
        </div>
      </WindowFrame>
      <span className="tcx-float absolute right-[2%] bottom-[6%] grid h-11 w-11 place-items-center rounded-2xl bg-gradient-to-br from-[#ff6b4a] to-[#b31800] text-white shadow-[0_16px_30px_-12px_rgba(201,28,0,0.6)]">
        <Icon name="sparkles" size={18} />
      </span>
    </div>
  );
}
