/* Components for the villa landing page. */

const { useState, useEffect, useMemo, useRef } = React;

// ─────────────────────────────────────────────────────────────
// Map helpers — build links to external map services
// ─────────────────────────────────────────────────────────────
function mapyCzLink([lat, lon], zoom = 15) {
  return `https://mapy.cz/zakladni?source=coor&id=${lon}%2C${lat}&z=${zoom}`;
}
function googleMapsLink([lat, lon]) {
  return `https://www.google.com/maps/search/?api=1&query=${lat}%2C${lon}`;
}
function googleDirections(from, to, mode = "driving") {
  return `https://www.google.com/maps/dir/?api=1&origin=${from[0]}%2C${from[1]}&destination=${to[0]}%2C${to[1]}&travelmode=${mode}`;
}
function mapyCzRoute(from, to, mode = "car_fast") {
  return `https://mapy.cz/fnc/v1/route?mapset=outdoor&start=${from[1]},${from[0]}&end=${to[1]},${to[0]}&routeType=${mode}`;
}

// ─────────────────────────────────────────────────────────────
// Icons (monoline, 24x24)
// ─────────────────────────────────────────────────────────────
const icon = (path, vb = "0 0 24 24") => ({ size = 22, color = "currentColor", stroke = 1.5 }) => (
  <svg width={size} height={size} viewBox={vb} fill="none" stroke={color} strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round">
    {path}
  </svg>
);

const IconWifi = icon(<><path d="M5 12.55a11 11 0 0 1 14 0"/><path d="M1.42 9a16 16 0 0 1 21.16 0"/><path d="M8.53 16.11a6 6 0 0 1 6.95 0"/><circle cx="12" cy="20" r="1" fill="currentColor"/></>);
const IconKey = icon(<><circle cx="7.5" cy="15.5" r="4.5"/><path d="M21 2l-9.6 9.6"/><path d="M15.5 7.5l3 3L22 7l-3-3"/></>);
const IconCar = icon(<><path d="M5 17h14M5 17v2a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1v-2M19 17v2a1 1 0 0 0 1 1h1a1 1 0 0 0 1-1v-2"/><path d="M2 17l2-6a2 2 0 0 1 2-1.5h12a2 2 0 0 1 2 1.5l2 6"/><circle cx="6.5" cy="14" r="0.8" fill="currentColor"/><circle cx="17.5" cy="14" r="0.8" fill="currentColor"/></>);
const IconPhone = icon(<path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.8 19.8 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.72 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.35 1.85.59 2.81.72A2 2 0 0 1 22 16.92z"/>);
const IconMail = icon(<><rect x="2" y="5" width="20" height="14" rx="2"/><path d="M3 6l9 7 9-7"/></>);
const IconClock = icon(<><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></>);
const IconShield = icon(<path d="M12 2l9 4v6c0 5-3.5 9.5-9 10-5.5-.5-9-5-9-10V6l9-4z"/>);
const IconStorage = icon(<><rect x="3" y="8" width="18" height="12" rx="2"/><path d="M3 12h18"/><path d="M7 4h10l2 4H5l2-4z"/></>);
const IconSun = icon(<><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></>);
const IconMoon = icon(<path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"/>);
const IconGrid = icon(<><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></>);
const IconList = icon(<><line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="21" y2="18"/><circle cx="4" cy="6" r="1" fill="currentColor"/><circle cx="4" cy="12" r="1" fill="currentColor"/><circle cx="4" cy="18" r="1" fill="currentColor"/></>);
const IconTile = icon(<><rect x="3" y="3" width="18" height="10"/><rect x="3" y="16" width="18" height="5"/></>);
const IconClose = icon(<><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></>);
const IconArrow = icon(<><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></>);
const IconPinDrop = icon(<><path d="M12 22s8-7 8-13a8 8 0 1 0-16 0c0 6 8 13 8 13z"/><circle cx="12" cy="9" r="2.5"/></>);
const IconNavigate = icon(<><polygon points="3 11 22 2 13 21 11 13 3 11"/></>);
const IconCopy = icon(<><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></>);
const IconCheck = icon(<polyline points="20 6 9 17 4 12"/>);
const IconHome = icon(<><path d="M3 12l9-9 9 9"/><path d="M5 10v10h14V10"/></>);
const IconStar = icon(<polygon points="12 2 15 9 22 9 17 14 19 22 12 18 5 22 7 14 2 9 9 9"/>);

// Category icons — elegant, monoline
const IconTrip = icon(<><path d="M3 21h18"/><path d="M6 21V8l6-5 6 5v13"/><path d="M10 21v-6h4v6"/></>);
const IconWater = icon(<><path d="M12 2c0 6-6 9-6 14a6 6 0 0 0 12 0c0-5-6-8-6-14z"/></>);
const IconSport = icon(<><circle cx="12" cy="12" r="9"/><path d="M12 3v18M3 12h18M5.5 5.5l13 13M18.5 5.5l-13 13"/></>);
const IconWalk = icon(<><circle cx="13" cy="4" r="2"/><path d="M7 22l3-8 4 2 2 6M13 14l-3-4 4-3 4 3v5"/></>);
const IconWellness = icon(<><path d="M12 2C9 6 7 8 7 12a5 5 0 0 0 10 0c0-4-2-6-5-10z"/><path d="M12 22v-4"/></>);
const IconBike = icon(<><circle cx="5.5" cy="17.5" r="4"/><circle cx="18.5" cy="17.5" r="4"/><path d="M5.5 17.5L10 6h4l3 5h-7M14 6h3"/></>);
const IconGastro = icon(<><path d="M4 3v8a3 3 0 0 0 3 3v7"/><path d="M4 8h6"/><path d="M7 3v5"/><path d="M17 3c-2 0-3 2-3 5s1 5 3 5v8"/></>);

const CATEGORY_META = {
  trips: { Icon: IconTrip },
  water: { Icon: IconWater },
  sport: { Icon: IconSport },
  walks: { Icon: IconWalk },
  wellness: { Icon: IconWellness },
  cycling: { Icon: IconBike },
  gastro: { Icon: IconGastro },
};

const DIFFICULTY_DOTS = { easy: 1, medium: 2, hard: 3 };

// ─────────────────────────────────────────────────────────────
// Flags (SVG, no external deps)
// ─────────────────────────────────────────────────────────────
function Flag({ code, size = 18 }) {
  const w = size * 1.4, h = size;
  const r = 2;
  const flags = {
    cs: (
      <g>
        <rect width={w} height={h/2} fill="#fff"/>
        <rect y={h/2} width={w} height={h/2} fill="#D7141A"/>
        <polygon points={`0,0 ${w*0.45},${h/2} 0,${h}`} fill="#11457E"/>
      </g>
    ),
    en: (
      <g>
        <rect width={w} height={h} fill="#012169"/>
        <path d={`M0,0 L${w},${h} M${w},0 L0,${h}`} stroke="#fff" strokeWidth="2"/>
        <path d={`M0,0 L${w},${h} M${w},0 L0,${h}`} stroke="#C8102E" strokeWidth="1"/>
        <rect x={w/2-1.5} width="3" height={h} fill="#fff"/>
        <rect y={h/2-1.5} width={w} height="3" fill="#fff"/>
        <rect x={w/2-0.8} width="1.6" height={h} fill="#C8102E"/>
        <rect y={h/2-0.8} width={w} height="1.6" fill="#C8102E"/>
      </g>
    ),
    de: (
      <g>
        <rect width={w} height={h/3} fill="#000"/>
        <rect y={h/3} width={w} height={h/3} fill="#DD0000"/>
        <rect y={2*h/3} width={w} height={h/3} fill="#FFCE00"/>
      </g>
    ),
    pl: (
      <g>
        <rect width={w} height={h/2} fill="#fff"/>
        <rect y={h/2} width={w} height={h/2} fill="#DC143C"/>
      </g>
    ),
  };
  return (
    <svg width={w} height={h} viewBox={`0 0 ${w} ${h}`} style={{ borderRadius: r, overflow: 'hidden', display: 'block' }}>
      <clipPath id={`fc-${code}`}><rect width={w} height={h} rx={r}/></clipPath>
      <g clipPath={`url(#fc-${code})`}>{flags[code]}</g>
      <rect width={w} height={h} rx={r} fill="none" stroke="rgba(0,0,0,0.1)" strokeWidth="0.5"/>
    </svg>
  );
}

// ─────────────────────────────────────────────────────────────
// Language switcher
// ─────────────────────────────────────────────────────────────
function LangSwitcher({ lang, setLang, dark }) {
  const codes = ["cs", "en", "de", "pl"];
  return (
    <div style={{
      display: 'flex', gap: 4, padding: 4, borderRadius: 999,
      background: dark ? 'rgba(255,255,255,0.08)' : 'rgba(30,58,46,0.06)',
      border: dark ? '1px solid rgba(255,255,255,0.1)' : '1px solid rgba(30,58,46,0.08)',
    }}>
      {codes.map(c => (
        <button
          key={c}
          onClick={() => setLang(c)}
          aria-label={`Language: ${c.toUpperCase()}`}
          style={{
            background: lang === c ? (dark ? 'rgba(255,255,255,0.15)' : '#fff') : 'transparent',
            border: 'none', padding: '6px 8px', borderRadius: 999, cursor: 'pointer',
            display: 'flex', alignItems: 'center', gap: 6,
            boxShadow: lang === c ? '0 1px 3px rgba(0,0,0,0.08)' : 'none',
            fontFamily: 'Inter, sans-serif', fontSize: 12, letterSpacing: '0.08em',
            textTransform: 'uppercase', fontWeight: 600,
            color: lang === c ? (dark ? '#F5EAD0' : '#1E3A2E') : (dark ? 'rgba(255,255,255,0.55)' : 'rgba(30,58,46,0.55)'),
            transition: 'all 0.2s',
          }}
        >
          <Flag code={c} size={13}/>
          {c.toUpperCase()}
        </button>
      ))}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Hero background — layered botanical SVG (no photos)
// ─────────────────────────────────────────────────────────────
function HeroBackground({ dark, variant }) {
  if (variant === 'minimal') {
    return (
      <div style={{
        position: 'absolute', inset: 0,
        background: dark
          ? 'linear-gradient(180deg, #0F241C 0%, #1E3A2E 100%)'
          : 'linear-gradient(180deg, #1E3A2E 0%, #2C4C3E 100%)',
      }} />
    );
  }
  if (variant === 'topo') {
    return (
      <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
        <div style={{ position: 'absolute', inset: 0, background: dark ? '#0F241C' : '#1E3A2E' }}/>
        <svg viewBox="0 0 400 300" style={{ position:'absolute', inset:0, width:'100%', height:'100%' }} preserveAspectRatio="xMidYMid slice">
          {Array.from({length: 14}).map((_, i) => (
            <path
              key={i}
              d={`M-20,${40 + i*18 + Math.sin(i)*15} Q 100,${30 + i*18 + Math.sin(i+1)*20} 200,${50 + i*18 + Math.sin(i+2)*15} T 420,${40 + i*18}`}
              fill="none"
              stroke="#C9A961"
              strokeWidth="0.6"
              opacity={0.08 + (i%3)*0.04}
            />
          ))}
        </svg>
      </div>
    );
  }
  // Default: botanical — real photo with elegant green/gold overlay
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
      {window.HERO_IMAGE && (
        <img
          src={window.HERO_IMAGE}
          alt=""
          style={{
            position: 'absolute', inset: 0, width: '100%', height: '100%',
            objectFit: 'cover', filter: 'saturate(0.88)',
          }}
          onError={(e) => { e.currentTarget.style.display = 'none'; }}
        />
      )}
      <div style={{
        position:'absolute', inset:0,
        background: dark
          ? 'linear-gradient(180deg, rgba(15,36,28,0.85) 0%, rgba(30,58,46,0.88) 55%, rgba(15,36,28,0.95) 100%)'
          : 'linear-gradient(180deg, rgba(30,58,46,0.78) 0%, rgba(42,74,59,0.8) 55%, rgba(30,58,46,0.9) 100%)',
      }}/>
      <div style={{
        position:'absolute', inset:0,
        background: 'radial-gradient(ellipse at 30% 40%, rgba(245,234,208,0.12), transparent 60%), radial-gradient(ellipse at 80% 70%, rgba(201,169,97,0.1), transparent 60%)',
      }}/>
      <svg viewBox="0 0 400 500" preserveAspectRatio="xMidYMax slice" style={{position:'absolute', inset:0, width:'100%', height:'100%'}}>
        {[[20,80],[350,60],[80,180],[320,220],[180,120],[60,320],[330,360],[200,420]].map(([x,y],i)=>(
          <g key={i} transform={`translate(${x},${y}) rotate(${i*30})`}>
            <path d="M0 0 Q 20 20 0 40 Q -20 20 0 0" fill="#C9A961" opacity={0.12}/>
          </g>
        ))}
        <g opacity={dark?0.3:0.35}>
          <path d="M-20,500 Q 20,420 60,440 Q 100,460 80,500 Z" fill="#0F241C"/>
          <path d="M320,500 Q 360,430 400,440 Q 440,470 420,500 Z" fill="#0F241C"/>
          <path d="M160,500 Q 200,440 240,460 Q 280,490 260,500 Z" fill="#0F241C"/>
        </g>
      </svg>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Generic info card
// ─────────────────────────────────────────────────────────────
function InfoCard({ icon: Icon, title, children, accent = false, dark }) {
  return (
    <div style={{
      background: accent ? '#1E3A2E' : (dark ? 'rgba(255,255,255,0.04)' : '#fff'),
      color: accent ? '#F5EAD0' : (dark ? '#F5EAD0' : '#1E3A2E'),
      padding: '22px 22px 24px',
      borderRadius: 10,
      border: accent ? 'none' : (dark ? '1px solid rgba(255,255,255,0.08)' : '1px solid rgba(30,58,46,0.08)'),
      boxShadow: accent ? '0 1px 2px rgba(0,0,0,0.04)' : '0 1px 2px rgba(0,0,0,0.02)',
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 10 }}>
        <div style={{ color: '#C9A961' }}>
          <Icon size={26} stroke={1.5}/>
        </div>
        <div style={{
          fontFamily: '"Cormorant Garamond", "Cormorant", serif',
          fontSize: 24, fontWeight: 600, letterSpacing: '-0.01em',
          color: accent ? '#F5EAD0' : (dark ? '#F5EAD0' : '#1E3A2E'),
        }}>{title}</div>
      </div>
      <div style={{
        fontFamily: 'Inter, sans-serif', fontSize: 14.5, lineHeight: 1.55,
        color: accent ? 'rgba(245,234,208,0.88)' : (dark ? 'rgba(245,234,208,0.75)' : 'rgba(30,58,46,0.78)'),
      }}>
        {children}
      </div>
    </div>
  );
}

// Small pill button — used everywhere for external links
function PillLink({ href, children, primary = false, dark }) {
  return (
    <a href={href} target="_blank" rel="noopener noreferrer" style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: '7px 12px', borderRadius: 999,
      background: primary ? '#C9A961' : (dark ? 'rgba(245,234,208,0.08)' : 'rgba(30,58,46,0.06)'),
      color: primary ? '#1E3A2E' : 'inherit',
      textDecoration: 'none', fontWeight: 600, fontSize: 12.5,
      fontFamily: 'Inter, sans-serif',
      border: primary ? 'none' : (dark ? '1px solid rgba(245,234,208,0.1)' : '1px solid rgba(30,58,46,0.08)'),
      letterSpacing: '0.01em',
      transition: 'all 0.15s',
    }}>{children}</a>
  );
}

Object.assign(window, {
  IconWifi, IconKey, IconCar, IconPhone, IconMail, IconClock, IconShield, IconStorage,
  IconSun, IconMoon, IconGrid, IconList, IconTile, IconClose, IconArrow, IconPinDrop,
  IconNavigate, IconCopy, IconCheck, IconHome, IconStar,
  IconTrip, IconWater, IconSport, IconWalk, IconWellness, IconBike, IconGastro,
  CATEGORY_META, DIFFICULTY_DOTS,
  Flag, LangSwitcher, HeroBackground, InfoCard, PillLink,
  mapyCzLink, googleMapsLink, googleDirections, mapyCzRoute,
});
