import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useRef, useState} from 'react';
import {Image, StyleSheet, Text, View, ViewProps} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';
import Animated, {EasingNode} from 'react-native-reanimated';
import {useDispatch, useStore} from 'react-redux';
import {RootState} from '../../store/rootReducer';
import {MomentPostType, MomentTagType, ScreenType, UserType} from '../../types';
import {
  getTimePosted,
  navigateToProfile,
  normalize,
  SCREEN_WIDTH,
} from '../../utils';
import {mentionPartTypes, renderTextWithMentions} from '../../utils/comments';
import {CommentsCount} from '../comments';
import {MomentTags} from '../common';

interface MomentPostContentProps extends ViewProps {
  screenType: ScreenType;
  moment: MomentPostType;
  momentTags: MomentTagType[];
}
const MomentPostContent: React.FC<MomentPostContentProps> = ({
  screenType,
  moment,
  style,
  momentTags,
}) => {
  const state: RootState = useStore().getState();
  const navigation = useNavigation();
  const dispatch = useDispatch();
  const [elapsedTime, setElapsedTime] = useState('');
  const [tags, setTags] = useState<MomentTagType[]>(momentTags);
  const imageRef = useRef(null);
  const [visible, setVisible] = useState(false);

  const [fadeValue, setFadeValue] = useState<Animated.Value<number>>(
    new Animated.Value(0),
  );

  useEffect(() => {
    setTags(momentTags);
  }, [momentTags]);

  useEffect(() => {
    setElapsedTime(getTimePosted(moment.date_created));
  }, [moment.date_created]);

  useEffect(() => {
    const fade = async () => {
      Animated.timing(fadeValue, {
        toValue: 1,
        duration: 250,
        easing: EasingNode.linear,
      }).start();
    };
    fade();
  }, [fadeValue]);

  return (
    <View style={[styles.container, style]}>
      <TouchableWithoutFeedback
        onPress={() => {
          setVisible(!visible);
          setFadeValue(new Animated.Value(0));
        }}>
        <Image
          ref={imageRef}
          style={styles.image}
          source={{uri: moment.moment_url}}
          resizeMode={'cover'}
        />
        {tags.length > 0 && (
          <Image
            source={require('../../assets/icons/tag_indicate.png')}
            style={styles.tagIcon}
          />
        )}
      </TouchableWithoutFeedback>
      {visible && (
        <Animated.View style={[styles.tapTag, {opacity: fadeValue}]}>
          <MomentTags
            editing={false}
            tags={tags}
            setTags={() => null}
            imageRef={imageRef}
          />
        </Animated.View>
      )}
      <View style={styles.footerContainer}>
        <CommentsCount
          commentsCount={moment.comments_count.toString()}
          momentId={moment.moment_id}
          screenType={screenType}
        />
        <Text style={styles.text}>{elapsedTime}</Text>
      </View>
      {renderTextWithMentions({
        value: moment.caption,
        styles: styles.captionText,
        partTypes: mentionPartTypes('white'),
        onPress: (user: UserType) =>
          navigateToProfile(state, dispatch, navigation, screenType, user),
      })}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {},
  image: {
    width: SCREEN_WIDTH,
    aspectRatio: 1,
    marginBottom: '3%',
  },
  footerContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginLeft: '7%',
    marginRight: '5%',
    marginBottom: '2%',
  },
  text: {
    position: 'relative',
    paddingBottom: '1%',
    paddingTop: '1%',
    marginLeft: '7%',
    marginRight: '2%',
    color: '#ffffff',
    fontWeight: 'bold',
  },
  captionText: {
    position: 'relative',
    paddingTop: '1%',
    marginLeft: '5%',
    marginRight: '5%',
    color: '#ffffff',
    fontWeight: '500',
    fontSize: normalize(13),
    lineHeight: normalize(15.51),
    letterSpacing: normalize(0.6),
  },
  tapTag: {
    position: 'absolute',
  },
  tagIcon: {
    width: normalize(30),
    height: normalize(30),
    position: 'absolute',
    bottom: '7%',
    left: normalize(20),
  },
});
export default MomentPostContent;
