import React from 'react';
import {StyleSheet, View, Text, LayoutChangeEvent, Linking} from 'react-native';
import {TAGG_DARK_BLUE, TOGGLE_BUTTON_TYPE} from '../../constants';
import ToggleButton from './ToggleButton';
import {RootState} from '../../store/rootReducer';
import {useSelector} from 'react-redux';
import {ScreenType} from '../../types';
import {NO_PROFILE} from '../../store/initialStates';

interface ProfileBodyProps {
  onLayout: (event: LayoutChangeEvent) => void;
  isFriend: boolean;
  isBlocked: boolean;
  handleFriendUnfriend: () => void;
  handleBlockUnblock: () => void;
  userXId: string | undefined;
  screenType: ScreenType;
}
const ProfileBody: React.FC<ProfileBodyProps> = ({
  onLayout,
  isFriend,
  isBlocked,
  handleFriendUnfriend,
  handleBlockUnblock,
  userXId,
  screenType,
}) => {
  const {
    profile = NO_PROFILE,
    user: {username},
  } = userXId
    ? useSelector((state: RootState) => state.userX[screenType][userXId])
    : useSelector((state: RootState) => state.user);

  const {biography, website} = profile;
  return (
    <View onLayout={onLayout} style={styles.container}>
      <Text style={styles.username}>{`@${username}`}</Text>
      {biography.length > 0 && (
        <Text style={styles.biography}>{`${biography}`}</Text>
      )}
      {website.length > 0 && (
        <Text
          style={styles.website}
          onPress={() => {
            Linking.openURL(
              website.startsWith('http') ? website : 'http://' + website,
            );
          }}>{`${website}`}</Text>
      )}

      {userXId && isBlocked && (
        <View style={styles.toggleButtonContainer}>
          <ToggleButton
            toggleState={isBlocked}
            handleToggle={handleBlockUnblock}
            buttonType={TOGGLE_BUTTON_TYPE.BLOCK_UNBLOCK}
          />
        </View>

      )}
      {userXId && !isBlocked && (
        <View style={styles.toggleButtonContainer}>
          <ToggleButton
            toggleState={isFriend}
            handleToggle={handleFriendUnfriend}
            buttonType={TOGGLE_BUTTON_TYPE.FRIEND_UNFRIEND}
          />
        </View>

      )}
    </View>
  );
};

const styles = StyleSheet.create({
  toggleButtonContainer: {
    flexDirection: 'row',
    flex: 1,
    paddingTop: '3.5%',
    paddingBottom: '2%',
  },
  container: {
    paddingVertical: '1%',
    paddingHorizontal: 18,
    backgroundColor: 'white',
  },
  username: {
    fontWeight: '600',
    fontSize: 16.5,
    marginBottom: '1%',
  },
  biography: {
    fontSize: 16,
    marginBottom: '0.5%',
  },
  website: {
    fontSize: 16,
    color: TAGG_DARK_BLUE,
    marginBottom: '1%',
  },
});

export default ProfileBody;
