import 'dart:developer';
import 'dart:io';

import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:sfm_app/product/base/widget/dialog/request_permission_dialog.dart';

class LocationPermissionRequest {
  LocationPermissionRequest._init();
  static LocationPermissionRequest? _instance;
  static LocationPermissionRequest get instance =>
      _instance ??= LocationPermissionRequest._init();

  Future<bool> checkLocationPermission(context) async {
    var status = await Permission.location.status;
    log("Status: $status");
    if (status.isDenied || status.isPermanentlyDenied) {
      requestLocationPermisson(context, Icons.location_on_outlined,
          Permission.location, AppSettingsType.location, "Location");
      return false;
    } else {
      return true;
    }
  }

  void requestLocationPermisson(
    context,
    IconData icon,
    Permission service,
    AppSettingsType appSettingsType,
    String serviceName,
  ) async {
    var status = await service.status;
    if (status.isDenied) {
      await service.request();
    } else if (status.isPermanentlyDenied) {
      if (Platform.isAndroid) {
        RequestPermissionDialog.instance.requestPermissionDialogAndroid(
          context,
          icon,
          serviceName,
          appSettingsType,
        );
      } else if (Platform.isIOS) {
        RequestPermissionDialog.instance.requestPermissionDialogIOS(
          context,
          appSettingsType,
          serviceName,
          appSettingsType,
        );
      } else {
        log("Location Permission: Undefined Platform");
      }
    } else {}
  }
}
