import React, {useEffect} from 'react';
import {StyleSheet, Text} from 'react-native';
import {View} from 'react-native-animatable';
import {Easing, useSharedValue, withTiming} from 'react-native-reanimated';
import {SafeAreaView} from 'react-native-safe-area-context';
import {useSelector} from 'react-redux';
import {RootState} from '../../store/rootReducer';
import {MomentUploadStatusType} from '../../types';
import {normalize, SCREEN_WIDTH, StatusBarHeight} from '../../utils';
import {GradientProgressBar} from '../common';

interface MomentUploadProgressBarProps {}

const MomentUploadProgressBar: React.FC<MomentUploadProgressBarProps> =
  ({}) => {
    const {momentUploadStatus} = useSelector((state: RootState) => state.user);
    const progress = useSharedValue(0);

    useEffect(() => {
      if (momentUploadStatus === MomentUploadStatusType.Uploading) {
        progress.value = withTiming(1, {
          duration: 30 * 1000,
          easing: Easing.out(Easing.quad),
        });
      }
    }, [momentUploadStatus]);

    return (
      <View style={styles.background}>
        <SafeAreaView style={styles.container}>
          <Text style={styles.text}>Uploading Moment...</Text>
          <GradientProgressBar style={styles.bar} progress={progress} />
        </SafeAreaView>
      </View>
    );
  };

const styles = StyleSheet.create({
  background: {
    position: 'absolute',
    zIndex: 999,
    height: StatusBarHeight + 100,
    backgroundColor: 'white',
    width: '100%',
    alignItems: 'center',
  },
  container: {
    justifyContent: 'space-evenly',
    height: '100%',
  },
  text: {
    fontSize: normalize(14),
    fontWeight: 'bold',
    lineHeight: 17,
    marginVertical: 12,
  },
  bar: {
    width: SCREEN_WIDTH * 0.9,
  },
});

export default MomentUploadProgressBar;
