import { useNavigation } from '@react-navigation/core';
import { CommonActions } from '@react-navigation/native';
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { StyleSheet, Text, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import Button, { ButtonType } from '../components/buttons/Button';
import { second, minute, hour } from '../constants';
import { DispatchAction } from '../contexts/reducers/store';
import { useStore } from '../contexts/store';
import { useTheme } from '../contexts/theme';
import { Screens } from '../types/navigators';
import { testIdWithKey } from '../utils/testable';
const AttemptLockout = () => {
  const {
    ColorPallet,
    TextTheme,
    Assets
  } = useTheme();
  const {
    t
  } = useTranslation();
  const [state, dispatch] = useStore();
  const [time, setTime] = useState();
  const [timeoutDone, setTimeoutDone] = useState(false);
  const navigation = useNavigation();
  const styles = StyleSheet.create({
    container: {
      flex: 1,
      alignItems: 'center',
      backgroundColor: ColorPallet.brand.primaryBackground
    },
    title: {
      ...TextTheme.headingThree,
      marginHorizontal: 50,
      textAlign: 'center',
      marginBottom: 50
    },
    description: {
      ...TextTheme.normal,
      textAlign: 'center',
      marginHorizontal: 50,
      marginBottom: 50
    },
    tryAgain: {
      ...TextTheme.normal,
      textAlign: 'center'
    },
    countDown: {
      ...TextTheme.bold,
      textAlign: 'center'
    },
    image: {
      width: 150,
      height: 150,
      marginBottom: 20,
      marginTop: 25
    }
  });

  // update the countdown timer. Return true if the lockout penalty time is over
  const updateTimeRemaining = () => {
    let penaltyServed = true;
    const penalty = state.loginAttempt.lockoutDate;
    const currDate = Date.now();
    if (penalty) {
      let diff = penalty - currDate;
      if (diff > 0) {
        penaltyServed = false;
        const hoursLeft = Math.floor(diff / hour);
        diff = diff - hoursLeft * hour;
        const minutesLeft = Math.floor(diff / minute);
        diff = diff - minutesLeft * minute;
        const secondsLeft = Math.floor(diff / second);
        const timer = {
          hours: hoursLeft,
          minutes: minutesLeft,
          seconds: secondsLeft
        };
        setTime(timer);
      }
    }
    return penaltyServed;
  };

  // run once immediately at screen initialization
  useEffect(() => {
    setTimeoutDone(updateTimeRemaining());
  }, []);

  // make sure set timeout only runs once
  useEffect(() => {
    const updateTimer = setTimeout(() => {
      // calculate time remaining
      const timerDone = updateTimeRemaining();
      setTimeoutDone(timerDone);
      if (timerDone) {
        clearInterval(updateTimer);
      }
    }, 1000);
  });
  const unlock = async () => {
    dispatch({
      type: DispatchAction.ATTEMPT_UPDATED,
      payload: [{
        loginAttempts: state.loginAttempt.loginAttempts,
        lockoutDate: undefined,
        servedPenalty: true
      }]
    });
    navigation.dispatch(CommonActions.reset({
      index: 0,
      routes: [{
        name: Screens.EnterPIN
      }]
    }));
  };
  return /*#__PURE__*/React.createElement(SafeAreaView, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(Assets.svg.appLockout, {
    style: styles.image
  }), /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(Text, {
    style: styles.title
  }, t('AttemptLockout.Title')), /*#__PURE__*/React.createElement(Text, {
    style: styles.description
  }, t('AttemptLockout.Description')), timeoutDone ? /*#__PURE__*/React.createElement(Button, {
    title: t('Global.TryAgain'),
    buttonType: ButtonType.Primary,
    testID: testIdWithKey('Enter'),
    accessibilityLabel: t('Global.TryAgain'),
    onPress: unlock
  }) : /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(Text, {
    style: styles.tryAgain
  }, t('AttemptLockout.TryAgain')), time && /*#__PURE__*/React.createElement(Text, {
    style: styles.countDown
  }, time === null || time === void 0 ? void 0 : time.hours, " ", t('AttemptLockout.Hours'), " ", time === null || time === void 0 ? void 0 : time.minutes, " ", t('AttemptLockout.Minutes'), " ", time === null || time === void 0 ? void 0 : time.seconds, ' ', t('AttemptLockout.Seconds')))));
};
export default AttemptLockout;
//# sourceMappingURL=AttemptLockout.js.map