/* global React, MYSOKU_DATA */
// <Mysoku /> — em-scaled property listing card.
// Supports 4 visual variants via `data.variant`:
//   'std'      neutral (default) — Fコート / ○×ハイツ / ○×レジデンス
//   'banner'   オレンジ帯ヘッダー + 朱印スタンプ — X-OVER21 シリーズ
//   'spec'     データテーブル偏重 + AD札 — メゾン / F-coop
//   'tower'    チャコール強調 + FOR RENT 英字 — F-AIR シリーズ
//   'campaign' Recommended Points サイドバー + サテン地色 — F-residence / F-grace / F-villa / F-terrace
// Per-variant CSS lives in styles.css under `.msk.v-*`.

// ---------- Floorplan SVGs (3 layouts) ----------
function Floorplan({ type }) {
  if (type === 'b') {
    return (
      <svg viewBox="0 0 140 78" preserveAspectRatio="none">
        <rect x="2" y="2" width="136" height="74" fill="none" stroke="#1a1816" strokeWidth="0.8"/>
        <rect x="2" y="2" width="48" height="12" fill="none" stroke="#1a1816" strokeWidth="0.5" strokeDasharray="1.5 1"/>
        <rect x="88" y="2" width="50" height="12" fill="none" stroke="#1a1816" strokeWidth="0.5" strokeDasharray="1.5 1"/>
        <rect x="14" y="18" width="36" height="32" fill="#f7f4ec" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="32" y="36" fontSize="4.5" fill="#1a1816" textAnchor="middle">Bedroom</text>
        <rect x="52" y="18" width="50" height="32" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="77" y="36" fontSize="4.5" fill="#1a1816" textAnchor="middle">DK</text>
        <rect x="14" y="52" width="20" height="14" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="24" y="61" fontSize="3.5" fill="#8a847c" textAnchor="middle">Storage</text>
        <rect x="104" y="18" width="22" height="16" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="115" y="28" fontSize="3.8" fill="#1a1816" textAnchor="middle">Bath</text>
        <rect x="104" y="36" width="22" height="14" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="115" y="44" fontSize="3.8" fill="#1a1816" textAnchor="middle">Lav.</text>
        <rect x="60" y="56" width="32" height="14" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="76" y="64" fontSize="3.5" fill="#1a1816" textAnchor="middle">Entry</text>
        <circle cx="128" cy="68" r="5" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <path d="M128 64 L130 70 L128 68 L126 70 Z" fill="#1a1816"/>
      </svg>
    );
  }
  if (type === 'c') {
    return (
      <svg viewBox="0 0 140 78" preserveAspectRatio="none">
        <rect x="2" y="2" width="136" height="74" fill="none" stroke="#1a1816" strokeWidth="0.8"/>
        <rect x="2" y="2" width="80" height="12" fill="none" stroke="#1a1816" strokeWidth="0.5" strokeDasharray="1.5 1"/>
        <rect x="124" y="2" width="14" height="60" fill="none" stroke="#1a1816" strokeWidth="0.5" strokeDasharray="1.5 1"/>
        <rect x="14" y="18" width="58" height="36" fill="#f7f4ec" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="43" y="38" fontSize="4.5" fill="#1a1816" textAnchor="middle">Bedroom</text>
        <rect x="74" y="18" width="14" height="18" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="81" y="29" fontSize="3" fill="#8a847c" textAnchor="middle">Clo.</text>
        <rect x="90" y="18" width="30" height="20" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="105" y="30" fontSize="3.8" fill="#1a1816" textAnchor="middle">Kitchen</text>
        <rect x="90" y="40" width="14" height="16" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="97" y="50" fontSize="3" fill="#1a1816" textAnchor="middle">Bath</text>
        <rect x="106" y="40" width="14" height="16" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="113" y="50" fontSize="3" fill="#1a1816" textAnchor="middle">Lav.</text>
        <rect x="74" y="58" width="46" height="14" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <text x="97" y="67" fontSize="3.5" fill="#1a1816" textAnchor="middle">Entry</text>
        <circle cx="128" cy="68" r="5" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
        <path d="M128 64 L130 70 L128 68 L126 70 Z" fill="#1a1816"/>
      </svg>
    );
  }
  return (
    <svg viewBox="0 0 140 78" preserveAspectRatio="none">
      <rect x="2" y="2" width="136" height="74" fill="none" stroke="#1a1816" strokeWidth="0.8"/>
      <rect x="2" y="2" width="60" height="14" fill="none" stroke="#1a1816" strokeWidth="0.5" strokeDasharray="1.5 1"/>
      <rect x="14" y="20" width="44" height="36" fill="#f7f4ec" stroke="#1a1816" strokeWidth="0.4"/>
      <text x="36" y="40" fontSize="4.5" fill="#1a1816" textAnchor="middle">Bedroom</text>
      <rect x="62" y="20" width="22" height="22" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
      <text x="73" y="33" fontSize="3.8" fill="#1a1816" textAnchor="middle">Kitchen</text>
      <rect x="88" y="20" width="22" height="14" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
      <text x="99" y="29" fontSize="3.8" fill="#1a1816" textAnchor="middle">Bath</text>
      <rect x="88" y="36" width="22" height="12" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
      <text x="99" y="44" fontSize="3.8" fill="#1a1816" textAnchor="middle">Lav.</text>
      <rect x="62" y="44" width="22" height="14" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
      <text x="73" y="53" fontSize="3.5" fill="#1a1816" textAnchor="middle">Entry</text>
      <rect x="14" y="58" width="96" height="14" fill="none" stroke="#1a1816" strokeWidth="0.5" strokeDasharray="1.5 1"/>
      <text x="62" y="68" fontSize="3.5" fill="#8a847c" textAnchor="middle">Roof Balcony</text>
      <circle cx="128" cy="68" r="5" fill="none" stroke="#1a1816" strokeWidth="0.4"/>
      <path d="M128 64 L130 70 L128 68 L126 70 Z" fill="#1a1816"/>
    </svg>
  );
}

function Photo({ kind }) {
  return <div className={`msk-photo msk-photo-${kind}`}></div>;
}

// ---------- Variant-specific header/sub elements ----------
function VariantHeader({ data }) {
  const v = data.variant || 'std';
  if (v === 'banner') {
    return (
      <div className="msk-banner-strip">
        <span className="msk-banner-kind">賃貸マンション</span>
        <span className="msk-banner-brand">{data.brand}</span>
      </div>
    );
  }
  if (v === 'tower') {
    return (
      <div className="msk-tower-strip">
        <span className="msk-tower-label">{data.brand}</span>
        <span className="msk-tower-sub">FOR RENT</span>
      </div>
    );
  }
  if (v === 'spec') {
    return (
      <div className="msk-spec-strip">
        <span className="msk-spec-brand">{data.brand}</span>
        {data.ad && <span className="msk-spec-ad">{data.ad}</span>}
      </div>
    );
  }
  if (v === 'campaign') {
    return (
      <div className="msk-camp-strip">
        <span className="msk-camp-brand">{data.brand}</span>
        <span className="msk-camp-sub">GROUP</span>
      </div>
    );
  }
  return null;
}

function Stamp({ text }) {
  if (!text) return null;
  // multi-line via \n
  const lines = text.split('\n');
  return (
    <div className="msk-stamp">
      {lines.map((l, i) => <span key={i}>{l}</span>)}
    </div>
  );
}

function CampaignBox({ items }) {
  if (!items || !items.length) return null;
  return (
    <div className="msk-campaign-box">
      <div className="msk-campaign-head">〜Campaign〜</div>
      <ul>
        {items.map((c, i) => <li key={i}>■ {c}</li>)}
      </ul>
    </div>
  );
}

// ---------- Main Mysoku ----------
function Mysoku({ data, withLabel = true }) {
  const d = data;
  const v = d.variant || 'std';
  return (
    <div className={`msk v-${v}`}>
      {withLabel && <span className="msk-label">マイソク.pdf</span>}

      <VariantHeader data={d} />

      <div className="msk-top">
        <div className="msk-title-block">
          <div className="msk-title">{d.title}</div>
          <div className="msk-roomnum">{d.roomNum}</div>
          <div className="msk-catch">{d.catch}</div>
        </div>
        <table className="msk-rent">
          <tbody>
            <tr><th>号室</th><th>賃料</th></tr>
            <tr>
              <td>{d.roomNum}</td>
              <td className="rent-val">{d.rent}</td>
            </tr>
            <tr>
              <td colSpan="2" className="mgmt">管理費 {d.mgmt}</td>
            </tr>
            <tr>
              <td>敷 {d.deposit}</td>
              <td>礼 {d.keymoney}</td>
            </tr>
          </tbody>
        </table>
        <Stamp text={d.stamp} />
      </div>

      <div className="msk-mid">
        <div className="msk-photos">
          <Photo kind={d.photos[0]} />
          <Photo kind={d.photos[1]} />
        </div>
        <div className="msk-floor">
          <Floorplan type={d.floorplan} />
        </div>
        {v === 'campaign' && <CampaignBox items={d.campaign} />}
      </div>

      <div className="msk-info">
        <div><span className="lbl">所在地</span><span className="val">{d.address}</span></div>
        <div><span className="lbl">交通</span>
          <span className="val">
            {d.transit.map((t, i) => <span key={i} className="line">{t}</span>)}
          </span>
        </div>
        <div><span className="lbl">構造</span><span className="val">{d.structure}</span></div>
        <div><span className="lbl">専有面積</span><span className="val">{d.area}</span></div>
        <div><span className="lbl">間取り</span><span className="val">{d.layout}</span></div>
        <div><span className="lbl">方角</span><span className="val">{d.direction}</span></div>
        <div><span className="lbl">築年月</span><span className="val">{d.built}</span></div>
        <div><span className="lbl">契約期間</span><span className="val">{d.contract}</span></div>
      </div>

      <div className="msk-equip">
        {d.equipPrimary.map((e, i) => <span key={i} className="filled">◆{e}</span>)}
        {d.equipSecondary.map((e, i) => <span key={i} className="hollow">◇{e}</span>)}
      </div>

      <div className="msk-footer">
        <span>図面が現状と異なる場合は現状を優先とします。</span>
        <span>TEL {d.tel}</span>
      </div>
    </div>
  );
}

Object.assign(window, { Mysoku });
