// 三张架构图：纯 SVG，暗色科技主题

const D2_TONES = {
  blue:   { fill: 'rgba(86,140,235,.13)',  stroke: 'rgba(112,163,242,.48)', title: '#CBDDF7' },
  purple: { fill: 'rgba(160,120,230,.13)', stroke: 'rgba(176,142,236,.46)', title: '#DCCEF7' },
  teal:   { fill: 'rgba(18,160,147,.13)',  stroke: 'rgba(64,201,183,.46)',  title: '#BEEDE6' },
  orange: { fill: 'rgba(200,129,58,.13)',  stroke: 'rgba(212,152,82,.48)',  title: '#F2D9BE' },
  hal:    { fill: 'rgba(200,129,58,.18)',  stroke: 'rgba(228,170,98,.62)',  title: '#F8E3C6' },
  gray:   { fill: 'rgba(255,255,255,.045)',stroke: 'rgba(255,255,255,.16)', title: '#E9E5DE' },
  green:  { fill: 'rgba(150,200,90,.11)',  stroke: 'rgba(162,212,107,.42)', title: '#DCEFC2' },
};

const D2_LINES = {
  blue:   '#5B93E8',
  orange: '#D89448',
  purple: '#A480E0',
  green:  '#93C45C',
  dark:   'rgba(255,255,255,.45)',
};

const D2Markers = () => (
  <defs>
    {Object.entries(D2_LINES).map(([k, c]) => (
      <marker key={k} id={`d2-${k}`} viewBox="0 0 10 8" refX="9" refY="4"
              markerWidth="9" markerHeight="8" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M0,0 L10,4 L0,8 Z" fill={c} />
      </marker>
    ))}
    {Object.entries(D2_LINES).map(([k, c]) => (
      <marker key={k + 's'} id={`d2-${k}-s`} viewBox="0 0 10 8" refX="1" refY="4"
              markerWidth="9" markerHeight="8" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M10,0 L0,4 L10,8 Z" fill={c} />
      </marker>
    ))}
  </defs>
);

const d2path = (pts, r = 8) => {
  let d = `M ${pts[0][0]} ${pts[0][1]}`;
  for (let i = 1; i < pts.length - 1; i++) {
    const [x0, y0] = pts[i - 1], [x1, y1] = pts[i], [x2, y2] = pts[i + 1];
    const d1 = Math.hypot(x1 - x0, y1 - y0), d2 = Math.hypot(x2 - x1, y2 - y1);
    if (!d1 || !d2) { d += ` L ${x1} ${y1}`; continue; }
    const rr = Math.min(r, d1 / 2, d2 / 2);
    const p1 = [x1 + (x0 - x1) / d1 * rr, y1 + (y0 - y1) / d1 * rr];
    const p2 = [x1 + (x2 - x1) / d2 * rr, y1 + (y2 - y1) / d2 * rr];
    d += ` L ${p1[0]} ${p1[1]} Q ${x1} ${y1} ${p2[0]} ${p2[1]}`;
  }
  const last = pts[pts.length - 1];
  return d + ` L ${last[0]} ${last[1]}`;
};

const D2Arrow = ({ pts, tone = 'blue', dashed, both }) => {
  const c = D2_LINES[tone];
  return (
    <path d={d2path(pts)} fill="none" stroke={c} strokeWidth="1.6"
          strokeLinecap="round" strokeLinejoin="round"
          strokeDasharray={dashed ? '6 5' : undefined}
          markerEnd={`url(#d2-${tone})`}
          markerStart={both ? `url(#d2-${tone}-s)` : undefined} />
  );
};

const D2Box = ({ x, y, w, h, tone = 'blue', title, sub = [], children }) => {
  const t = D2_TONES[tone];
  const x2 = x + w / 2;
  if (children) {
    return (
      <g>
        <rect x={x} y={y} width={w} height={h} rx="9" fill={t.fill} stroke={t.stroke} strokeWidth="1.6" />
        {children}
      </g>
    );
  }
  const block = 24 + sub.length * 20;
  const top = y + (h - block) / 2;
  return (
    <g>
      <rect x={x} y={y} width={w} height={h} rx="9" fill={t.fill} stroke={t.stroke} strokeWidth="1.6" />
      <text x={x2} y={top + 18} textAnchor="middle" className="n-title" fill={t.title}>{title}</text>
      {sub.map((s, i) => (
        <text key={i} x={x2} y={top + 42 + i * 20} textAnchor="middle" className="n-sub">{s}</text>
      ))}
    </g>
  );
};

const BinderPill = ({ x, y, w = 54, h = 19 }) => (
  <g>
    <rect x={x - w / 2} y={y - h / 2} width={w} height={h} rx={h / 2}
          fill="#0A1113" stroke="rgba(176,142,236,.55)" strokeWidth="1.3" />
    <text x={x} y={y + 3.2} textAnchor="middle" className="binder">Binder</text>
  </g>
);

const D2Region = ({ x, y, w, h, label }) => (
  <g>
    <rect x={x} y={y} width={w} height={h} rx="12" fill="none"
          stroke="rgba(255,255,255,.10)" strokeWidth="1" strokeDasharray="6 6" />
    <text x={x + 16} y={y + 22} className="band-label">{label}</text>
  </g>
);

// ======================= 图 1：整体架构 =======================
const SvgArch = () => (
  <svg viewBox="0 0 1280 1010" className="dfx" role="img"
       aria-label="Multi Room 整体架构：无线音箱、应用层、Framework 层、Native 音频服务、Vendor 层与硬件层">
    <D2Markers />
    <text x="640" y="34" textAnchor="middle" className="d-title">Multi Room 整体架构</text>

    {/* ===== 分层底色 ===== */}
    <rect x="16" y="58" width="1248" height="122" rx="10" fill="rgba(150,200,90,.05)" />
    <text x="32" y="80" className="band-label">无线音箱</text>
    <rect x="16" y="186" width="1248" height="120" rx="10" fill="rgba(86,140,235,.055)" />
    <text x="32" y="208" className="band-label">应用层</text>
    <rect x="16" y="312" width="1248" height="108" rx="10" fill="rgba(160,120,230,.055)" />
    <text x="32" y="334" className="band-label">Framework 层（Java）</text>
    <rect x="16" y="426" width="1248" height="130" rx="10" fill="rgba(18,160,147,.06)" />
    <text x="700" y="446" className="band-label">Native 音频服务（audioserver）</text>
    <rect x="16" y="562" width="1248" height="228" rx="10" fill="rgba(200,129,58,.05)" />
    <text x="32" y="584" className="band-label">Vendor 层</text>
    <rect x="16" y="796" width="1248" height="134" rx="10" fill="rgba(255,255,255,.028)" />
    <text x="32" y="818" className="band-label">硬件</text>

    {/* ===== 连线 ===== */}
    <D2Arrow pts={[[180, 296], [180, 338]]} tone="blue" />
    <D2Arrow pts={[[180, 408], [180, 462]]} tone="blue" />
    <D2Arrow pts={[[493, 408], [493, 462]]} tone="purple" />
    <D2Arrow pts={[[352, 500], [300, 500]]} tone="dark" />
    <D2Arrow pts={[[740, 296], [740, 338]]} tone="dark" />
    <D2Arrow pts={[[180, 538], [180, 596]]} tone="blue" />
    <D2Arrow pts={[[180, 778], [180, 826]]} tone="blue" />
    <D2Arrow pts={[[496, 778], [496, 826]]} tone="blue" />
    <D2Arrow pts={[[620, 253], [672, 253]]} tone="dark" />
    <D2Arrow pts={[[682, 650], [815, 650]]} tone="orange" />
    <D2Arrow pts={[[940, 588], [940, 296]]} tone="orange" dashed />
    <D2Arrow pts={[[1160, 588], [1160, 296]]} tone="orange" />
    <D2Arrow pts={[[950, 210], [950, 162]]} tone="green" />

    {/* ===== 节点 ===== */}
    <D2Box x={690} y={70} w={550} h={92} tone="green"
           title="无线音箱（客厅 / 卧室 / 厨房...）"
           sub={['通过 Wi-Fi 接收 Soundbar 发出的声音，多个房间同时播放']} />

    <D2Box x={42} y={210} w={278} h={86} tone="blue" title="播放应用"
           sub={['电视节目 / HDMI / 在线视频', '正常播放声音']} />
    <D2Box x={352} y={210} w={268} h={86} tone="blue" title="TVUI（设置界面）"
           sub={['配对音箱 · 选择房间', '调音量 / 静音']} />
    <D2Box x={676} y={210} w={562} h={86} tone="blue" title="TVBG（后台服务）"
           sub={['连接无线音箱 · 把声音通过 Wi-Fi 发给音箱', '计算还需多少延迟 · 打开热点']} />

    <D2Box x={42} y={338} w={278} h={70} tone="purple" title="AudioTrack"
           sub={['应用播放声音的入口']} />
    <D2Box x={352} y={338} w={282} h={70} tone="purple" title="AudioManager"
           sub={['音量 / 静音调节']} />
    <D2Box x={676} y={338} w={250} h={70} tone="purple" title="Wi-Fi 热点管理"
           sub={['热点空闲、关屏时不自动关闭']} />

    <D2Box x={42} y={462} w={258} h={76} tone="teal" title="AudioFlinger"
           sub={['把各路声音混在一起', '交给 Audio HAL 播放']} />
    <D2Box x={352} y={462} w={282} h={76} tone="teal" title="AudioPolicy"
           sub={['决定声音从哪个设备输出', '管理各类声音的音量']} />

    <D2Box x={42} y={596} w={640} h={182} tone="hal">
      <text x={362} y={634} textAnchor="middle" className="n-title" fill="#F8E3C6">Audio HAL</text>
      <text x={362} y={660} textAnchor="middle" className="n-sub">MS12 解码 + 音效处理</text>
      <text x={362} y={692} textAnchor="middle" className="n-sub">① 复制一份立体声，送给 音频服务</text>
      <text x={362} y={714} textAnchor="middle" className="n-sub">② 本地喇叭按要求延迟，等无线音箱</text>
      <text x={362} y={736} textAnchor="middle" className="n-sub">③ 电视画面同步延迟，保证声画同步</text>
      <text x={362} y={758} textAnchor="middle" className="n-sub">④ 把平台自身的延迟报给 TVBG</text>
    </D2Box>

    <D2Box x={815} y={588} w={423} h={190} tone="orange">
      <text x={1026} y={624} textAnchor="middle" className="n-title" fill="#F2D9BE">音频服务</text>
      <text x={1026} y={650} textAnchor="middle" className="n-sub">TVBG 和 Audio HAL 之间的“中间人”</text>
      <text x={1026} y={680} textAnchor="middle" className="n-sub">向上：把声音、平台延迟交给 TVBG</text>
      <text x={1026} y={702} textAnchor="middle" className="n-sub">向下：把连接 / 断开 / 延迟指令交给 HAL</text>
      <text x={1026} y={724} textAnchor="middle" className="n-sub">只有连上音箱才转发声音</text>
    </D2Box>

    <D2Box x={42} y={826} w={278} h={86} tone="gray" title="本地喇叭" sub={['Soundbar 自带喇叭']} />
    <D2Box x={352} y={826} w={288} h={86} tone="gray" title="电视画面" sub={['视频按要求延迟']} />
    <D2Box x={676} y={826} w={562} h={86} tone="gray" title="Wi-Fi 芯片"
           sub={['连家庭路由，或开 Soundbar 热点（2.4G / 5G）']} />

    {/* ===== Binder / 标注 ===== */}
    <BinderPill x={180} y={435} />
    <BinderPill x={493} y={435} />
    <BinderPill x={748} y={650} />
    <BinderPill x={940} y={452} />
    <BinderPill x={1160} y={452} />

    <text x={646} y={245} textAnchor="middle" className="n-tag">设置</text>
    <text x={326} y={492} textAnchor="middle" className="n-tag">选设备</text>
    <text x={752} y={322} className="n-tag">开热点</text>
    <text x={508} y={808} className="n-tag">画面延迟</text>
    <text x={962} y={194} className="n-tag green">Wi-Fi 发送声音</text>
    <text x={748} y={636} textAnchor="middle" className="n-tag orange">声音 + 平台延迟</text>
    <text x={952} y={540} className="n-tag orange">连接 / 断开 / 延迟指令</text>
    <text x={1148} y={500} textAnchor="end" className="n-tag orange">声音数据 · 平台延迟</text>

    {/* ===== 图例与说明 ===== */}
    <line x1="32" y1="966" x2="72" y2="966" stroke={D2_LINES.blue} strokeWidth="1.8" markerEnd="url(#d2-blue)" />
    <text x="82" y="970" className="legend">本地播放声音</text>
    <line x1="236" y1="966" x2="276" y2="966" stroke={D2_LINES.orange} strokeWidth="1.8" markerEnd="url(#d2-orange)" />
    <text x="286" y="970" className="legend">声音 / 指令</text>
    <line x1="498" y1="966" x2="538" y2="966" stroke={D2_LINES.purple} strokeWidth="1.8" markerEnd="url(#d2-purple)" />
    <text x="548" y="970" className="legend">音量控制</text>
    <BinderPill x={642} y={966} />
    <text x="678" y="970" className="legend">跨进程调用（Binder IPC）</text>

    <text x="32" y="1000" className="d-note">
      说明：TVBG、音频服务、Audio HAL 分属不同进程，相互之间都通过 Binder 通信。
    </text>
  </svg>
);

// ======================= 图 2：声音通路与声画同步 =======================
const SvgFlow = () => (
  <svg viewBox="0 0 1280 700" className="dfx" role="img"
       aria-label="声音怎么送到无线音箱、怎么做到声画同步的数据通路图">
    <D2Markers />
    <text x="640" y="36" textAnchor="middle" className="d-title">声音怎么送到无线音箱，怎么做到声画同步</text>

    {/* 连线 */}
    <D2Arrow pts={[[190, 365], [216, 365]]} tone="blue" />
    <D2Arrow pts={[[372, 365], [420, 365]]} tone="blue" />
    <D2Arrow pts={[[590, 365], [620, 365]]} tone="blue" />
    <D2Arrow pts={[[505, 262], [505, 322]]} tone="dark" />
    <D2Arrow pts={[[800, 170], [860, 170]]} tone="orange" />
    <D2Arrow pts={[[1030, 130], [1075, 130]]} tone="orange" />
    <D2Arrow pts={[[1160, 164], [1160, 228]]} tone="green" />
    <D2Arrow pts={[[800, 371], [870, 371]]} tone="orange" />
    <D2Arrow pts={[[800, 493], [870, 493]]} tone="orange" />
    <D2Arrow pts={[[1035, 371], [1085, 371]]} tone="blue" />
    <D2Arrow pts={[[1035, 493], [1085, 493]]} tone="blue" />

    {/* TVBG 控制域 */}
    <path d="M1040 64 L1266 64 L1266 550 L852 550 L852 320 L1040 320 Z" fill="none"
          stroke="rgba(216,148,72,.55)" strokeWidth="1.4" strokeDasharray="7 6" strokeLinejoin="round" />
    <text x="1028" y="302" textAnchor="end" className="n-tag orange">TVBG 下发「需要多等多久」</text>

    {/* 节点 */}
    <D2Box x={24} y={322} w={166} h={86} tone="blue" title="播放应用" sub={['播放声音']} />
    <D2Box x={216} y={322} w={156} h={86} tone="purple" title="AudioTrack" sub={['声音入口']} />
    <D2Box x={420} y={322} w={170} h={86} tone="teal" title="AudioFlinger" sub={['混音']} />
    <D2Box x={420} y={176} w={170} h={86} tone="teal" title="AudioPolicy" sub={['选出设备']} />

    <D2Box x={620} y={150} w={180} h={410} tone="hal">
      <text x={710} y={250} textAnchor="middle" className="n-title" fill="#F8E3C6">Audio HAL</text>
      <text x={710} y={278} textAnchor="middle" className="n-sub">MS12 解码</text>
      <text x={710} y={300} textAnchor="middle" className="n-sub">+ 音效处理</text>
      <text x={710} y={350} textAnchor="middle" className="n-sub">一份给音箱</text>
      <text x={710} y={372} textAnchor="middle" className="n-sub">一份给本地喇叭</text>
      <text x={710} y={394} textAnchor="middle" className="n-sub">同时控制画面延迟</text>
    </D2Box>

    <D2Box x={860} y={104} w={170} h={80} tone="orange"
           title="音频服务" sub={['只在连上音箱时转发']} />
    <D2Box x={1075} y={84} w={170} h={80} tone="blue" title="TVBG" sub={['打包发送']} />
    <D2Box x={1075} y={228} w={170} h={80} tone="green" title="无线音箱" sub={['经 Wi-Fi 收到声音']} />

    <D2Box x={870} y={330} w={165} h={82} tone="orange" title="额外延迟" sub={['等无线音箱']} />
    <D2Box x={870} y={452} w={165} h={82} tone="orange" title="额外延迟" sub={['画面同步推迟']} />
    <D2Box x={1085} y={330} w={165} h={82} tone="gray" title="本地喇叭" sub={['Soundbar 自带']} />
    <D2Box x={1085} y={452} w={165} h={82} tone="gray" title="电视画面" sub={['视频']} />

    {/* Binder / 标注 */}
    <BinderPill x={396} y={365} w={48} />
    <BinderPill x={830} y={170} w={48} />
    <BinderPill x={1052} y={130} w={44} />
    <text x={830} y={158} textAnchor="middle" className="n-tag orange">20ms/包</text>
    <text x={1168} y={200} className="n-tag green">Wi-Fi</text>
    <text x={517} y={296} className="n-tag">路由</text>

    {/* 底部说明 */}
    <rect x="24" y="586" width="1258" height="106" rx="10"
          fill="rgba(200,129,58,.08)" stroke="rgba(216,160,90,.32)" strokeWidth="1.2" />
    <text x="46" y="612" className="n-title" fill="#EBC79B">声画同步的思路：</text>
    <text x="46" y="640" className="n-sub">① 平台把自己处理声音的延迟（音效、HDMI / ARC 等，实测约 137ms）报给 TVBG；</text>
    <text x="46" y="662" className="n-sub">② TVBG 算出无线音箱比本地慢多少，下发“额外延迟”；③ 本地喇叭和电视画面都推迟同样的时间，三者同时到达；</text>
    <text x="46" y="684" className="n-sub">④ 断开音箱后额外延迟清零，本地喇叭与画面恢复正常。</text>
  </svg>
);

// ======================= 图 3：SoftAP 在系统中的位置 =======================
const SvgSoftAp = () => (
  <svg viewBox="0 0 1280 430" className="dfx" role="img"
       aria-label="Multi Channel SoftAP 在系统中的位置：TVBG、Wi-Fi 热点管理、Wi-Fi 芯片、家庭路由器与无线音箱">
    <D2Markers />

    <D2Region x={32} y={54} w={300} h={306} label="应用层" />
    <D2Region x={352} y={54} w={300} h={306} label="Framework 层" />
    <D2Region x={672} y={54} w={296} h={306} label="硬件（Wi-Fi 芯片）" />
    <D2Region x={992} y={54} w={256} h={306} label="外部设备" />

    <D2Arrow pts={[[308, 207], [380, 207]]} tone="dark" />
    <D2Arrow pts={[[600, 207], [700, 207]]} tone="orange" />
    <D2Arrow pts={[[960, 180], [1006, 180], [1006, 132], [1054, 132]]} tone="blue" />
    <D2Arrow pts={[[960, 234], [1006, 234], [1006, 288], [1054, 288]]} tone="orange" />

    <D2Box x={56} y={152} w={252} h={110} tone="blue" title="TVBG"
           sub={['申请 / 保持热点']} />
    <D2Box x={380} y={152} w={220} h={110} tone="purple" title="Wi-Fi 热点管理"
           sub={['打开并保持 2.4G / 5G']} />
    <D2Box x={700} y={152} w={260} h={110} tone="teal" title="Wi-Fi 芯片"
           sub={['STA + SoftAP 并发']} />

    <D2Box x={1054} y={86} w={176} h={92} tone="gray" title="家庭路由器" sub={['上网（STA）']} />
    <D2Box x={1054} y={242} w={176} h={92} tone="green" title="无线音箱" sub={['连热点（SoftAP）']} />

    <BinderPill x={344} y={207} w={52} />

    <text x={640} y={404} textAnchor="middle" className="d-note">
      音箱既可直连 Soundbar 热点，也可接入家庭路由与电视处在同一网段；热点由平台保持，空闲与关屏都不自动关闭。
    </text>
  </svg>
);

Object.assign(window, { SvgArch, SvgFlow, SvgSoftAp });