// ============================================================
// SAMPLE — Config view (single layout: params + rack visual + inspector)
// ============================================================
const { useState: useStateSC, useEffect: useEffectSC, useRef: useRefSC } = React;

function SampleConfigView({ cfg, setCfg, apiKey, setApiKey, syncStatus }) {
  const [selGroupId, setSelGroupId] = useStateSC(cfg.groups[0]?.id || null);
  const [selSlot,    setSelSlot]    = useStateSC(null);
  const [selRackIdx, setSelRackIdx] = useStateSC(0);
  const [leftW,      setLeftW]      = useStateSC(500);
  const [rightW,     setRightW]     = useStateSC(540);
  const [dragging,   setDragging]   = useStateSC(null); // 'left' | 'right' | null
  const stageRef     = useRefSC(null);
  const stageWrapRef = useRefSC(null);
  const stFileRef    = useRefSC(null);
  const dragRef      = useRefSC(null); // { side, startX, startW }

  useEffectSC(() => attachMiddleMousePan(stageWrapRef.current), []);

  useEffectSC(() => {
    const onMove = (e) => {
      const d = dragRef.current;
      if (!d) return;
      const delta = e.clientX - d.startX;
      if (d.side === 'left')  setLeftW(Math.max(160, Math.min(700, d.startW + delta)));
      if (d.side === 'right') setRightW(Math.max(200, Math.min(700, d.startW - delta)));
    };
    const onUp = () => { dragRef.current = null; setDragging(null); };
    window.addEventListener('mousemove', onMove);
    window.addEventListener('mouseup', onUp);
    return () => { window.removeEventListener('mousemove', onMove); window.removeEventListener('mouseup', onUp); };
  }, []);

  const startDrag = (side, e) => {
    e.preventDefault();
    dragRef.current = { side, startX: e.clientX, startW: side === 'left' ? leftW : rightW };
    setDragging(side);
  };

  const activeGroup = cfg.groups.find(g => g.id === selGroupId) || cfg.groups[0];

  const anyOnMap = {};
  if (activeGroup) {
    activeGroup.racks.forEach(r => { anyOnMap[r.id] = r.slots.some(s => s.power === 'on'); });
  }

  const toggleSlot = (slotId) => {
    if (!activeGroup) return;
    setCfg(c => ({
      ...c,
      groups: c.groups.map(g => g.id !== activeGroup.id ? g : {
        ...g,
        racks: g.racks.map(r => ({
          ...r,
          slots: r.slots.map(s => s.id === slotId ? { ...s, power: s.power === 'on' ? 'off' : 'on' } : s),
        })),
      }),
    }));
  };

  const health    = activeGroup ? groupHealth(activeGroup) : null;
  const healthCol = health
    ? (health.level === 'ok' ? 'var(--ok)' : health.level === 'warn' ? 'var(--warn)' : 'var(--bad)')
    : 'var(--fg-2)';

  // ── Param helpers ──────────────────────────────────────────
  const setParams = (patch) => setCfg(c => ({ ...c, params: { ...c.params, ...patch } }));

  const regenFromParams = () => {
    const { N = 2, K, M } = cfg.params;
    setCfg(c => ({
      ...c,
      groups: c.groups.map(g => {
        const existing = g.racks.filter(r => !r.absent);
        // Grow to N racks
        let racks = existing.slice(0, N);
        while (racks.length < N) {
          const idx = racks.length;
          racks.push({
            id: sRid('sr'),
            label: `${g.tag}-${String.fromCharCode(65 + idx)}`,
            alias: '', media: [],
            slots: Array.from({ length: K }, () => ({
              id: sRid('ss'), kind: 'empty', name: '', power: 'off', ports: [], media: [],
            })),
          });
        }
        return {
          ...g,
          racks: racks.map(r => ({
            ...r,
            slots: Array.from({ length: K }, (_, i) => {
              const prev = r.slots[i];
              const kind = prev?.kind || 'empty';
              const hasPorts = kind === 'switch';
              return {
                id:    prev?.id    || sRid('ss'),
                kind,
                name:  prev?.name  || '',
                power: prev?.power || 'off',
                media: prev?.media || [],
                ports: hasPorts
                  ? Array.from({ length: 2 * M }, (_, pi) => {
                      const pp = prev?.ports?.[pi];
                      return pp ? normPort(pp) : { plug: 'empty', led: 'off' };
                    })
                  : [],
              };
            }),
          })),
        };
      }),
    }));
  };

  const addGroup = () => {
    const id  = sRid('sg');
    const tag = `SG-${String(cfg.groups.length + 1).padStart(2, '0')}`;
    const { N = 2, K, M } = cfg.params;
    const racks = Array.from({ length: N }, (_, i) => ({
      id: sRid('sr'), label: `${tag}-${String.fromCharCode(65 + i)}`, alias: '', media: [],
      slots: Array.from({ length: K }, () => ({
        id: sRid('ss'), kind: 'empty', name: '', power: 'off', ports: [], media: [],
      })),
    }));
    setCfg(c => ({ ...c, groups: [...c.groups, { id, tag, alias: '', racks }] }));
    setSelGroupId(id);
    setSelRackIdx(0);
  };

  const doExport = () => {
    const url = URL.createObjectURL(new Blob([JSON.stringify(cfg, null, 2)], { type: 'application/json' }));
    Object.assign(document.createElement('a'), { href: url, download: `sample-config-${Date.now()}.json` }).click();
    URL.revokeObjectURL(url);
  };

  const doImport = (e) => {
    const f = e.target.files?.[0]; if (!f) return;
    const rd = new FileReader();
    rd.onload = () => { try { setCfg(JSON.parse(rd.result)); } catch { alert('Invalid JSON'); } };
    rd.readAsText(f);
  };

  const doReset = () => {
    if (!confirm('샘플 설정을 초기값으로 되돌릴까요?')) return;
    const def = defaultSampleConfig();
    setCfg(def);
    setSelGroupId(def.groups[0]?.id || null);
    setSelRackIdx(0);
    setSelSlot(null);
  };

  const { M, K, portIconSize = 22, portMode = 'plug_led', showPortNums = false } = cfg.params;

  return (
    <div className="cfg-rack-visual view-enter"
         style={{ gridTemplateColumns: `${leftW}px 6px 1fr 6px ${rightW}px`,
                  gridTemplateRows: '0 1fr',
                  userSelect: dragging ? 'none' : '' }}>

      {/* Left: params + groups + health + file ops */}
      <aside className="rack-side-left">
        <div className="panel">
          <div className="panel-title"><span>Parameters</span><span className="spacer"/></div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
            <Field label="N · 랙/그룹" value={cfg.params.N ?? 2}>
              <input type="range" className="slider" min="1" max="12"
                     value={cfg.params.N ?? 2}
                     onChange={(e) => setParams({ N: +e.target.value })} />
            </Field>
            <Field label="M · 포트 컬럼 수" value={cfg.params.M}>
              <input type="range" className="slider" min="2" max="24"
                     value={cfg.params.M}
                     onChange={(e) => setParams({ M: +e.target.value })} />
              <div className="mono" style={{ fontSize: 9, color: 'var(--fg-3)', marginTop: 3 }}>
                {cfg.params.M}열 × 2행 = 총 {2 * cfg.params.M}포트
              </div>
            </Field>
            <Field label="K · 슬롯 수" value={cfg.params.K}>
              <input type="range" className="slider" min="2" max="14"
                     value={cfg.params.K}
                     onChange={(e) => setParams({ K: +e.target.value })} />
            </Field>
            <button className="btn" onClick={regenFromParams}>
              <Icon kind="reset" size={12} /> 파라미터 적용
            </button>
            <Field label="포트 표시 모드">
              <div className="segmented" style={{ gridTemplateColumns: 'repeat(2, 1fr)' }}>
                <button className={portMode === 'plug_led' ? 'active' : ''}
                        onClick={() => setParams({ portMode: 'plug_led' })}>Plug+LED</button>
                <button className={portMode === 'led_only' ? 'active' : ''}
                        onClick={() => setParams({ portMode: 'led_only' })}>LED만</button>
              </div>
            </Field>
            <Field label="포트 번호 표시">
              <div className="segmented" style={{ gridTemplateColumns: 'repeat(2, 1fr)' }}>
                <button className={showPortNums ? 'active' : ''}
                        onClick={() => setParams({ showPortNums: true })}>표시</button>
                <button className={!showPortNums ? 'active' : ''}
                        onClick={() => setParams({ showPortNums: false })}>숨김</button>
              </div>
            </Field>
            <Field label="포트 아이콘 크기" value={`${cfg.params.portIconSize ?? 22}px`}>
              <input type="range" className="slider" min="14" max="34" step="2"
                     value={cfg.params.portIconSize ?? 22}
                     onChange={(e) => setParams({ portIconSize: +e.target.value })} />
            </Field>
          </div>
        </div>

        <div className="panel">
          <div className="panel-title">
            <span>Groups</span><span className="spacer"/><span className="mono">{cfg.groups.length}</span>
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
            {cfg.groups.map(g => (
              <button key={g.id}
                      className={`btn ${activeGroup?.id === g.id ? 'primary' : ''}`}
                      style={{ justifyContent: 'flex-start' }}
                      onClick={() => { setSelGroupId(g.id); setSelSlot(null); setSelRackIdx(0); }}>
                <span className="mono">{g.tag}</span>
                {g.alias && g.alias.trim() && (
                  <span style={{ marginLeft: 6, fontSize: 10, color: 'var(--accent)' }}>{g.alias}</span>
                )}
                <span style={{ marginLeft: 'auto', fontSize: 9, color: 'var(--fg-3)' }}>{g.racks.length}R</span>
              </button>
            ))}
          </div>
          <button className="btn" style={{ marginTop: 10 }} onClick={addGroup}>
            <Icon kind="add" size={12} /> 그룹 추가
          </button>
        </div>

        {activeGroup && health && (
          <div className="panel">
            <div className="panel-title">
              <span>{activeGroup.alias || activeGroup.tag}</span>
              <span className="spacer"/>
              <span style={{ color: healthCol }}>{health.level.toUpperCase()}</span>
            </div>
            <div className="stat-row"><span className="k">Racks</span><span className="v">{activeGroup.racks.length}</span></div>
            <div className="stat-row"><span className="k">Devices</span><span className="v">{health.total}</span></div>
            <div className="stat-row ok"><span className="k">Powered</span><span className="v">{health.on}/{health.total}</span></div>
            <div className="stat-row"><span className="k">Total Ports</span><span className="v">{health.ports}</span></div>
            <div className="stat-row warn"><span className="k">Suspicious</span><span className="v">{health.susp}</span></div>
          </div>
        )}

        <div className="panel">
          <div className="panel-title"><span>File</span><span className="spacer"/></div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
            <div className="btn-row">
              <button className="btn primary" onClick={doExport}><Icon kind="export" size={12} /> Export</button>
              <button className="btn" onClick={() => stFileRef.current?.click()}>
                <Icon kind="import" size={12} /> Import
              </button>
              <input ref={stFileRef} type="file" accept="application/json"
                     style={{ display: 'none' }} onChange={doImport} />
            </div>
            <button className="btn danger" onClick={doReset}><Icon kind="reset" size={12} /> Reset</button>
          </div>
        </div>

        <div className="panel">
          <div className="panel-title"><span>Server Sync</span><span className="spacer"/></div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
            <div className="mono" style={{ fontSize: 9, color: 'var(--fg-3)' }}>
              API Key — 서버 저장/미디어 업로드에 필요
            </div>
            <input
              type="password"
              className="sample-input"
              placeholder="API Key 입력..."
              value={apiKey}
              onChange={(e) => { const v = e.target.value; setApiKey(v); saveApiKey(v); }}
              style={{ width: '100%', boxSizing: 'border-box' }}
            />
            <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
              <div style={{
                width: 7, height: 7, borderRadius: '50%', flexShrink: 0,
                background: syncStatus === 'ok' ? 'var(--ok)' : syncStatus === 'error' ? 'var(--bad)' : syncStatus === 'saving' ? 'var(--warn)' : 'var(--fg-3)',
              }} />
              <span className="mono" style={{ fontSize: 9, color: 'var(--fg-3)' }}>
                {syncStatus === 'ok'    ? '서버 동기화 완료'
                 : syncStatus === 'error'  ? '저장 실패 — Key 확인'
                 : syncStatus === 'saving' ? '저장 중...'
                 : syncStatus === 'loading' ? '서버 로딩 중...'
                 : 'API Key 없음 (로컬만 저장)'}
              </span>
            </div>
          </div>
        </div>
      </aside>

      <div className={`resize-handle-v ${dragging === 'left' ? 'dragging' : ''}`}
           onMouseDown={(e) => startDrag('left', e)} />

      {/* Center: rack stage using SampleRackColumnV2 */}
      <div className="rack-stage" ref={stageWrapRef}>
        <div className="bp-grid" style={{ opacity: 0.35 }} />
        <Corners />
        <div className="bp-vignette" />
        <div className="rack-stage-inner" ref={stageRef}>
          {activeGroup ? (
            <>
              <PowerBus racks={activeGroup.racks} stageRef={stageRef} anyOnMap={anyOnMap} />
              {activeGroup.racks.map(r => (
                <SampleRackColumnV2 key={r.id} rack={r} group={activeGroup} M={M} K={K}
                                    iconSize={portIconSize}
                                    setCfg={setCfg}
                                    apiKey={apiKey}
                                    readOnly={false}
                                    onSelectSlot={(rackId, slotId) => setSelSlot({ rackId, slotId })}
                                    selSlotId={selSlot && selSlot.rackId === r.id ? selSlot.slotId : null} />
              ))}
            </>
          ) : (
            <div style={{ alignSelf: 'center', color: 'var(--fg-3)', fontFamily: 'var(--font-mono)', fontSize: 11 }}>
              좌측에서 그룹을 선택하세요.
            </div>
          )}
        </div>
      </div>

      <div className={`resize-handle-v ${dragging === 'right' ? 'dragging' : ''}`}
           onMouseDown={(e) => startDrag('right', e)} />

      {/* Right: SlotInspector + RackEditor */}
      <aside className="rack-side-right">
        {activeGroup ? (
          <>
            <SlotInspector group={activeGroup} selSlot={selSlot} M={M}
                           setCfg={setCfg} onAutoClear={() => setSelSlot(null)} />
            <RackEditor group={activeGroup} cfg={cfg} setCfg={setCfg}
                        selRackIdx={selRackIdx} setSelRackIdx={setSelRackIdx}
                        hidePorts={true} />
          </>
        ) : (
          <div className="panel">
            <div className="panel-title"><span>Inspector</span><span className="spacer"/></div>
            <div className="mono" style={{ fontSize: 11, color: 'var(--fg-2)' }}>그룹을 먼저 선택하세요.</div>
          </div>
        )}
      </aside>
    </div>
  );
}

Object.assign(window, { SampleConfigView });
