import 'dart:developer';
import 'dart:io';
import 'package:easy_localization/easy_localization.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/widgets/common_widgets/confirm_dialog.dart';
import 'package:permission_handler/permission_handler.dart';

import 'dialogs_and_bottomsheets.dart';

enum ConfirmAction { CANCEL, ACCEPT }

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

  log("result: $result");
  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, LocaleKeys.settingDeniedPermanently.tr(), LocaleKeys.openAppSetting.tr(), LocaleKeys.cancel.tr());
      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;
      for (var element in value.values) {
        allGranted = allGranted && element == PermissionStatus.granted;
      }

      completion(allGranted);
    });
  }

  static getDialog(BuildContext context) {
    return showDialog(
      context: context,
      builder: (BuildContext cxt) => ConfirmDialog(
        message: LocaleKeys.youNeedStoragePermissionFiles.tr(),
        onTap: () {
          Navigator.pop(context);
          openAppSettings();
        },
      ),
    );
  }

  static Future<int> getAndroidDeviceInfo() async {
    if (Platform.isAndroid) {
      AndroidDeviceInfo deviceInfo = await DeviceInfoPlugin().androidInfo;
      return deviceInfo.version.sdkInt;
    } else {
      return 0;
    }
  }

  static Future<bool> checkStoragePermissions(BuildContext context) async {
    bool permissionStatus = false;
    if (await getAndroidDeviceInfo() > 32) {
      permissionStatus = await Permission.photos.request().isGranted;
      if (permissionStatus) {
        return true;
      } else {
        getDialog(context);
        return false;
      }
    } else {
      if (Platform.isAndroid) {
        permissionStatus = await Permission.storage.request().isGranted;
      } else if (Platform.isIOS) {
        print("Ios");
        permissionStatus = await Permission.photos.request().isGranted;
      }
      if (permissionStatus) {
        return true;
      } else {
        getDialog(context);
        return false;
      }
    }
  }
}
