import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io' show Platform;

class MapsBottomModal extends StatelessWidget {
  final String address;

  const MapsBottomModal({
    super.key,
    required this.address,
  });

  static Future<void> show(BuildContext context, String address) async {
    final theme = Theme.of(context);

    if (Platform.isAndroid) {
      final url = Uri.parse('geo:0,0?q=$address');
      if (await canLaunchUrl(url)) {
        await launchUrl(url, mode: LaunchMode.externalApplication);
      } else {
        if (context.mounted) {
          showDialog<void>(
            context: context,
            builder: (context) => AlertDialog(
              title: Text(
                context.strings.open_maps_error_title,
              ),
              content: Text(context.strings.open_maps_error_subtitle),
              actions: [
                TextButton(
                  onPressed: () => Navigator.pop(context),
                  child: Text('OK', style: theme.textTheme.bodyMedium),
                ),
              ],
            ),
          );
        }
      }
      return;
    }

    // For iOS, show the modal with options
    if (context.mounted) {
      await showModalBottomSheet<void>(
        context: context,
        builder: (BuildContext context) {
          return MapsBottomModal(address: address);
        },
      );
    }
  }

  Future<void> _launchMapsUrl(BuildContext context, Uri url) async {
    if (await canLaunchUrl(url)) {
      await launchUrl(url, mode: LaunchMode.externalApplication);
    } else {
      if (context.mounted) {
        showDialog<void>(
          context: context,
          builder: (context) => AlertDialog(
            title: Text(context.strings.open_maps_error_title),
            content: Text(context.strings.open_maps_error_subtitle),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: Text('OK', style: Theme.of(context).textTheme.bodyMedium),
              ),
            ],
          ),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Container(
      height: 150,
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ListTile(
            leading: const Icon(Icons.map_outlined),
            title: Text(context.strings.apple_maps, style: theme.textTheme.bodyMedium),
            onTap: () async {
              Navigator.pop(context);
              await _launchMapsUrl(
                context,
                Uri.parse('maps://?q=$address'),
              );
            },
          ),
          ListTile(
            leading: const Icon(Icons.map),
            title: Text(context.strings.google_maps, style: theme.textTheme.bodyMedium),
            onTap: () async {
              Navigator.pop(context);
              await _launchMapsUrl(
                context,
                Uri.parse('https://www.google.com/maps/search/?api=1&query=$address'),
              );
            },
          ),
        ],
      ),
    );
  }
}