import 'package:url_launcher/url_launcher.dart';
import 'dart:io';
import 'package:siro_driver/controller/functions/country_logic.dart';

void showInBrowser(String url) async {
  if (await canLaunchUrl(Uri.parse(url))) {
    launchUrl(Uri.parse(url));
  } else {}
}

String cleanAndFormatPhoneNumber(String phoneNumber) {
  String formattedNumber = phoneNumber.replaceAll(RegExp(r'\s+'), '');

  if (formattedNumber.length > 6) {
    formattedNumber = CountryLogic.formatCurrentCountryPhone(formattedNumber);
    if (!formattedNumber.startsWith('+')) {
      formattedNumber = '+$formattedNumber';
    }
  }
  return formattedNumber;
}

Future<void> makePhoneCall(String phoneNumber) async {
  String formattedNumber = cleanAndFormatPhoneNumber(phoneNumber);

  // Create URI directly from String to avoid double encoding '+' as '%2B'
  final Uri launchUri = Uri.parse('tel:$formattedNumber');

  // 4. Execute with externalApplication mode
  try {
    if (!await launchUrl(launchUri, mode: LaunchMode.externalApplication)) {
      throw 'Could not launch $launchUri';
    }
  } catch (e) {
    if (await canLaunchUrl(launchUri)) {
      await launchUrl(launchUri);
    } else {
      print("Cannot launch url: $launchUri");
    }
  }
}

void launchCommunication(
    String method, String contactInfo, String message) async {
  String formattedContact = cleanAndFormatPhoneNumber(contactInfo);
  // WhatsApp prefers the phone number without the '+' prefix
  String whatsappContact = formattedContact.replaceAll('+', '');
  String url;

  if (Platform.isIOS) {
    switch (method) {
      case 'phone':
        url = 'tel:$formattedContact';
        break;
      case 'sms':
        url = 'sms:$formattedContact?body=${Uri.encodeComponent(message)}';
        break;
      case 'whatsapp':
        url =
            'https://api.whatsapp.com/send?phone=$whatsappContact&text=${Uri.encodeComponent(message)}';
        break;
      case 'email':
        url =
            'mailto:$formattedContact?subject=Subject&body=${Uri.encodeComponent(message)}';
        break;
      default:
        return;
    }
  } else if (Platform.isAndroid) {
    switch (method) {
      case 'phone':
        url = 'tel:$formattedContact';
        break;
      case 'sms':
        url = 'sms:$formattedContact?body=${Uri.encodeComponent(message)}';
        break;
      case 'whatsapp':
        final bool whatsappInstalled =
            await canLaunchUrl(Uri.parse('whatsapp://'));
        if (whatsappInstalled) {
          url =
              'whatsapp://send?phone=$whatsappContact&text=${Uri.encodeComponent(message)}';
        } else {
          url =
              'https://api.whatsapp.com/send?phone=$whatsappContact&text=${Uri.encodeComponent(message)}';
        }
        break;
      case 'email':
        url =
            'mailto:$formattedContact?subject=Subject&body=${Uri.encodeComponent(message)}';
        break;
      default:
        return;
    }
  } else {
    return;
  }

  if (await canLaunchUrl(Uri.parse(url))) {
    await launchUrl(Uri.parse(url));
  } else {}
}
