import {
  PermissionsAndroid,
  Alert,
  Linking,
  Platform,
  ToastAndroid,
} from "react-native";
import {
  UELogEvent,
  UELogMessage,
} from "./libEventLog/UserExperior/LibUserExperior";

import {
  check,
  PERMISSIONS,
  RESULTS,
  request,
  openSettings,
} from "react-native-permissions";

import store from "react-native-digitcamera/src/redux/store";

export const getAndroidPermission = async (onGranted, onRejected) => {
  console.log("*** Permission getAndroidPermission before");
  const result = await PermissionsAndroid.requestMultiple([
    PermissionsAndroid.PERMISSIONS.CAMERA,
    PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
    PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
    PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
  ]);
  console.log("*** Permission getAndroidPermission after await ", result);
  if (
    result["android.permission.CAMERA"] === "granted" &&
    result["android.permission.RECORD_AUDIO"] === "granted" &&
    result["android.permission.WRITE_EXTERNAL_STORAGE"] === "granted" &&
    result["android.permission.READ_EXTERNAL_STORAGE"] === "granted" &&
    result["android.permission.READ_PHONE_STATE"] === "granted"
  ) {
    console.log("*** Permission getAndroidPermission granted all");
    onGranted();
    UELogEvent("Permission.js, Permissions. ", {
      "External Storage Write": JSON.stringify("granted"),
      "External Storage Read": JSON.stringify("granted"),
      "Phone State": JSON.stringify("granted"),
      Camera: JSON.stringify("granted"),
      Audio: JSON.stringify("granted"),
    });
  } else if (
    result["android.permission.CAMERA"] === "never_ask_again" ||
    result["android.permission.RECORD_AUDIO"] === "never_ask_again" ||
    result["android.permission.WRITE_EXTERNAL_STORAGE"] === "never_ask_again" ||
    result["android.permission.READ_EXTERNAL_STORAGE"] === "never_ask_again" ||
    result["android.permission.READ_PHONE_STATE"] === "never_ask_again"
  ) {
    console.log("*** Permission getAndroidPermission denied any one");
    Alert.alert(
      translate("Permission denied"),
      "We will be not able to proceed your vehicle inspection"
    );
    onRejected();
  }
};

export const detectPermissionStatus = async () => {
  if (Platform.OS === "android") {
    const result = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
    if (result === RESULTS.GRANTED) {
      store.dispatch({
        type: "GLOBAL_MODAL_HIDE",
      });
      store.dispatch({
        type: "LOCATION_GRANTED",
      });
    } else {
      await openSettings();
    }
  } else {
    const result = await check(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
    if (result === RESULTS.GRANTED) {
      store.dispatch({
        type: "GLOBAL_MODAL_HIDE",
      });
      store.dispatch({
        type: "LOCATION_GRANTED",
      });
    } else {
      await openSettings();
    }
  }

  // const result = await (PERMISSIONS.ANDROID)
  // if (result) {
  //   store.dispatch({
  //     type: "GLOBAL_MODAL_HIDE",
  //   });
  //   store.dispatch({
  //     type: "LOCATION_GRANTED",
  //   });
  // } else {
  //   openAppSettings();
  // }
};

export const requestLocationPermission = async () => {
  if (Platform.OS === "android") {
    try {
      const status = await request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
      return status;
    } catch (error) {
      console.log(
        "Permission.js . requestLocationPermission . request[ANDROID] . error - ",
        error
      );
    }
  } else {
    try {
      const status = await request(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
      return status;
    } catch (error) {
      console.log(
        "Permission.js . requestLocationPermission . request[IOS] . error - ",
        error
      );
    }
  }
  return RESULTS.UNAVAILABLE;
};
