import 'package:url_launcher/url_launcher.dart';
import 'dart:io';

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

Future<void> makePhoneCall(String phoneNumber) async {
  // 1. Clean the number
  String formattedNumber = phoneNumber.replaceAll(RegExp(r'\s+'), '');

  // 2. Format logic (Syria/International)
  if (formattedNumber.length > 6) {
    if (formattedNumber.startsWith('09')) {
      formattedNumber = '+963${formattedNumber.substring(1)}';
    } else if (!formattedNumber.startsWith('+')) {
      formattedNumber = '+$formattedNumber';
    }
  }

  // 3. Create URI
  final Uri launchUri = Uri(
    scheme: 'tel',
    path: formattedNumber,
  );

  // 4. Execute with externalApplication mode
  try {
    // Attempt to launch directly without checking canLaunchUrl first
    // (Sometimes canLaunchUrl returns false on some devices even if it works)
    if (!await launchUrl(launchUri, mode: LaunchMode.externalApplication)) {
      throw 'Could not launch $launchUri';
    }
  } catch (e) {
    // Fallback: Try checking canLaunchUrl if the direct launch fails
    if (await canLaunchUrl(launchUri)) {
      await launchUrl(launchUri);
    } else {
      print("Cannot launch url: $launchUri");
    }
  }
}

void launchCommunication(
    String method, String contactInfo, String message) async {
  String url;

  if (Platform.isIOS) {
    switch (method) {
      case 'phone':
        url = 'tel:$contactInfo';
        break;
      case 'sms':
        url = 'sms:$contactInfo?body=${Uri.encodeComponent(message)}';
        break;
      case 'whatsapp':
        url =
            'https://api.whatsapp.com/send?phone=$contactInfo&text=${Uri.encodeComponent(message)}';
        break;
      case 'email':
        url =
            'mailto:$contactInfo?subject=Subject&body=${Uri.encodeComponent(message)}';
        break;
      default:
        return;
    }
  } else if (Platform.isAndroid) {
    switch (method) {
      case 'phone':
        url = 'tel:$contactInfo';
        break;
      case 'sms':
        url = 'sms:$contactInfo?body=${Uri.encodeComponent(message)}';
        break;
      case 'whatsapp':
        // Check if WhatsApp is installed
        final bool whatsappInstalled =
            await canLaunchUrl(Uri.parse('whatsapp://'));
        if (whatsappInstalled) {
          url =
              'whatsapp://send?phone=$contactInfo&text=${Uri.encodeComponent(message)}';
        } else {
          // Provide an alternative action, such as opening the WhatsApp Web API
          url =
              'https://api.whatsapp.com/send?phone=$contactInfo&text=${Uri.encodeComponent(message)}';
        }
        break;
      case 'email':
        url =
            'mailto:$contactInfo?subject=Subject&body=${Uri.encodeComponent(message)}';
        break;
      default:
        return;
    }
  } else {
    return;
  }

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