import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmflocation/flutter_bmflocation.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/view_widget/request_permission.dart';
import 'package:permission_handler/permission_handler.dart';

import 'font_weight.dart';


BaiduLocationAndroidOption initAndroidOptions() {
  BaiduLocationAndroidOption options = BaiduLocationAndroidOption(
    // 定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
      locationMode: BMFLocationMode.hightAccuracy,
      // 是否需要返回地址信息
      isNeedAddress: true,
// 是否需要返回海拔高度信息
      isNeedAltitude: false,
// 是否需要返回周边poi信息
      isNeedLocationPoiList: false,
// 是否需要返回新版本rgc信息
      isNeedNewVersionRgc: false,
// 是否需要返回位置描述信息
      isNeedLocationDescribe: true,
// 是否使用gps
      openGps: true,
// 可选,设置场景定位参数,包括签到场景、运动场景、出行场景
      locationPurpose: BMFLocationPurpose.signIn,
// 坐标系
      coordType: BMFLocationCoordType.bd09ll,
// 设置发起定位请求的间隔,int类型,单位ms
// 如果设置为0,则代表单次定位,即仅定位一次,默认为0
      scanspan: 0);
  return options;
}

BaiduLocationIOSOption initIOSOptions() {
  BaiduLocationIOSOption options = BaiduLocationIOSOption(
    // 坐标系
    coordType: BMFLocationCoordType.bd09ll,
    // 位置获取超时时间
    locationTimeout: 10,
    // 获取地址信息超时时间
    reGeocodeTimeout: 10,
    // 应用位置类型 默认为automotiveNavigation
    activityType: BMFActivityType.automotiveNavigation,
    // 设置预期精度参数 默认为best
    desiredAccuracy: BMFDesiredAccuracy.best,
    // 是否需要最新版本rgc数据
    isNeedNewVersionRgc: false,
    // 指定定位是否会被系统自动暂停
    pausesLocationUpdatesAutomatically: false,
    // 指定是否允许后台定位,
    // 允许的话是可以进行后台定位的,但需要项目配置允许后台定位,否则会报错,具体参考开发文档
    allowsBackgroundLocationUpdates: false,
    // 设定定位的最小更新距离
    distanceFilter: 10,
  );
  return options;
}


class Location {
  static Location _instance;

  Location._internal() {
    aMapFlutterLocation = LocationFlutterPlugin();
  }

  static Location getInstance() {
    if (_instance == null) {
      _instance = Location._internal();
    }
    return _instance;
  }

  LocationFlutterPlugin aMapFlutterLocation;

  Future<bool> startLocation(context, Function(BaiduLocation result) locationCallback) async {
    aMapFlutterLocation.prepareLoc(initAndroidOptions().getMap(), initIOSOptions().getMap());
    if (!(await Permission.locationWhenInUse.serviceStatus.isEnabled)) {
      enableLocation(context);
      return false;
    }
    if (await Permission.location.isPermanentlyDenied) {
      requestDialog(context, locationCallback);
      return false;
    } else if (await Permission.location.isGranted) {
      aMapFlutterLocation.singleLocationCallback(callback: (BaiduLocation result) {
        locationCallback.call(result);
      });
      if (Platform.isIOS) {
        await aMapFlutterLocation.singleLocation({
          'isReGeocode': true,
          'isNetworkState': true,
        });
      } else if (Platform.isAndroid) {
        await aMapFlutterLocation.startLocation();
      }
      return true;
    } else if (await Permission.location.isUndetermined) {
      showAlertDialog(context);
      await Permission.location.request();
      Navigator.of(context).pop();
      return false;
    } else {
      if (Platform.isIOS) {
        //去设置中心
        requestDialog(context, locationCallback);
      } else {
        showAlertDialog(context);
        await Permission.location.request();
        Navigator.of(context).pop();
      }
      return false;
    }
  }

  void stopLocation() {
    if (aMapFlutterLocation != null) aMapFlutterLocation.stopLocation();
  }

  enableLocation(context) {
    showCupertinoDialog(
      context: context,
      builder: (context) {
        return RequestPermission(
          "assets/image/icon_permission_location_bg.webp",
          S.of(context).nindingweigongnengweikaiqi,
          S.of(context).weilexiangnintuijianfujindemendianxinxi,
          S.of(context).dakaidingwei,
          (result) async {
            if (result) {
              final AndroidIntent intent = AndroidIntent(
                  action: 'action_location_source_settings',
                  package: "com.zsw.huixiang");
              await intent.launch();
              // startLocation();
            }
          },
          heightRatioWithWidth: 0.82,
        );
      },
    );
  }

  requestDialog(context, Function(BaiduLocation result) locationCallback) {
    showCupertinoDialog(
      context: context,
      builder: (context) {
        return RequestPermission(
          "assets/image/icon_permission_location_bg.webp",
          S.of(context).nindingweiquanxianweiyunxu,
          S.of(context).weilexiangnintuijianfujindemendianxinxi,
          S.of(context).kaiqiquanxian,
          (result) async {
            if (result) {
              await openAppSettings();
              if (await Permission.location.isGranted) {
                startLocation(context, locationCallback);
              }
            }
          },
          heightRatioWithWidth: 0.82,
        );
      },
    );
  }

  ///定位权限说明弹窗
  showAlertDialog(context) {
    //显示对话框
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          titlePadding: EdgeInsets.all(10),
          backgroundColor: Colors.transparent,
          elevation: 0,
          alignment: Alignment.topCenter,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(6),
          ),
          children: <Widget>[
            Container(
              width: double.infinity,
              height: 120.h,
              padding: EdgeInsets.all(16),
              decoration: new BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              child:Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "访问精确地理位置信息权限说明",
                    style: TextStyle(
                      fontSize: 15.sp,
                      fontWeight:MyFontWeight.regular,
                      color: Colors.black,
                    ),
                  ),
                  SizedBox(height: 3.h,),
                  Text(
                    "为了向您推荐附近的门店信息,推荐您在使用期间让我们使用位置信息,不授权该权限不影响app正常使用。",
                    style: TextStyle(
                      fontSize: 13.sp,
                      height: 1.3.h,
                      fontWeight:MyFontWeight.regular,
                      color: Colors.black,
                    ),
                  ),
                ],
              ),
            ),
          ],
        );
      },
    );
  }

}