import React, { useState, useEffect } from 'react'; import { Box, Button, FormControl, FormLabel, FormHelperText, Input, VStack, HStack, Text, Alert, AlertIcon, AlertTitle, AlertDescription, Badge, Divider, Link, useColorModeValue, } from '@chakra-ui/react'; import MapStyleSelector from './MapStyleSelector'; import { FiMapPin, FiCheck, FiX, FiExternalLink } from 'react-icons/fi'; import { parseMapUrl, MapCoordinates, validateCoordinates, reverseGeocode } from '../../utils/mapUrlParser'; import ContactMap from '../home/ContactMap'; interface MapLinkImporterProps { onImport: (coordinates: MapCoordinates) => void; currentLatitude?: number; currentLongitude?: number; currentZoom?: number; mapStyle?: string; onMapStyleChange?: (style: string) => void; clubPrimaryColor?: string; clubSecondaryColor?: string; clubName?: string; } const MapLinkImporter: React.FC = ({ onImport, currentLatitude, currentLongitude, currentZoom, mapStyle, onMapStyleChange, clubPrimaryColor, clubSecondaryColor, clubName, }) => { const [urlInput, setUrlInput] = useState(''); const [parsedData, setParsedData] = useState(null); const [error, setError] = useState(null); const [previewCoords, setPreviewCoords] = useState(null); const bgColor = useColorModeValue('white', 'gray.800'); const borderColor = useColorModeValue('gray.200', 'gray.700'); useEffect(() => { // Initialize preview with current coordinates if available if (currentLatitude && currentLongitude) { setPreviewCoords({ latitude: currentLatitude, longitude: currentLongitude, zoom: currentZoom, source: 'unknown', }); } }, [currentLatitude, currentLongitude, currentZoom]); const handleUrlChange = async (e: React.ChangeEvent) => { const value = e.target.value; setUrlInput(value); setError(null); setParsedData(null); if (!value.trim()) { return; } // Try to parse the URL const result = parseMapUrl(value); if (result) { if (validateCoordinates(result.latitude, result.longitude)) { // Perform reverse geocoding to get detailed address try { const addressDetails = await reverseGeocode(result.latitude, result.longitude); const enrichedResult = { ...result, ...addressDetails }; setParsedData(enrichedResult); setPreviewCoords(enrichedResult); setError(null); } catch (err) { // If geocoding fails, still use the basic data setParsedData(result); setPreviewCoords(result); setError(null); } } else { setError('Souřadnice jsou mimo platný rozsah'); setParsedData(null); } } else { setError('Nepodařilo se rozpoznat URL mapy. Podporované: mapy.cz, Google Maps'); setParsedData(null); } }; const handleImport = () => { if (parsedData) { onImport(parsedData); setUrlInput(''); setParsedData(null); setError(null); } }; const handleClear = () => { setUrlInput(''); setParsedData(null); setError(null); // Reset preview to current coordinates if (currentLatitude && currentLongitude) { setPreviewCoords({ latitude: currentLatitude, longitude: currentLongitude, zoom: currentZoom, source: 'unknown', }); } else { setPreviewCoords(null); } }; return ( Importovat z URL mapy Rychlé odkazy: Mapy.cz Google Maps {parsedData && ( Úspěšně rozpoznáno! {parsedData.source === 'mapy.cz' ? 'Mapy.cz' : 'Google Maps'} Šířka: {parsedData.latitude.toFixed(7)} Délka: {parsedData.longitude.toFixed(7)} {parsedData.zoom && ( Zoom: {parsedData.zoom} )} {parsedData.street && ( Ulice: {parsedData.street} )} {parsedData.city && ( Město: {parsedData.city} )} {parsedData.zip && ( PSČ: {parsedData.zip} )} {parsedData.country && ( Země: {parsedData.country} )} {parsedData.address && ( Celá adresa: {parsedData.address} )} )} {error && ( {error} )} {/* Map Preview */} {previewCoords && ( <> Náhled mapy Souřadnice: {previewCoords.latitude.toFixed(6)}, {previewCoords.longitude.toFixed(6)} {previewCoords.zoom && ` | Zoom: ${previewCoords.zoom}`} {/* Map Style Selector */} {onMapStyleChange && ( <> Styl mapy Vyberte vzhled mapy, který se zobrazí na vašem webu. )} )} {/* Show map even without preview if coordinates exist */} {!previewCoords && (currentLatitude && currentLongitude) && ( <> Náhled mapy Souřadnice: {currentLatitude.toFixed(6)}, {currentLongitude.toFixed(6)} {currentZoom && ` | Zoom: ${currentZoom}`} )} ); }; export default MapLinkImporter;