import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import ListItemAvatar from '@mui/material/ListItemAvatar';
import Avatar from '@mui/material/Avatar';
import Divider from '@mui/material/Divider';
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
import { makeStyles } from '@material-ui/core'

import AlertInfo from '../../../../../reusableComponents/AlertInfo';
import { timeStampEU,firstLetter,prodAwsS3 } from '../../../../../../helpers'
import { TMessages } from '../../../../../../typescript/redux/messages/types'

const useStyles = makeStyles({
  container: {
    position: 'absolute',
    animationDuration: '0.2s',
    animationDirection: 'normal',
    animation: `$moveElement`,
  },  
  listItem: {
    cursor:'pointer',
    '&:hover': {
      backgroundColor: '#f0f0f0',
    }
  },
  folderIcon: {
    margin: 'auto 0px',
    color: '#54b0fc',
  },
  '@keyframes moveElement': {
   '0%': { left: '100%'},
   '100%': { left: '0%'},
  },   
})

interface ITextList {
  openPinned: boolean,
  filteredAndSorted: TMessages,
  handleScrollToTheMessage: (_id:string) => void,
}

const TextList = ({ filteredAndSorted,handleScrollToTheMessage,openPinned }: ITextList) => {
  const classes = useStyles()
  
   return filteredAndSorted.length > 0 ?(
    <List className={classes.container}>
       {filteredAndSorted.map(({ message, createdAt, lastName, name, color, avatarUrl,_id }) =>
      <div key={createdAt}>
           <ListItem onClick={() => handleScrollToTheMessage(_id)}
             alignItems="flex-start" className={classes.listItem}>
            <ListItemAvatar>
              <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
                  sx={{ background: color, width: 38, height: 38}}>
                  {`${firstLetter(name)}${firstLetter(lastName)}`}
              </Avatar>
            </ListItemAvatar>
             <ListItemText style={{ wordBreak: 'break-word',marginRight:2 }} primary={message}
               secondary={timeStampEU(createdAt)} secondaryTypographyProps={{color: '#020202',paddingTop:0.5}}/>
            <ContentCopyIcon className={classes.folderIcon} fontSize='large' />             
          </ListItem>
          <Divider variant="inset" />
      </div>)}
    </List>
   ): <AlertInfo name={`You do not have ${openPinned&&'Pinned'} Text yet!`}/>
}

export default TextList