import React, { memo } from 'react';
import { useUpbitOrderbook } from '../../hooks/useUpbitOrderbook';

interface Props {
  market: string;
  onPick?: (price: number, rowType: 'ask' | 'bid') => void;
  fillHeight?: boolean;
  hideHeader?: boolean;
  depth?: number;
  /** split 패널용 — 매도/매수 호가만 */
  section?: 'all' | 'asks' | 'bids';
}

function fmtPrice(p: number): string {
  if (!Number.isFinite(p)) return '-';
  return p >= 1000 ? p.toLocaleString('ko-KR', { maximumFractionDigits: 0 }) : p.toFixed(4);
}

function fmtSize(s: number): string {
  if (!Number.isFinite(s)) return '-';
  if (s >= 1000) return s.toLocaleString('ko-KR', { maximumFractionDigits: 2 });
  return s.toFixed(4);
}

const PaperCompactOrderbook: React.FC<Props> = memo(({
  market,
  onPick,
  fillHeight = false,
  hideHeader = false,
  depth = 8,
  section = 'all',
}) => {
  const { orderbook } = useUpbitOrderbook(market);
  const rowCount = fillHeight ? Math.max(depth, 12) : depth;
  const asks = orderbook.asks.slice(0, rowCount).reverse();
  const bids = orderbook.bids.slice(0, rowCount);
  const maxSize = Math.max(
    ...asks.map(a => a.size),
    ...bids.map(b => b.size),
    1,
  );
  const mid = bids[0]?.price ?? asks[asks.length - 1]?.price ?? 0;

  const showAsks = section === 'all' || section === 'asks';
  const showBids = section === 'all' || section === 'bids';
  const showMid = section === 'all' || section === 'asks';

  return (
    <div className={`ptd-ob${fillHeight ? ' ptd-ob--fill' : ''}${section !== 'all' ? ' ptd-ob--section' : ''}`}>
      {!hideHeader && section === 'all' && <div className="ptd-ob-head">호가 (Depth)</div>}
      <div className="ptd-ob-body">
        {showAsks && asks.map(a => (
          <button
            key={`a-${a.price}`}
            type="button"
            className="ptd-ob-row ptd-ob-row--ask"
            onClick={() => onPick?.(a.price, 'ask')}
          >
            <span className="ptd-ob-bar" style={{ width: `${(a.size / maxSize) * 100}%` }} />
            <span className="ptd-ob-price">{fmtPrice(a.price)}</span>
            <span className="ptd-ob-size">{fmtSize(a.size)}</span>
          </button>
        ))}
        {showMid && section !== 'all' && (
          <div className="ptd-ob-mid ptd-ob-mid--inline">{mid ? fmtPrice(mid) : '—'}</div>
        )}
        {section === 'all' && <div className="ptd-ob-mid">{mid ? fmtPrice(mid) : '—'}</div>}
        {showBids && bids.map(b => (
          <button
            key={`b-${b.price}`}
            type="button"
            className="ptd-ob-row ptd-ob-row--bid"
            onClick={() => onPick?.(b.price, 'bid')}
          >
            <span className="ptd-ob-bar" style={{ width: `${(b.size / maxSize) * 100}%` }} />
            <span className="ptd-ob-price">{fmtPrice(b.price)}</span>
            <span className="ptd-ob-size">{fmtSize(b.size)}</span>
          </button>
        ))}
      </div>
    </div>
  );
});

PaperCompactOrderbook.displayName = 'PaperCompactOrderbook';
export default PaperCompactOrderbook;
