
import 'package:permission_handler/permission_handler.dart';

import 'dialogs.dart';

enum ConfirmAction { CANCEL, ACCEPT }

Future<bool> requestPermissionGranted(
    context, Permission requestPermissions) async {
  var result = await requestPermissions.request();

  switch (result) {
    case PermissionStatus.granted:
      // Application has been given permission to use the feature.
      return true;
    case PermissionStatus.denied:
      // Application has been denied permission to use the feature.
      return false;
    case PermissionStatus.permanentlyDenied:
      ConfirmAction? res = await showConfirmDialogs(
          context,
          'You was denied Permission. You have give manual permission from app setting. ',
          'Open App Setting',
          'Cancel');
      if (res == ConfirmAction.ACCEPT) {
        return false;
      } else if (res == ConfirmAction.CANCEL) {
        return false;
      }
      return false;
    case PermissionStatus.restricted:
      // iOS has restricted access to a specific feature.
      return false;
    default:
      return false;
  }
}


class AppPermissions{
  static void location(Function(bool) completion) {
    Permission.location.isGranted.then((isGranted){
      if(!isGranted){
        Permission.location.request().then((granted){
          completion(granted == PermissionStatus.granted);
        });
      }
      completion(isGranted);
    });

  }

  static void checkAll(Function(bool) completion){
    [
      Permission.location
    ].request().then((value){

      bool allGranted = false;
      value.values.forEach((element) {
        allGranted = allGranted && element == PermissionStatus.granted;
      });

      completion(allGranted);

    });
  }
}