/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-09-23 14:33:56
 * @LastEditTime : 2023-05-16 16:54:10
 * @Description  : Do not edit
 */
// import 'package:attendance_package/src/models/response_model/clock_status/location_clock_model.dart';
// import 'package:attendance_package/src/service/impl/location_service_impl.dart';
import 'package:flutter/material.dart';
// import 'package:geolocator/geolocator.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'dart:async';
// import 'dart:io';
import 'package:app_settings/app_settings.dart';
// import 'dart:math' as math;
import 'package:mobx/mobx.dart';
part 'location_util_store.g.dart';

// enum _PositionItemType {
//   log,
//   position,
// }

// class _PositionItem {
//   _PositionItem(this.type, this.displayValue);

//   final _PositionItemType type;
//   final String displayValue;
// }

class LocationUtilStore = _LocationUtilStore with _$LocationUtilStore;

abstract class _LocationUtilStore with Store, ChangeNotifier {
  ///上下文（请勿修改）
  final BuildContext context1;

  _LocationUtilStore(this.context1) {
    ///可在此处理进入页面前的操作，比如响应路由的传参进行赋值。
  }

  // Position? _myLocation;
  // Position? get myLocation => _myLocation;
  // set myLocation(value) => _myLocation = value;
  String? _addr;

  get addr => _addr;

  // final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
  // GeolocatorPlatform get locationPlugin => _geolocatorPlatform;

  Future startLocation() async {
    // // 动态申请定位权限
    // await requestLocationPermission();
    reStartLocation();
  }

  // 高德地图坐标转百度地图
  // aMapToBdMap(WisLocationInfo locationInfo) {
  //   if (locationInfo.longitude != null || locationInfo.latitude != null) {
  //     var xPI = math.pi * 3000.0 / 180.0;
  //     var x = locationInfo.longitude, y = locationInfo.latitude;
  //     var z = math.sqrt(x! * x + y! * y) + 0.00002 * math.sin(y * xPI);
  //     var theta = math.atan2(y, x) + 0.000003 * math.cos(x * xPI);
  //     locationInfo.longitude = z * math.cos(theta) + 0.0065;
  //     locationInfo.latitude = z * math.sin(theta) + 0.006;
  //     return locationInfo;
  //   }
  // }

  // 定位未开启窗口
  showLocationNoOpenDialog() {
    showDialog(
      context: context1,
      builder: (context1) {
        return Wisdom(
            child: WisDialog(
          title: Text('提示'),
          insetPadding: EdgeInsets.symmetric(vertical: 0),
          content: Wisdom.column(
            height: 250.pt,
            width: 200.pt,
            color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 20.pt),
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Wisdom.column(
                children: [
                  // Image(
                  //   image: AssetList.$dwsb_png_image,
                  //   height: 110.pt,
                  // ),
                  WisText(
                    '定位未开启哦~',
                    color: Colors.black,
                    size: 18.pt,
                  ),
                  WisText(
                    '请开启定位后再进行任务操作',
                    color: Colors.grey,
                    size: 14.pt,
                  ),
                ],
              ),
              WisButton(
                constraints: BoxConstraints(minHeight: 44.pt, minWidth: 267.pt),
                child: Text('去开启定位'),
                onPressed: () => AppSettings.openAppSettings(),
              )
            ],
          ),
        ));
      },
    );
  }

  // 定位错误窗口
  showErrorLocationDialog() {
    showDialog(
      context: context1,
      builder: (context1) {
        return Wisdom(
            child: WisDialog(
          title: Text('提示'),
          insetPadding: EdgeInsets.symmetric(vertical: 0),
          content: Wisdom.column(
            height: 280.pt,
            width: 200.pt,
            color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 20.pt),
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Wisdom.column(
                children: [
                  // Image(
                  //   image: AssetList.$dwsb_png_image,
                  //   height: 110.pt,
                  // ),
                  WisText(
                    '定位获取失败哦~',
                    color: Colors.black,
                    size: 18.pt,
                  ),
                  WisText(
                    '信号不好，请确认手机开启定位，位置信息后，重新操作',
                    color: Colors.grey,
                    size: 14.pt,
                  ),
                ],
              ),
              WisButton(
                constraints: BoxConstraints(minHeight: 44.pt, minWidth: 267.pt),
                child: Text('重新定位'),
                onPressed: () {
                  reStartLocation();
                  Navigator.pop(context1);
                },
              )
            ],
          ),
        ));
      },
    );
  }

  /// 申请定位权限
  /// 授予定位权限返回true， 否则返回false
  // Future<bool> requestLocationPermission() async {
  //   bool locationServiceEnabled = true;
  //   //获取当前的权限
  //   LocationPermission status = await Geolocator.checkPermission();
  //   switch (status) {
  //     case LocationPermission.denied:
  //       locationServiceEnabled = false;
  //       break;
  //     case LocationPermission.deniedForever:
  //       locationServiceEnabled = false;
  //       break;
  //     case LocationPermission.unableToDetermine:
  //       locationServiceEnabled = false;
  //       break;
  //     default:
  //       break;
  //   }
  //   print("GeoUtil - isReady - $locationServiceEnabled");
  //   return locationServiceEnabled;
  // }

  // Future<bool> getLocationPermission() async {
  //   bool locationServiceEnabled = true;
  //   //获取当前的权限
  //   LocationPermission status = await Geolocator.requestPermission();
  //   switch (status) {
  //     case LocationPermission.denied:
  //       locationServiceEnabled = false;
  //       break;
  //     case LocationPermission.deniedForever:
  //       locationServiceEnabled = false;
  //       break;
  //     case LocationPermission.unableToDetermine:
  //       locationServiceEnabled = false;
  //       break;
  //     default:
  //       break;
  //   }
  //   print("GeoUtil - isReady - $locationServiceEnabled");
  //   return locationServiceEnabled;
  // }

  ///获取iOS native的accuracyAuthorization类型
  // void requestAccuracyAuthorization() async {
  //   AMapAccuracyAuthorization currentAccuracyAuthorization =
  //       await _locationPlugin.getSystemAccuracyAuthorization();
  //   if (currentAccuracyAuthorization ==
  //       AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {
  //     // print("精确定位类型");
  //   } else if (currentAccuracyAuthorization ==
  //       AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) {
  //     // print("模糊定位类型");
  //   } else {
  //     // print("未知定位类型");
  //   }
  // }

  void destroy() {
    ///销毁定位
    // _locationPlugin.destroy();
  }

  ///重新定位
  void reStartLocation() {
    // if (null != _locationPlugin) {
    // _getCurrentPosition();
    // }
  }

  // Future<void> _getCurrentPosition() async {
  //   final hasPermission = await requestLocationPermission();
  //   print(hasPermission);
  //   if (!hasPermission) {
  //     return;
  //   }

  //   Position position = await _geolocatorPlatform.getCurrentPosition(
  //       locationSettings: LocationSettings(
  //     accuracy: LocationAccuracy.best,
  //   ));
  //   myLocation = position;
  //   // _updatePositionList(
  //   //   _PositionItemType.position,
  //   //   position.toString(),
  //   // );
  // }

  // 逆地理获取地址 (2024.07.05 去除高德)
  // Future onGetLocateName() async {
  //   var res = await GlobalApi.wisGetLocationName(
  //       'output=JSON&location=${_myLocation!.longitude},${_myLocation!.latitude}&poitype=商务写字楼&radius=5&extensions=all&batch=false&roadlevel=0');
  //   _addr = res!['formatted_address'] is String ? res['formatted_address'] : '';
  //   notifyListeners();
  // }
}
