import {RouteProp} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {useEffect, useState} from 'react';
import {ScrollView, StatusBar, StyleSheet, View} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import {
  AvatarTitle,
  SocialMediaInfo,
  TabsGradient,
  TaggPost,
  TwitterTaggPost,
} from '../../components';
import {AVATAR_GRADIENT} from '../../constants';
import {ProfileStackParams} from '../../routes';
import {
  SimplePostType,
  TwitterPostType,
  SocialAccountType,
  ScreenType,
} from '../../types';
import {AvatarHeaderHeight, SCREEN_HEIGHT} from '../../utils';
import {useSelector} from 'react-redux';
import {RootState} from '../../store/rootReducer';
import TaggLoadingIndicator from '../../components/common/TaggLoadingIndicator';

type SocialMediaTaggsRouteProp = RouteProp<
  ProfileStackParams,
  'SocialMediaTaggs'
>;

type SocialMediaTaggsNavigationProp = StackNavigationProp<
  ProfileStackParams,
  'SocialMediaTaggs'
>;

interface SocialMediaTaggsProps {
  route: SocialMediaTaggsRouteProp;
  navigation: SocialMediaTaggsNavigationProp;
}

const SocialMediaTaggs: React.FC<SocialMediaTaggsProps> = ({
  route,
  navigation,
}) => {
  const [accountData, setAccountData] = useState<SocialAccountType>({
    posts: [],
  });
  const {socialMediaType, userXId, screenType} = route.params;
  const {
    avatar,
    profile: {name},
  } = userXId
    ? useSelector((state: RootState) => state.userX[screenType][userXId])
    : useSelector((state: RootState) => state.user);

  const {socialAccounts} = userXId
    ? useSelector((state: RootState) => state.userX[screenType][userXId])
    : useSelector((state: RootState) => state.socialAccounts);

  useEffect(() => {
    const currentSocialData = {...socialAccounts[socialMediaType]};
    if (currentSocialData) {
      setAccountData(currentSocialData);
    }
  }, [socialAccounts, setAccountData]);

  useEffect(() => {
    navigation.setOptions({
      headerTitle: () => {
        return <AvatarTitle avatar={avatar} />;
      },
    });
  }, [avatar, navigation]);

  return (
    <LinearGradient
      useAngle={true}
      angle={148}
      style={styles.flex}
      colors={[AVATAR_GRADIENT.start, AVATAR_GRADIENT.end]}>
      <StatusBar barStyle={'light-content'} />
      {/* Cropping the scroll view to mimic the presence of a header while preserving the gradient background */}

      {accountData?.posts && accountData.posts.length > 0 ? (
        <View
          // we want a slightly bigger header here for the avatar image
          style={[styles.flex, {marginTop: AvatarHeaderHeight}]}>
          <ScrollView
            showsVerticalScrollIndicator={false}
            contentContainerStyle={styles.contentContainer}>
            <SocialMediaInfo
              fullname={name}
              type={socialMediaType}
              handle={accountData?.handle}
            />
            {(accountData?.posts as Array<
              SimplePostType | TwitterPostType
            >).map((post, index) =>
              socialMediaType === 'Twitter' ? (
                <TwitterTaggPost
                  key={index}
                  ownerHandle={accountData?.handle || '_'}
                  post={post as TwitterPostType}
                />
              ) : (
                <TaggPost
                  key={index}
                  post={post as SimplePostType}
                  social={socialMediaType}
                />
              ),
            )}
          </ScrollView>
          <TabsGradient />
        </View>
      ) : (
        <TaggLoadingIndicator color="white" />
      )}
    </LinearGradient>
  );
};

const styles = StyleSheet.create({
  contentContainer: {
    paddingBottom: SCREEN_HEIGHT / 15,
  },
  flex: {
    flex: 1,
  },
});

export default SocialMediaTaggs;
