import React from 'react';
import {View, Text, StyleSheet, ViewProps} from 'react-native';
import {TouchableOpacity} from 'react-native-gesture-handler';
import {useNavigation} from '@react-navigation/native';
import {RootState} from '../../store/rootReducer';
import {useSelector} from 'react-redux';
import {ScreenType} from '../../types';
import {EMPTY_PROFILE_PREVIEW_LIST} from '../../store/initialStates';

interface FollowCountProps extends ViewProps {
  mode: 'followers' | 'following';
  userXId: string | undefined;
  screenType: ScreenType;
}

const FollowCount: React.FC<FollowCountProps> = ({
  style,
  mode,
  userXId,
  screenType,
}) => {
  const {
    followers = EMPTY_PROFILE_PREVIEW_LIST,
    following = EMPTY_PROFILE_PREVIEW_LIST,
  } = userXId
    ? useSelector((state: RootState) => state.userX[screenType][userXId])
    : useSelector((state: RootState) => state.follow);

  const isFollowers = mode === 'followers';
  const count = isFollowers ? followers.length : following.length;

  const navigation = useNavigation();
  const displayed: string =
    count < 5e3
      ? `${count}`
      : count < 1e5
      ? `${(count / 1e3).toFixed(1)}k`
      : count < 1e6
      ? `${(count / 1e3).toFixed(0)}k`
      : `${count / 1e6}m`;
  return (
    <TouchableOpacity
      onPress={() =>
        navigation.push('FollowersListScreen', {
          isFollowers,
          userXId,
          screenType,
        })
      }>
      <View style={[styles.container, style]}>
        <Text style={styles.count}>{displayed}</Text>
        <Text style={styles.label}>
          {isFollowers ? 'Followers' : 'Following'}
        </Text>
      </View>
    </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
  },
  count: {
    fontWeight: '700',
    fontSize: 18,
  },
  label: {
    fontWeight: '400',
    fontSize: 16,
  },
});

export default FollowCount;
