import React from 'react';
import {View, StyleSheet, ViewProps, Keyboard} from 'react-native';
import * as Animatable from 'react-native-animatable';

interface RegistrationWizardProps extends ViewProps {
  step: 'one' | 'two' | 'three' | 'four' | 'five';
}

const RegistrationWizard = (props: RegistrationWizardProps) => {
  const stepStyle = styles.step;
  const stepActiveStyle = [styles.step, styles.stepActive];

  // detects visibility of keyboard to display or hide wizard
  const [keyboardVisible, setKeyboardVisible] = React.useState(false);

  Keyboard.addListener('keyboardDidShow', () => {
    setKeyboardVisible(true);
  });
  Keyboard.addListener('keyboardDidHide', () => {
    setKeyboardVisible(false);
  });

  return (
    <View {...props}>
      {!keyboardVisible && (
        <Animatable.View animation="fadeIn" duration={300}>
          <View style={styles.container}>
            <View style={props.step === 'one' ? stepActiveStyle : stepStyle} />
            <View style={styles.progress} />
            <View style={props.step === 'two' ? stepActiveStyle : stepStyle} />
            <View style={styles.progress} />
            <View
              style={props.step === 'three' ? stepActiveStyle : stepStyle}
            />
            <View style={styles.progress} />
            <View style={props.step === 'four' ? stepActiveStyle : stepStyle} />
            <View style={styles.progress} />
            <View style={props.step === 'five' ? stepActiveStyle : stepStyle} />
          </View>
        </Animatable.View>
      )}
      {keyboardVisible && (
        <Animatable.View animation="fadeOut" duration={300}>
          <View style={styles.container}>
            <View style={props.step === 'one' ? stepActiveStyle : stepStyle} />
            <View style={styles.progress} />
            <View style={props.step === 'two' ? stepActiveStyle : stepStyle} />
            <View style={styles.progress} />
            <View
              style={props.step === 'three' ? stepActiveStyle : stepStyle}
            />
            <View style={styles.progress} />
            <View style={props.step === 'four' ? stepActiveStyle : stepStyle} />
            <View style={styles.progress} />
            <View style={props.step === 'five' ? stepActiveStyle : stepStyle} />
          </View>
        </Animatable.View>
      )}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
  },
  step: {
    height: 18,
    width: 18,
    borderRadius: 9,
    borderWidth: 2,
    borderColor: '#e1f0ff',
  },
  stepActive: {
    backgroundColor: '#e1f0ff',
  },
  progress: {
    width: '16%',
    height: 2,
    backgroundColor: '#e1f0ff',
  },
});

export default RegistrationWizard;
