import { Popup } from 'react-map-gl/maplibre';
import type { MilitaryBase } from '../../data/militaryBases';

const COUNTRY_STYLE: Record<string, { color: string; flag: string; label: string }> = {
  CN: { color: '#ef4444', flag: '🇨🇳', label: '중국' },
  JP: { color: '#f472b6', flag: '🇯🇵', label: '일본' },
  KP: { color: '#f97316', flag: '🇰🇵', label: '북한' },
  TW: { color: '#10b981', flag: '🇹🇼', label: '대만' },
};

const TYPE_STYLE: Record<string, { icon: string; label: string; color: string }> = {
  naval:    { icon: '⚓', label: '해군기지', color: '#3b82f6' },
  airforce: { icon: '✈️', label: '공군기지', color: '#f59e0b' },
  army:     { icon: '🪖', label: '육군기지', color: '#22c55e' },
  missile:  { icon: '🚀', label: '미사일기지', color: '#ef4444' },
  joint:    { icon: '⭐', label: '합동기지', color: '#a78bfa' },
};

interface Props {
  selected: MilitaryBase | null;
  onClose: () => void;
}

export function MilitaryBaseLayer({ selected, onClose }: Props) {
  if (!selected) return null;
  const cs = COUNTRY_STYLE[selected.country] || COUNTRY_STYLE.CN;
  const ts = TYPE_STYLE[selected.type] || TYPE_STYLE.army;
  return (
    <Popup longitude={selected.lng} latitude={selected.lat}
      onClose={onClose} closeOnClick={false}
      anchor="bottom" maxWidth="300px" className="gl-popup">
      <div className="popup-body-sm" style={{ minWidth: 220 }}>
        <div className="popup-header" style={{ background: ts.color, color: '#fff', gap: 6, padding: '6px 10px' }}>
          <span style={{ fontSize: 16 }}>{cs.flag}</span>
          <strong style={{ fontSize: 13 }}>{ts.icon} {selected.nameKo}</strong>
        </div>
        <div style={{ display: 'flex', gap: 4, marginBottom: 6 }}>
          <span style={{
            background: ts.color, color: '#fff',
            padding: '2px 8px', borderRadius: 3, fontSize: 10, fontWeight: 700,
          }}>
            {ts.label}
          </span>
          <span style={{
            background: cs.color, color: '#fff',
            padding: '2px 8px', borderRadius: 3, fontSize: 10, fontWeight: 700,
          }}>
            {cs.label}
          </span>
        </div>
        <div style={{ fontSize: 11, color: 'var(--kcg-text)', marginBottom: 6, lineHeight: 1.4 }}>
          {selected.description}
        </div>
        <div className="popup-grid" style={{ gap: '2px 12px' }}>
          <div><span className="popup-label">시설명 : </span><strong>{selected.name}</strong></div>
          <div><span className="popup-label">유형 : </span>{ts.label}</div>
        </div>
        <div style={{ marginTop: 6, fontSize: 10, color: '#999' }}>
          {selected.lat.toFixed(4)}°N, {selected.lng.toFixed(4)}°E
        </div>
      </div>
    </Popup>
  );
}
