import { Header, useHeaderHeight, HeaderBackButton } from '@react-navigation/elements';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { StyleSheet, View, ScrollView, Linking, Platform } from 'react-native';
import { PERMISSIONS, RESULTS, request, check } from 'react-native-permissions';
import { SafeAreaView } from 'react-native-safe-area-context';
import Button, { ButtonType } from '../components/buttons/Button';
import ToggleButton from '../components/buttons/ToggleButton';
import DismissiblePopupModal from '../components/modals/DismissiblePopupModal';
import SafeAreaModal from '../components/modals/SafeAreaModal';
import { useAnimatedComponents } from '../contexts/animated-components';
import { useAuth } from '../contexts/auth';
import { DispatchAction } from '../contexts/reducers/store';
import { useStore } from '../contexts/store';
import { useTheme } from '../contexts/theme';
import { useAppAgent } from '../utils/agent';
import { testIdWithKey } from '../utils/testable';
import PINEnter, { PINEntryUsage } from './PINEnter';
import { TOKENS, useServices } from '../container-api';
import { HistoryCardType } from '../modules/history/types';
import { ThemedText } from '../components/texts/ThemedText';
var UseBiometryUsage = /*#__PURE__*/function (UseBiometryUsage) {
  UseBiometryUsage[UseBiometryUsage["InitialSetup"] = 0] = "InitialSetup";
  UseBiometryUsage[UseBiometryUsage["ToggleOnOff"] = 1] = "ToggleOnOff";
  return UseBiometryUsage;
}(UseBiometryUsage || {});
const BackButton = ({
  setCanSeeCheckPIN
}) => /*#__PURE__*/React.createElement(HeaderBackButton, {
  onPress: () => setCanSeeCheckPIN(false),
  tintColor: "white",
  labelVisible: false
});
const UseBiometry = () => {
  const [store, dispatch] = useStore();
  const {
    agent
  } = useAppAgent();
  const {
    t
  } = useTranslation();
  const [logger, historyManagerCurried, historyEnabled, historyEventsLogger] = useServices([TOKENS.UTIL_LOGGER, TOKENS.FN_LOAD_HISTORY, TOKENS.HISTORY_ENABLED, TOKENS.HISTORY_EVENTS_LOGGER]);
  const {
    isBiometricsActive,
    commitWalletToKeychain,
    disableBiometrics
  } = useAuth();
  const [biometryAvailable, setBiometryAvailable] = useState(false);
  const [biometryEnabled, setBiometryEnabled] = useState(store.preferences.useBiometry);
  const [continueEnabled, setContinueEnabled] = useState(true);
  const [settingsPopupConfig, setSettingsPopupConfig] = useState(null);
  const [canSeeCheckPIN, setCanSeeCheckPIN] = useState(false);
  const {
    TextTheme,
    ColorPallet,
    Assets
  } = useTheme();
  const {
    ButtonLoading
  } = useAnimatedComponents();
  const screenUsage = useMemo(() => {
    return store.onboarding.didCompleteOnboarding ? UseBiometryUsage.ToggleOnOff : UseBiometryUsage.InitialSetup;
  }, [store.onboarding.didCompleteOnboarding]);
  const headerHeight = useHeaderHeight();
  const BIOMETRY_PERMISSION = PERMISSIONS.IOS.FACE_ID;
  const styles = StyleSheet.create({
    container: {
      height: '100%',
      padding: 20,
      backgroundColor: ColorPallet.brand.primaryBackground
    },
    image: {
      minWidth: 200,
      minHeight: 200,
      marginBottom: 66
    },
    biometryAvailableRowGap: {
      rowGap: 20
    }
  });
  useEffect(() => {
    isBiometricsActive().then(result => {
      setBiometryAvailable(result);
    });
  }, [isBiometricsActive]);
  useEffect(() => {
    if (screenUsage === UseBiometryUsage.InitialSetup) {
      return;
    }
    if (biometryEnabled) {
      commitWalletToKeychain(biometryEnabled).then(() => {
        dispatch({
          type: DispatchAction.USE_BIOMETRY,
          payload: [biometryEnabled]
        });
      });
    } else {
      disableBiometrics().then(() => {
        dispatch({
          type: DispatchAction.USE_BIOMETRY,
          payload: [biometryEnabled]
        });
      });
    }
  }, [screenUsage, biometryEnabled, commitWalletToKeychain, disableBiometrics, dispatch]);
  const logHistoryRecord = useCallback(type => {
    try {
      if (!(agent && historyEnabled)) {
        logger.trace(`[${UseBiometry.name}]:[logHistoryRecord] Skipping history log, either history function disabled or agent undefined!`);
        return;
      }
      const historyManager = historyManagerCurried(agent);

      /** Save history record for card accepted */
      const recordData = {
        type: type,
        message: type,
        createdAt: new Date()
      };
      historyManager.saveHistory(recordData);
    } catch (err) {
      logger.error(`[${UseBiometry.name}]:[logHistoryRecord] Error saving history: ${err}`);
    }
  }, [agent, historyEnabled, logger, historyManagerCurried]);
  const continueTouched = useCallback(async () => {
    setContinueEnabled(false);
    await commitWalletToKeychain(biometryEnabled);
    dispatch({
      type: DispatchAction.USE_BIOMETRY,
      payload: [biometryEnabled]
    });
  }, [biometryEnabled, commitWalletToKeychain, dispatch]);
  const onOpenSettingsTouched = async () => {
    await Linking.openSettings();
    onOpenSettingsDismissed();
  };
  const onOpenSettingsDismissed = () => {
    setSettingsPopupConfig(null);
  };
  const onSwitchToggleAllowed = useCallback(newValue => {
    if (screenUsage === UseBiometryUsage.ToggleOnOff) {
      setCanSeeCheckPIN(true);
      if (historyEventsLogger.logToggleBiometry && store.onboarding.didAgreeToTerms && store.onboarding.didConsiderBiometry) {
        const type = HistoryCardType.ActivateBiometry;
        logHistoryRecord(type);
      }
    } else {
      setBiometryEnabled(newValue);
    }
  }, [screenUsage, historyEventsLogger.logToggleBiometry, logHistoryRecord, store.onboarding.didAgreeToTerms, store.onboarding.didConsiderBiometry]);
  const onRequestSystemBiometrics = useCallback(async newToggleValue => {
    const permissionResult = await request(BIOMETRY_PERMISSION);
    switch (permissionResult) {
      case RESULTS.GRANTED:
      case RESULTS.LIMITED:
        // Granted
        onSwitchToggleAllowed(newToggleValue);
        break;
      default:
        break;
    }
  }, [onSwitchToggleAllowed, BIOMETRY_PERMISSION]);
  const onCheckSystemBiometrics = useCallback(async () => {
    if (Platform.OS === 'android') {
      // Android doesn't need to prompt biometric permission
      // for an app to use it.
      return biometryAvailable ? RESULTS.GRANTED : RESULTS.UNAVAILABLE;
    } else if (Platform.OS === 'ios') {
      return await check(BIOMETRY_PERMISSION);
    }
    return RESULTS.UNAVAILABLE;
  }, [biometryAvailable, BIOMETRY_PERMISSION]);
  const toggleSwitch = useCallback(async () => {
    const newValue = !biometryEnabled;
    if (!newValue) {
      // Turning off doesn't require OS'es biometrics enabled
      onSwitchToggleAllowed(newValue);
      return;
    }

    // If the user is turning it on, they need
    // to first authenticate the OS'es biometrics before this action is accepted
    const permissionResult = await onCheckSystemBiometrics();
    switch (permissionResult) {
      case RESULTS.GRANTED:
      case RESULTS.LIMITED:
        // Already granted
        onSwitchToggleAllowed(newValue);
        break;
      case RESULTS.UNAVAILABLE:
        setSettingsPopupConfig({
          title: t('Biometry.SetupBiometricsTitle'),
          description: t('Biometry.SetupBiometricsDesc')
        });
        break;
      case RESULTS.BLOCKED:
        // Previously denied
        setSettingsPopupConfig({
          title: t('Biometry.AllowBiometricsTitle'),
          description: t('Biometry.AllowBiometricsDesc')
        });
        break;
      case RESULTS.DENIED:
        // Has not been requested
        await onRequestSystemBiometrics(newValue);
        break;
      default:
        break;
    }
  }, [onSwitchToggleAllowed, onRequestSystemBiometrics, onCheckSystemBiometrics, biometryEnabled, t]);
  const onAuthenticationComplete = useCallback(status => {
    // If successfully authenticated the toggle may proceed.
    if (status) {
      setBiometryEnabled(previousState => !previousState);
    }
    if (historyEventsLogger.logToggleBiometry && store.onboarding.didAgreeToTerms && store.onboarding.didConsiderBiometry) {
      const type = HistoryCardType.DeactivateBiometry;
      logHistoryRecord(type);
    }
    setCanSeeCheckPIN(false);
  }, [historyEventsLogger.logToggleBiometry, logHistoryRecord, store.onboarding.didAgreeToTerms, store.onboarding.didConsiderBiometry]);
  const renderHeaderLeft = useCallback(() => /*#__PURE__*/React.createElement(BackButton, {
    setCanSeeCheckPIN: () => setCanSeeCheckPIN(false)
  }), [setCanSeeCheckPIN]);
  return /*#__PURE__*/React.createElement(SafeAreaView, {
    edges: ['left', 'right', 'bottom']
  }, settingsPopupConfig && /*#__PURE__*/React.createElement(DismissiblePopupModal, {
    title: settingsPopupConfig.title,
    description: settingsPopupConfig.description,
    onCallToActionLabel: t('Biometry.OpenSettings'),
    onCallToActionPressed: onOpenSettingsTouched,
    onDismissPressed: onOpenSettingsDismissed
  }), /*#__PURE__*/React.createElement(ScrollView, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      alignItems: 'center'
    }
  }, /*#__PURE__*/React.createElement(Assets.svg.biometrics, {
    style: styles.image
  })), biometryAvailable ? /*#__PURE__*/React.createElement(View, {
    style: styles.biometryAvailableRowGap
  }, /*#__PURE__*/React.createElement(ThemedText, null, t('Biometry.EnabledText1')), /*#__PURE__*/React.createElement(ThemedText, null, t('Biometry.EnabledText2'), /*#__PURE__*/React.createElement(ThemedText, {
    variant: "bold"
  }, " ", t('Biometry.Warning')))) : /*#__PURE__*/React.createElement(View, {
    style: styles.biometryAvailableRowGap
  }, /*#__PURE__*/React.createElement(ThemedText, null, t('Biometry.NotEnabledText1')), /*#__PURE__*/React.createElement(ThemedText, null, t('Biometry.NotEnabledText2'))), /*#__PURE__*/React.createElement(View, {
    style: {
      flexDirection: 'row',
      marginVertical: 20
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      flexShrink: 1,
      marginRight: 10,
      justifyContent: 'center'
    }
  }, /*#__PURE__*/React.createElement(ThemedText, {
    variant: "bold"
  }, t('Biometry.UseToUnlock'))), /*#__PURE__*/React.createElement(View, {
    style: {
      justifyContent: 'center'
    }
  }, /*#__PURE__*/React.createElement(ToggleButton, {
    testID: testIdWithKey('ToggleBiometrics'),
    isEnabled: biometryEnabled,
    isAvailable: true,
    toggleAction: toggleSwitch,
    disabled: false,
    enabledIcon: "check",
    disabledIcon: "close"
  })))), /*#__PURE__*/React.createElement(View, {
    style: {
      marginTop: 'auto',
      margin: 20
    }
  }, store.onboarding.didCompleteOnboarding || /*#__PURE__*/React.createElement(Button, {
    title: t('Global.Continue'),
    accessibilityLabel: 'Continue',
    testID: testIdWithKey('Continue'),
    onPress: continueTouched,
    buttonType: ButtonType.Primary,
    disabled: !continueEnabled
  }, !continueEnabled && /*#__PURE__*/React.createElement(ButtonLoading, null))), /*#__PURE__*/React.createElement(SafeAreaModal, {
    style: {
      backgroundColor: ColorPallet.brand.primaryBackground
    },
    visible: canSeeCheckPIN,
    transparent: false,
    animationType: 'slide',
    presentationStyle: "fullScreen"
  }, /*#__PURE__*/React.createElement(Header, {
    title: t('Screens.EnterPIN'),
    headerTitleStyle: TextTheme.headerTitle,
    headerStyle: {
      height: headerHeight
    },
    headerLeft: renderHeaderLeft
  }), /*#__PURE__*/React.createElement(PINEnter, {
    usage: PINEntryUsage.ChangeBiometrics,
    setAuthenticated: onAuthenticationComplete,
    onCancelAuth: setCanSeeCheckPIN
  })));
};
export default UseBiometry;
//# sourceMappingURL=UseBiometry.js.map