// src/views/mapa_expandible.dart
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'rutas.dart';

class MapaExpandible extends StatefulWidget {
  final double latitud;
  final double longitud;
  final String nombreDomicilio;

  const MapaExpandible({
    super.key,
    required this.latitud,
    required this.longitud,
    required this.nombreDomicilio,
  });

  @override
  State<MapaExpandible> createState() => _MapaExpandibleState();
}

class _MapaExpandibleState extends State<MapaExpandible> {
  bool _isExpanded = false;

  bool get _isValidLatLng {
    final lat = widget.latitud;
    final lng = widget.longitud;
    return lat.isFinite && lng.isFinite && lat != 0 && lng != 0;
  }

  Future<void> _abrirGoogleMaps() async {
    final url = 'https://www.google.com/maps?q=${widget.latitud},${widget.longitud}&z=15';
    try {
      if (await canLaunch(url)) {
        await launch(url);
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('No se pudo abrir el mapa'), backgroundColor: Colors.red),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    if (!_isValidLatLng) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
        decoration: BoxDecoration(
          color: Colors.grey[200],
          borderRadius: BorderRadius.circular(20),
        ),
        child: const Text('UbicaciÃ³n no disponible', style: TextStyle(fontSize: 12)),
      );
    }

    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        GestureDetector(
          onTap: () {
            setState(() {
              _isExpanded = !_isExpanded;
            });
          },
          child: Container(
            padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
            decoration: BoxDecoration(
              color: Colors.grey[200],
              borderRadius: BorderRadius.circular(20),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(
                  _isExpanded ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                  color: colorAzul,
                  size: 20,
                ),
                const SizedBox(width: 4),
                Text(
                  _isExpanded ? 'Ocultar mapa' : 'Ver mapa',
                  style: TextStyle(color: colorAzul, fontSize: 12, fontWeight: FontWeight.w500),
                ),
              ],
            ),
          ),
        ),
        if (_isExpanded)
          Padding(
            padding: const EdgeInsets.only(top: 12),
            child: GestureDetector(
              onTap: _abrirGoogleMaps,
              child: Container(
                width: 200, // Ancho fijo en lugar de infinity
                height: 160,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(15),
                  border: Border.all(color: colorAzul.withOpacity(0.3), width: 1),
                  color: Colors.grey[100],
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(Icons.map, size: 40, color: colorAzul),
                    const SizedBox(height: 8),
                    Text(
                      'Ver en Google Maps',
                      style: TextStyle(color: colorAzul, fontWeight: FontWeight.bold, fontSize: 14),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      '${widget.latitud.toStringAsFixed(4)}, ${widget.longitud.toStringAsFixed(4)}',
                      style: TextStyle(fontSize: 11, color: Colors.grey[600]),
                    ),
                    const SizedBox(height: 8),
                    Container(
                      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 3),
                      decoration: BoxDecoration(
                        color: colorAzul,
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: const Text(
                        'Tocar para abrir',
                        style: TextStyle(color: Colors.white, fontSize: 11),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
      ],
    );
  }
}