import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:app_settings/app_settings.dart';
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart' as ph;
import 'package:url_launcher/url_launcher.dart';

class SettingsNavigator {
  SettingsNavigator._();
  static Future<bool> openAppSettings() async {
    try {
      final ok = await Geolocator.openAppSettings();
      if (ok) return true;
    } catch (_) {}
    try {
      final ok = await ph.openAppSettings();
      if (ok) return true;
    } catch (_) {}
    try {
      await AppSettings.openAppSettings();
      return true;
    } catch (_) {}
    if (Platform.isIOS) {
      try {
        final uri = Uri.parse('app-settings:');
        if (await canLaunchUrl(uri)) {
          await launchUrl(uri);
          return true;
        }
      } catch (_) {}
    }
    return false;
  }

  static Future<bool> openLocationSettings() async {
    if (Platform.isIOS) return openAppSettings();
    try {
      final ok = await Geolocator.openLocationSettings();
      if (ok) return true;
    } catch (_) {}
    try {
      const i = AndroidIntent(
        action: 'android.settings.LOCATION_SOURCE_SETTINGS',
      );
      await i.launch();
      return true;
    } catch (_) {}
    return openAppSettings();
  }
}
