import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/widgets/dialogs/confirm_dialog.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart';

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

  static void showErrorLocationDialog(bool isPermissionError, BuildContext context) {
    showDialog(
      context: context,
      builder: (cxt) => ConfirmDialog(
        message: "Please provide location permission",
        onTap: () {
          if (isPermissionError) {
            Geolocator.openAppSettings();
          } else {
            Geolocator.openLocationSettings();
          }
          Navigator.pop(context);
          // createVacationRule(list);
        },
      ),
    );
  }

  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);
    });
  }
}
