import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:flutter/material.dart';
import 'package:localstorage/localstorage.dart';
import 'package:permission_handler/permission_handler.dart';
// import 'package:vibration/vibration.dart';
import 'package:geolocator/geolocator.dart' as geo;

class PermissionService extends BaseService {

  geo.LocationPermission? locationPermission;
  AppGlobal appGlobal = new AppGlobal();

  setVibrationPermission(flag) async {
    localStorage.setItem('isVibration', flag);
  }

  isVibrationEnabled() {
    return (localStorage.getItem('isVibration') == null) ||
            (localStorage.getItem('isVibration')) == false
        ? false
        : true;
  }

  vibrate(callback, context) async {
    if (callback == null) return null;
    if (isVibrationEnabled() == true) {
      // if (await Vibration.hasVibrator() !=null) {
      //   Vibration.vibrate(duration: 100);
      //   callback();
      // }
    } else {
      callback();
    }
  }

  setTheme(flag) async {
    localStorage.setItem('isTheme', flag);
  }

  isThemeEnabled() {
    return localStorage.getItem('isTheme');
  }

  cameraPermission() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.camera,
    ].request();
  }

  static isCameraEnabled() async {
    return await Permission.camera.isGranted;
  }

  static isExternalStorageEnabled() async {
    return await Permission.storage.isGranted;
  }

  static isHealthDataPermissionEnabled() async {
    return await Permission.sensors.isGranted;
  }

  static isMicrophonePermissionEnabled() async {
    return await Permission.microphone.isGranted;
  }

  static isCalendarPermissionEnabled() async {
    return await Permission.calendarFullAccess.isGranted;
  }

  setCameraLocationPermission(context) async {
    Navigator.pop(context);
    openAppSettings();
  }

  static isLocationEnabled() async {
    var permission = await geo.Geolocator.checkPermission();
    if (permission == geo.LocationPermission.denied) {
      return false;
    } else {
      return true;
    }
  }

  openSettings() async {
    openAppSettings();
  }

  openAccessbility() {
   // OpenSettings.openAppSetting();
  }
}
