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 VideoLibraryIcon from '@mui/icons-material/VideoLibrary';
import Divider from '@mui/material/Divider';
import { makeStyles } from '@material-ui/core'
import { useEffect,useMemo } from 'react';

import AlertInfo from '../../../../reusableComponents/AlertInfo';
import { timeStampEU,filteredMessages,handleSort,prodAwsS3,firstLetter } from '../../../../../helpers'
import { TAllMessages } from '../../../../../typescript/redux/allMessages/types'

const useStyles = makeStyles({
  container: {
    width: '100%',
    maxHeight: '86vh',
    overflowY: 'scroll',
  '&::-webkit-scrollbar': {
    width: '0.4em'
  },
  '&::-webkit-scrollbar-track': {
    boxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
    webkitBoxShadow: 'inset 0 0 6px rgba(0,0,0,0.00)',
    backgroundColor: '#eceeec',
  },
  '&::-webkit-scrollbar-thumb': {
    backgroundColor: '#ccc8c8',
    },
  "&::-webkit-scrollbar-thumb:focus": {
    backgroundColor: "#959595",
          },
  "&::-webkit-scrollbar-thumb:active": {
    backgroundColor: "#959595",
    },
  },   
  folderIcon: {
    margin: 'auto 0px',
    color: 'rgba(0, 0, 0, 0.5)',
  },
  listItem: {
    cursor:'pointer',
    '&:hover': {
      backgroundColor: '#f0f0f0',
    }
  },  
})

interface IVideoList {
  messagesMemo: TAllMessages,
  value: string,
  date: any,
  setDisabled: React.Dispatch<boolean>,
  sort: boolean,
  handleScrollToTheMessage:(_id:string,companionId:string)  => void
}

const VideoList = ({ messagesMemo,value,date,sort,setDisabled,handleScrollToTheMessage }: IVideoList) => {
  const classes = useStyles()
  
  const filtered =  useMemo(() => {
    return messagesMemo.filter(({type}:{type:string}) => type === 'video')
    }, [messagesMemo])
  const filteredAndSorted = useMemo(() => {
    return filteredMessages(handleSort('createdAt', filtered, sort),date,value)
  },[filtered,sort,date,value])
  
  useEffect(() => {
    setDisabled(filtered.length > 0?false:true)
  }, [filtered, setDisabled])

  return (
    <>
      {filteredAndSorted.length > 0 &&
        <List className={classes.container}>
        {filteredAndSorted.map(({ createdAt, fullType,_id,companionId,name,avatarUrl,lastName,color }) =>
          <div key={createdAt}>
            <ListItem onClick={() => handleScrollToTheMessage(_id,companionId)}
              key={createdAt} alignItems="flex-start" className={classes.listItem}>
              <ListItemAvatar>
                <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
                  sx={{ background: color, width: 40, height: 40}}>
                  {`${firstLetter(name)}${firstLetter(lastName)}`}
                </Avatar>
              </ListItemAvatar>               
              <ListItemText
                primary={fullType}
                secondary={timeStampEU(createdAt)}
              />
              <VideoLibraryIcon className={classes.folderIcon} fontSize='large'/>            
            </ListItem>
            <Divider variant="inset" />
          </div>)}
      </List>}
      {(value || date)&&  filteredAndSorted.length === 0 && <AlertInfo name={`Can not find Video by request: ${value}`} />}
      {!value && !date && filteredAndSorted.length === 0 && <AlertInfo name='You do not have Video yet!'/>}
    </>
   )  
}

export default VideoList