import React, {useContext} from 'react';
import {Image, StyleSheet, View} from 'react-native';
import {getMember, normalize} from '../../utils';
import {useMessageContext} from 'stream-chat-react-native-core';
import {useStore} from 'react-redux';
import {ChatContext} from '../../App';

const MessageAvatar: React.FC = () => {
  const {channel} = useContext(ChatContext);
  const state = useStore().getState();
  const member = getMember(channel, state);
  const message = useMessageContext();

  return (
    <View style={styles.messageAvatarContainer}>
      {message.lastGroupMessage === true && (
        <Image
          style={styles.messageAvatar}
          source={
            member
              ? {uri: member.user?.thumbnail_url}
              : require('../../assets/images/avatar-placeholder.png')
          }
        />
      )}
    </View>
  );
};

const styles = StyleSheet.create({
  messageAvatarContainer: {
    width: normalize(20),
    zIndex: 1,
    bottom: 20,
    marginRight: '2%',
  },
  messageAvatar: {
    borderRadius: 50,
    width: normalize(18),
    height: normalize(18),
  },
});

export default MessageAvatar;
