import 'dart:async';

import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:permission_handler/permission_handler.dart';

import 'amap/const_config.dart';

typedef AMAPListener = void Function (Map<String, Object>);

void showPrivacy() {
  /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看，如果未包含或者没有弹窗展示，高德定位SDK将不会工作
  ///
  /// 高德SDK合规使用方案请参考官网地址：https://lbs.amap.com/news/sdkhgsy
  /// <b>必须保证在调用定位功能之前调用， 建议首次启动App时弹出《隐私政策》并取得用户同意</b>
  ///
  /// 高德SDK合规使用方案请参考官网地址：https://lbs.amap.com/news/sdkhgsy
  ///
  /// [hasContains] 隐私声明中是否包含高德隐私政策说明
  ///
  /// [hasShow] 隐私权政策是否弹窗展示告知用户
  AMapFlutterLocation.updatePrivacyShow(true, true);

  /// 设置是否已经取得用户同意，如果未取得用户同意，高德定位SDK将不会工作
  ///
  /// 高德SDK合规使用方案请参考官网地址：https://lbs.amap.com/news/sdkhgsy
  ///
  /// <b>必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意</b>
  ///
  /// [hasAgree] 隐私权政策是否已经取得用户同意
  AMapFlutterLocation.updatePrivacyAgree(true);
}

// 判断定位权限
Future _requirePermission() async {
  PermissionStatus _permissionGranted = await Permission.location.status;
  if (_permissionGranted == PermissionStatus.denied) {
    // 如果未允许, 尝试申请一次
    _permissionGranted = await Permission.location.request();
    if (_permissionGranted != PermissionStatus.granted) {
      throw Exception("请设置允许当前程序定位功能");
    }
  }
}

Future requireLocation(AMAPListener listener) async {
  AMapFlutterLocation.setApiKey(ConstConfig.amapApiKeys.androidKey!, ConstConfig.amapApiKeys.iosKey!);
  showPrivacy();
  await _requirePermission();
  final AMapFlutterLocation location = AMapFlutterLocation();
  location.onLocationChanged().listen(listener);
  location.setLocationOption(_getAMapLocationOption());
  location.startLocation();

  return location;
}

_getAMapLocationOption() {
  AMapLocationOption locationOption = new AMapLocationOption();

  ///是否单次定位
  locationOption.onceLocation = false;

  ///是否需要返回逆地理信息
  locationOption.needAddress = true;

  ///逆地理信息的语言类型
  locationOption.geoLanguage = GeoLanguage.DEFAULT;

  locationOption.desiredLocationAccuracyAuthorizationMode =
      AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;

  locationOption.fullAccuracyPurposeKey = "AMapLocationScene";

  ///设置Android端连续定位的定位间隔
  locationOption.locationInterval = 2000;

  ///设置Android端的定位模式<br>
  ///可选值：<br>
  ///<li>[AMapLocationMode.Battery_Saving]</li>
  ///<li>[AMapLocationMode.Device_Sensors]</li>
  ///<li>[AMapLocationMode.Hight_Accuracy]</li>
  locationOption.locationMode = AMapLocationMode.Hight_Accuracy;

  ///设置iOS端的定位最小更新距离<br>
  locationOption.distanceFilter = -1;

  ///设置iOS端期望的定位精度
  /// 可选值：<br>
  /// <li>[DesiredAccuracy.Best] 最高精度</li>
  /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
  /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
  /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
  /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
  locationOption.desiredAccuracy = DesiredAccuracy.Best;

  ///设置iOS端是否允许系统暂停定位
  locationOption.pausesLocationUpdatesAutomatically = false;

  return locationOption;
}


// import 'dart:io';
//
// import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'
//     show BMFMapSDK, BMF_COORD_TYPE;
// import 'package:flutter_bmflocation/flutter_bmflocation.dart';
// import 'package:permission_handler/permission_handler.dart';
//
// Future requireLocation() async {
//   // 判断定位权限
//   PermissionStatus _permissionGranted = await Permission.location.status;
//   if (_permissionGranted == PermissionStatus.denied) {
//     // 如果未允许, 尝试申请一次
//     _permissionGranted = await Permission.location.request();
//     if (_permissionGranted != PermissionStatus.granted) {
//       throw Exception("请设置允许当前程序定位功能");
//     }
//   }
//
//   final LocationFlutterPlugin location = LocationFlutterPlugin();
//
//   // 设置是否隐私政策
//   location.setAgreePrivacy(true);
//   BMFMapSDK.setAgreePrivacy(true);
//   if (Platform.isAndroid) {
//     // Android 目前不支持接口设置Apikey,
//     // 请在主工程的Manifest文件里设置，详细配置方法请参考官网(https://lbsyun.baidu.com/)demo
//     BMFMapSDK.setCoordType(BMF_COORD_TYPE.COMMON);
//   }
//
//   return location;
// }