/* global React */

// ============================================================
// China CDN, "What is China CDN" + "Why UD"
// What: topology diagram (origin → edge nodes around China → users)
// Why: 4 trimmed selling points (from 6), no pills/eyebrows
// ============================================================
const CH_WHAT_CSS = `
.CH-what{position:relative;background:#fff;padding:var(--section-pad-y-d) 0;font-family:'Inter','Noto Sans HK','Noto Sans TC',sans-serif;color:#0F2A47}
.CH-what-head{max-width:none;margin-bottom:48px}
.CH-what-head .CH-lede,.CH-what-head .CH-lede.hl{max-width:none}
.CH-maps{display:grid;grid-template-columns:1fr 1fr;gap:24px;align-items:start}
.CH-maps .CH-map{max-width:none;justify-self:stretch}
.CH-maps .CH-map-toggle{display:none}
.CH-maps .CH-map-legend{display:none}
@media (max-width:900px){.CH-maps{grid-template-columns:1fr}}

.CH-h2{margin:0;font-size:clamp(36px,4.2vw,56px);line-height:1.08;font-weight:700;letter-spacing:-1.6px;color:#0F2A47;text-wrap:balance;max-width:780px}
.CH-h2 .accent{background:linear-gradient(100deg,#A855F7 0%,#7C3AED 100%);-webkit-background-clip:text;background-clip:text;color:transparent}
.CH-lede{margin-top:24px;font-size:17px;line-height:1.8;color:#475569;max-width:560px;text-wrap:pretty}
.CH-lede strong{color:#0F2A47;font-weight:600}
.CH-lede + .CH-lede{margin-top:18px}
.CH-lede.hl{margin-top:22px;background:linear-gradient(180deg,#F6F1FE 0%,#F1EAFD 100%);border:1px solid rgba(139,92,246,0.22);border-radius:14px;padding:22px 24px;color:#3B2A5A;box-shadow:0 6px 20px rgba(139,92,246,0.10)}
.CH-lede.hl strong{color:#6D28D9}

/* China speed map (red = slow / no CDN, green = fast / UD CDN) */
.CH-map{position:relative;width:100%;max-width:540px;justify-self:end;display:flex;flex-direction:column;gap:16px}
.CH-map-toggle{display:flex;align-self:flex-start;background:#F1EEF9;border:1px solid rgba(15,42,71,0.10);border-radius:999px;padding:4px;gap:2px}
.CH-map-toggle button{appearance:none;border:0;background:transparent;cursor:pointer;font-family:'Noto Sans HK','Inter',sans-serif;font-size:13.5px;font-weight:700;letter-spacing:-.1px;color:#64748b;padding:9px 17px;border-radius:999px;white-space:nowrap;display:flex;align-items:center;gap:8px}
.CH-map-toggle button .dot{width:9px;height:9px;border-radius:50%;flex:none;background:currentColor}
.CH-map-toggle button[data-on="slow"].on{background:#fff;color:#dc2626;box-shadow:0 2px 8px rgba(15,42,71,0.10)}
.CH-map-toggle button[data-on="fast"].on{background:#fff;color:#15803d;box-shadow:0 2px 8px rgba(15,42,71,0.10)}
.CH-map-stage{position:relative;width:100%;aspect-ratio:600/520;border-radius:20px;overflow:hidden;background:radial-gradient(125% 120% at 50% 0%,#FBFAFE 0%,#F4F1FB 100%);border:1px solid rgba(15,42,71,0.08)}
.CH-map-stage svg{position:absolute;inset:0;width:100%;height:100%;display:block}
.CH-map-badge{position:absolute;left:16px;bottom:16px;display:flex;align-items:center;gap:9px;font-family:'Noto Sans HK','Inter',sans-serif;font-size:13.5px;font-weight:800;padding:8px 14px;border-radius:999px;background:rgba(255,255,255,0.94);backdrop-filter:blur(4px);box-shadow:0 3px 12px rgba(15,42,71,0.10)}
.CH-map-badge .ms{font-family:'Chakra Petch',ui-monospace,monospace}
.CH-map-legend{display:flex;gap:24px;flex-wrap:wrap;font-family:'Noto Sans HK','Inter',sans-serif}
.CH-map-legend div{display:flex;align-items:center;gap:9px;font-size:13px;font-weight:600;color:#475569}
.CH-map-legend .sw{width:24px;border-top:3px dashed;flex:none}

@media (max-width: 1024px){
  .CH-what{padding:var(--section-pad-y-t) 0}
  .CH-what-inner{grid-template-columns:1fr;gap:56px}
  .CH-map{justify-self:start;max-width:520px}
}
@media (max-width: 640px){
  .CH-what{padding:var(--section-pad-y-m) 0}
  .CH-h2{font-size:32px;letter-spacing:-1px}
  .CH-lede{font-size:15.5px}
}

/* ---------- Why UD China CDN (4 cards) ---------- */
.CH-why{position:relative;background:linear-gradient(180deg,#FAFAFE 0%,#F6F4FB 100%);padding:var(--section-pad-y-d) 0;border-top:1px solid rgba(15,42,71,0.06);border-bottom:1px solid rgba(15,42,71,0.06);font-family:'Inter','Noto Sans HK','Noto Sans TC',sans-serif;color:#0F2A47}
.CH-why-head{max-width:780px;margin-bottom:56px}
.CH-why-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:20px}
.CH-why-card{position:relative;background:#fff;border:1px solid rgba(15,42,71,0.08);border-radius:20px;padding:34px 32px 32px;transition:border-color .25s,transform .25s,box-shadow .25s;display:flex;flex-direction:column;gap:18px;overflow:hidden}
.CH-why-card::before{content:'';position:absolute;top:0;left:0;right:0;height:3px;background:linear-gradient(90deg,#A855F7,#7C3AED);transform:scaleX(0);transform-origin:left;transition:transform .3s ease}
.CH-why-card:hover::before{transform:scaleX(1)}
.CH-why-card:hover{border-color:rgba(139,92,246,0.30);box-shadow:0 18px 44px rgba(124,58,237,0.13);transform:translateY(-3px)}
.CH-why-card.flag{background:linear-gradient(150deg,#15233D 0%,#0F2A47 55%,#241a45 145%);color:#fff;border-color:transparent}
.CH-why-card.flag::after{content:'';position:absolute;top:-90px;right:-70px;width:260px;height:260px;background:radial-gradient(circle,rgba(139,92,246,0.34) 0%,transparent 70%);pointer-events:none}
.CH-why-card.flag::before{background:linear-gradient(90deg,#C4B5FD,#A855F7)}
.CH-why-card.flag:hover{box-shadow:0 20px 48px rgba(15,42,71,0.40)}

.CH-why-top{position:relative;z-index:1;display:flex;align-items:center;justify-content:space-between}
.CH-why-icon{width:58px;height:58px;border-radius:16px;background:linear-gradient(135deg,#F4ECFE 0%,#ECE1FD 100%);color:#7C3AED;display:flex;align-items:center;justify-content:center;box-shadow:inset 0 0 0 1px rgba(139,92,246,0.14)}
.CH-why-icon svg{width:34px;height:34px;overflow:visible}
.CH-why-card.flag .CH-why-icon{background:linear-gradient(135deg,rgba(168,85,247,0.32) 0%,rgba(124,58,237,0.30) 100%);color:#DDD0FE;box-shadow:inset 0 0 0 1px rgba(196,181,253,0.28)}
.CH-why-idx{font-family:'Chakra Petch','Inter',sans-serif;font-size:30px;font-weight:600;letter-spacing:.5px;color:rgba(139,92,246,0.18);font-variant-numeric:tabular-nums;line-height:1}
.CH-why-card.flag .CH-why-idx{color:rgba(196,181,253,0.38)}

.CH-why-h3{position:relative;z-index:1;margin:0;font-size:22px;font-weight:700;letter-spacing:-.3px;line-height:1.3;text-wrap:balance;color:inherit}
.CH-why-p{position:relative;z-index:1;margin:0;font-size:15px;line-height:1.7;color:#475569;text-wrap:pretty}
.CH-why-card.flag .CH-why-p{color:rgba(255,255,255,0.78)}

@media (max-width: 1024px){
  .CH-why{padding:var(--section-pad-y-t) 0}
}
@media (max-width: 800px){
  .CH-why-grid{grid-template-columns:1fr;gap:14px}
  .CH-why-card{padding:30px 26px 28px}
}
@media (max-width: 640px){
  .CH-why{padding:var(--section-pad-y-m) 0}
  .CH-why-head{margin-bottom:40px}
}
`;

// ---- China speed map: red (slow / no CDN) vs green (fast / UD CDN) ----
// Stylized China silhouette. Two stories on the same map:
//   slow → traffic detours to an overseas origin (long red routes)
//   fast → an edge node sits right next to each city (short green hops)
const CHINA_PATH = "M 96 250 C 70 215 78 165 120 140 C 150 122 165 150 200 140 C 235 130 250 96 300 100 C 345 104 360 132 405 120 C 450 108 470 92 510 110 C 548 128 556 150 540 175 C 528 195 500 192 492 212 C 486 228 470 226 452 232 C 470 244 486 250 478 266 C 470 280 452 276 458 296 C 470 312 470 330 452 350 C 436 372 420 384 402 398 C 380 412 356 410 336 420 C 312 430 300 414 286 420 C 262 432 240 410 210 402 C 170 390 140 360 116 322 C 96 292 100 272 96 250 Z";

const CH_CITIES = [
  { name: '北京', x: 436, y: 206, node: { x: 408, y: 196 }, slow: '296ms', fast: '38ms' },
  { name: '西安', x: 360, y: 258, node: { x: 333, y: 250 }, slow: '281ms', fast: '44ms' },
  { name: '成都', x: 286, y: 300, node: { x: 259, y: 290 }, slow: '312ms', fast: '47ms' },
  { name: '上海', x: 462, y: 300, node: { x: 434, y: 304 }, slow: '274ms', fast: '31ms' },
  { name: '香港', x: 403, y: 398, node: { x: 376, y: 392 }, slow: '268ms', fast: '22ms' },
];
const CH_ORIGIN = { x: 58, y: 66 };
// Dramatic detour path overseas → 香港, used for the slow packet
const SLOW_HERO = `M ${CH_ORIGIN.x} ${CH_ORIGIN.y} C 150 230 545 175 545 320 C 545 415 470 405 403 398`;

function ChinaSpeedMap({ force }) {
  const [mode, setMode] = React.useState(force || 'slow');
  const pick = (m) => setMode(m);

  const fast = mode === 'fast';
  const accent = fast ? '#16a34a' : '#dc2626';
  const fill = fast ? 'rgba(34,197,94,0.16)' : 'rgba(239,68,68,0.15)';
  const stroke = fast ? 'rgba(22,163,74,0.55)' : 'rgba(220,38,38,0.50)';

  return (
    <div className="CH-map">
      <div className="CH-map-toggle" role="tablist" aria-label="中國 CDN 對比">
        <button data-on="slow" className={fast ? '' : 'on'} onClick={() => pick('slow')}>
          <span className="dot" style={{ background: '#dc2626' }} />沒有 CDN
        </button>
        <button data-on="fast" className={fast ? 'on' : ''} onClick={() => pick('fast')}>
          <span className="dot" style={{ background: '#16a34a' }} />用 UD 中國 CDN
        </button>
      </div>

      <div className="CH-map-stage">
        <svg viewBox="0 0 600 520" aria-hidden="true">
          {/* China landmass */}
          <path d={CHINA_PATH} fill={fill} stroke={stroke} strokeWidth="2" />
          <ellipse cx="372" cy="452" rx="19" ry="12" fill={fill} stroke={stroke} strokeWidth="1.6" />
          <text x="150" y="205" fontFamily="'Noto Sans HK','Inter',sans-serif"
            fontSize="15" fontWeight="800" fill="rgba(15,42,71,0.30)" letterSpacing="3">中國</text>

          {/* SLOW: overseas origin + long red detours */}
          {!fast && (
            <g>
              {CH_CITIES.map((c, i) => (
                <path key={`r-${i}`}
                  d={`M ${CH_ORIGIN.x} ${CH_ORIGIN.y} Q ${(CH_ORIGIN.x + c.x) / 2} ${Math.min(CH_ORIGIN.y, c.y) - 46} ${c.x} ${c.y}`}
                  fill="none" stroke="#ef4444" strokeWidth="1.6" strokeDasharray="5 6" opacity="0.5" />
              ))}
              <circle r="4.5" fill="#fff" stroke="#dc2626" strokeWidth="2.2">
                <animateMotion dur="5s" repeatCount="indefinite" calcMode="linear"
                  keyPoints="0;0.34;0.4;0.74;0.8;1" keyTimes="0;0.3;0.46;0.74;0.86;1" path={SLOW_HERO} />
              </circle>
              {/* overseas origin server */}
              <g transform={`translate(${CH_ORIGIN.x - 26}, ${CH_ORIGIN.y - 20})`}>
                <rect x="0" y="0" width="52" height="40" rx="8" fill="#fff" stroke="#0F2A47" strokeWidth="2" />
                <line x1="10" y1="13" x2="42" y2="13" stroke="#0F2A47" strokeWidth="1.8" />
                <line x1="10" y1="21" x2="42" y2="21" stroke="#0F2A47" strokeWidth="1.8" />
                <line x1="10" y1="29" x2="32" y2="29" stroke="#0F2A47" strokeWidth="1.8" />
              </g>
              <text x={CH_ORIGIN.x} y={CH_ORIGIN.y + 36} textAnchor="middle"
                fontFamily="'Noto Sans HK','Inter',sans-serif" fontSize="12.5" fontWeight="700" fill="#0F2A47">海外源站</text>
            </g>
          )}

          {/* FAST: a single Hong Kong node transmits out to every mainland region */}
          {fast && (
            <g>
              {CH_CITIES.filter((c) => c.name !== '香港').map((c, i) => {
                const d = `M 403 396 Q ${(403 + c.x) / 2} ${Math.min(396, c.y) - 36} ${c.x} ${c.y}`;
                return (
                  <g key={`g-${i}`}>
                    <path d={d} fill="none" stroke="#16a34a" strokeWidth="2" opacity="0.5" />
                    <circle r="3.6" fill="#fff" stroke="#16a34a" strokeWidth="2">
                      <animateMotion dur="1.8s" repeatCount="indefinite" calcMode="linear"
                        begin={`${i * 0.22}s`} path={d} />
                    </circle>
                  </g>
                );
              })}
            </g>
          )}

          {/* Cities (always on top) */}
          {CH_CITIES.map((c, i) => {
            if (fast && c.name === '香港') {
              return (
                <g key={`c-${i}`}>
                  <g transform={`translate(${c.x - 26}, ${c.y - 20})`}>
                    <rect x="0" y="0" width="52" height="40" rx="9" fill="#16a34a" stroke="#fff" strokeWidth="2.4" />
                    <line x1="11" y1="13" x2="41" y2="13" stroke="#fff" strokeWidth="2" />
                    <line x1="11" y1="21" x2="41" y2="21" stroke="#fff" strokeWidth="2" />
                    <line x1="11" y1="29" x2="33" y2="29" stroke="#fff" strokeWidth="2" />
                  </g>
                  <text x={c.x} y={c.y + 38} textAnchor="middle"
                    fontFamily="'Noto Sans HK','Inter',sans-serif" fontSize="13" fontWeight="800" fill="#15803d">香港節點</text>
                </g>
              );
            }
            return (
              <g key={`c-${i}`}>
                <circle cx={c.x} cy={c.y} r="5" fill="#0F2A47" />
                <circle cx={c.x} cy={c.y} r="9" fill="none" stroke={accent} strokeWidth="1.6" opacity="0.6" />
                <text x={c.x + 12} y={c.y - 3}
                  fontFamily="'Noto Sans HK','Inter',sans-serif" fontSize="13" fontWeight="700" fill="#0F2A47">{c.name}</text>
                <text x={c.x + 12} y={c.y + 12} fontFamily="'Chakra Petch',ui-monospace,monospace"
                  fontSize="11" fontWeight="700" fill={accent}>{fast ? c.fast : c.slow}</text>
              </g>
            );
          })}
        </svg>

        <div className="CH-map-badge" style={{ color: accent }}>
          {fast
            ? <><span className="ms">香港節點直送內地</span> · 平均 &lt;50ms · 快</>
            : <><span className="ms">公網繞道海外</span> · 280ms+ · 慢</>}
        </div>
      </div>

      <div className="CH-map-legend">
        <div><span className="sw" style={{ borderColor: '#dc2626' }} />紅 = 慢（繞道海外源站）</div>
        <div><span className="sw" style={{ borderColor: '#16a34a' }} />綠 = 快（香港節點 CN2 GIA 直送內地）</div>
      </div>
    </div>
  );
}

function ChinaWhat() {
  return (
    <>
      <style>{CH_WHAT_CSS}</style>
      <section className="CH-what" data-screen-label="02 What is China CDN">
        <div className="container">
          <div className="CH-what-head">
            <h2 className="CH-h2">甚麼是 <span className="accent">中國 CDN？</span></h2>
            <p className="CH-lede">
              中國 CDN 是一種針對內地用戶而設的內容傳遞網絡服務。它將你的網站、圖片、影片、API 回應
              <strong>預先快取於中國周邊的邊緣節點</strong>，內地網民連線時只需從最就近的節點取得內容，
              毋須繞道海外伺服器，因而大幅縮短載入時間並避開跨境網絡擠塞。
            </p>
            <p className="CH-lede hl">
              UD 的中國 CDN 採用全中國最優質的 <strong>CN2 GIA 專線</strong>, 中國電信下一代承載網中最高級別的線路，
              獲中國電信、中國聯通及中國移動三大營運商支援，具備高優先級、高穩定性及低延遲三大特性，
              能為跨境企業帶來接近本地服務器的體驗。
            </p>
          </div>
          <div className="CH-maps">
            <ChinaSpeedMap force="slow" />
            <ChinaSpeedMap force="fast" />
          </div>
        </div>
      </section>
    </>
  );
}

// ----- Why UD China CDN, 4 cards (no pills, no eyebrow) -----

const GlyphRoute = () => (
  <svg viewBox="0 0 40 40" fill="none" aria-hidden="true">
    <circle cx="6" cy="20" r="3.2" fill="currentColor" />
    <circle cx="34" cy="20" r="3.2" fill="currentColor" />
    <path d="M 9 20 L 31 20" stroke="currentColor" strokeWidth="2" strokeLinecap="round" />
    <path d="M 14 14 L 16 20 L 14 26" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" opacity=".7" />
    <path d="M 22 14 L 24 20 L 22 26" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" opacity=".7" />
  </svg>
);
const GlyphShield = () => (
  <svg viewBox="0 0 40 40" fill="none" aria-hidden="true">
    <path d="M 20 5 L 33 10 L 33 20 Q 33 30 20 35 Q 7 30 7 20 L 7 10 Z"
      stroke="currentColor" strokeWidth="2" fill="none" strokeLinejoin="round" />
    <path d="M 14 20 L 18 24 L 26 16" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" fill="none" />
  </svg>
);
const GlyphNoICP = () => (
  <svg viewBox="0 0 40 40" fill="none" aria-hidden="true">
    <rect x="6" y="9" width="28" height="22" rx="3" stroke="currentColor" strokeWidth="2" fill="none" />
    <line x1="11" y1="16" x2="22" y2="16" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" />
    <line x1="11" y1="22" x2="18" y2="22" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" opacity=".55" />
    <line x1="8" y1="32" x2="32" y2="8" stroke="#dc2626" strokeWidth="2.4" strokeLinecap="round" />
  </svg>
);
const GlyphChat = () => (
  <svg viewBox="0 0 40 40" fill="none" aria-hidden="true">
    <path d="M 6 10 L 6 26 Q 6 30 10 30 L 22 30 L 28 35 L 28 30 L 30 30 Q 34 30 34 26 L 34 10 Q 34 6 30 6 L 10 6 Q 6 6 6 10 Z"
      stroke="currentColor" strokeWidth="2" fill="none" strokeLinejoin="round" />
    <circle cx="14" cy="18" r="1.8" fill="currentColor" />
    <circle cx="20" cy="18" r="1.8" fill="currentColor" />
    <circle cx="26" cy="18" r="1.8" fill="currentColor" />
  </svg>
);

const WHY_CARDS = [
  {
    flag: true,
    icon: <GlyphNoICP />,
    title: '直連內地，毋須 ICP 備案',
    body: 'UD 透過香港及內地周邊的邊緣節點與 CN2 GIA 專線，把內容送到內地用戶眼前。毋須註冊內地公司或申請 ICP 備案，網民即可近乎本地速度瀏覽，省下動輒數月的審批。',
  },
  {
    icon: <GlyphRoute />,
    title: '頂級線路，延時低於 50ms',
    body: 'CN2 GIA 繞開擠塞的公共骨幹，繁忙時段仍維持穩定低延遲，承諾 99.99% 正常運行。配合 UD 自家管理的邊緣節點，把跨境網絡的不可預測性降到最低。',
  },
  {
    icon: <GlyphShield />,
    title: '內建安全防護，毋須額外加購',
    body: '流量經 UD 邊緣節點轉折，自動隱藏源伺服器 IP，內建免費 Let\'s Encrypt SSL、WAF 與 DDoS 防護，從源頭減少網絡攻擊風險。',
  },
  {
    icon: <GlyphChat />,
    title: '真人 24×7 技術支援',
    body: '電郵、電話、即時通訊全天候待命，香港團隊以中、英文為你解決問題，毋須跨時區或輪候自動回覆。',
  },
];

function ChinaWhy() {
  return (
    <section className="CH-why" data-screen-label="03 Why UD">
      <div className="container">
        <div className="CH-why-head">
          <h2 className="CH-h2">為何企業選用 UD <span className="accent">中國 CDN</span></h2>
          <p className="CH-lede">把進軍內地最棘手的網速、合規與保安，整合成一套即裝即用的方案。</p>
        </div>
        <div className="CH-why-grid">
          {WHY_CARDS.map((c, i) => (
            <div key={i} className={`CH-why-card ${c.flag ? 'flag' : ''}`}>
              <div className="CH-why-top">
                <div className="CH-why-icon">{c.icon}</div>
              </div>
              <h3 className="CH-why-h3">{c.title}</h3>
              <p className="CH-why-p">{c.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

window.ChinaWhat = ChinaWhat;
window.ChinaWhy = ChinaWhy;
