import { useState } from 'react';
import { Marker, Popup } from 'react-map-gl/maplibre';
import { GOV_BUILDINGS } from '../../data/govBuildings';
import type { GovBuilding } from '../../data/govBuildings';

const COUNTRY_STYLE: Record<string, { color: string; flag: string; label: string }> = {
  CN: { color: '#ef4444', flag: '🇨🇳', label: '중국' },
  JP: { color: '#f472b6', flag: '🇯🇵', label: '일본' },
};

const TYPE_STYLE: Record<string, { icon: string; label: string; color: string }> = {
  executive:    { icon: '🏛️', label: '행정부', color: '#f59e0b' },
  legislature:  { icon: '🏛️', label: '입법부', color: '#a78bfa' },
  military_hq:  { icon: '⭐', label: '군사본부', color: '#ef4444' },
  intelligence: { icon: '🔍', label: '정보기관', color: '#6366f1' },
  foreign:      { icon: '🌐', label: '외교부', color: '#3b82f6' },
  maritime:     { icon: '⚓', label: '해양기관', color: '#06b6d4' },
  defense:      { icon: '🛡️', label: '국방부', color: '#dc2626' },
};

export function GovBuildingLayer() {
  const [selected, setSelected] = useState<GovBuilding | null>(null);

  return (
    <>
      {GOV_BUILDINGS.map(g => {
        const ts = TYPE_STYLE[g.type] || TYPE_STYLE.executive;
        return (
          <Marker key={g.id} longitude={g.lng} latitude={g.lat} anchor="center"
            onClick={(e) => { e.originalEvent.stopPropagation(); setSelected(g); }}>
            <div
              className="flex flex-col items-center cursor-pointer"
              style={{ filter: `drop-shadow(0 0 3px ${ts.color}88)` }}
            >
              <div style={{
                width: 16, height: 16, borderRadius: '50%',
                background: 'rgba(0,0,0,0.7)', border: `1.5px solid ${ts.color}`,
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                fontSize: 9,
              }}>
                {ts.icon}
              </div>
              <div style={{
                fontSize: 5, color: ts.color, marginTop: 0,
                textShadow: '0 0 3px #000, 0 0 3px #000',
                whiteSpace: 'nowrap', fontWeight: 700,
              }}>
                {g.nameKo.length > 10 ? g.nameKo.slice(0, 10) + '..' : g.nameKo}
              </div>
            </div>
          </Marker>
        );
      })}

      {selected && (() => {
        const cs = COUNTRY_STYLE[selected.country] || COUNTRY_STYLE.CN;
        const ts = TYPE_STYLE[selected.type] || TYPE_STYLE.executive;
        return (
          <Popup longitude={selected.lng} latitude={selected.lat}
            onClose={() => setSelected(null)} closeOnClick={false}
            anchor="bottom" maxWidth="320px" className="gl-popup">
            <div className="popup-body-sm" style={{ minWidth: 240 }}>
              <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: '#ccc', lineHeight: 1.4, marginBottom: 6 }}>
                {selected.description}
              </div>
              <div style={{ fontSize: 11, color: 'var(--kcg-muted)', marginBottom: 4 }}>
                {selected.name}
              </div>
              <div style={{ fontSize: 10, color: '#999' }}>
                {selected.lat.toFixed(4)}°N, {selected.lng.toFixed(4)}°E
              </div>
            </div>
          </Popup>
        );
      })()}
    </>
  );
}
