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

import AlertInfo from '../../../../reusableComponents/AlertInfo';
import { timeStampEU,handleDownload,filteredMessages,handleSort,prodAwsS3 } 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: {
      color: '#54b0fc',
      cursor: 'pointer',
      '&:hover': {
        color: '#016cc3'
      },
  },
  listItem: {
    '&:hover': {
      backgroundColor: '#f0f0f0',
    }
  },  
})

interface IVideoList {
  messagesMemo: TAllMessages,
  value: string,
  date: any,
  setDisabled: React.Dispatch<boolean>,
  sort: boolean
}

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

  return (
    <>
      {filteredAndSorted.length > 0 &&
        <List className={classes.container}>
        {filteredAndSorted.map(({ message, createdAt, fullType }) =>
          <div key={createdAt}>
            <ListItem key={createdAt} alignItems="flex-start" className={classes.listItem}>
              <ListItemAvatar>
                <VideoLibraryIcon onClick={() =>
                  handleDownload(`${prodAwsS3}/${message}`, fullType)}
                  className={classes.folderIcon} fontSize='large' />
              </ListItemAvatar>
              <ListItemText
                primary={fullType}
                secondary={timeStampEU(createdAt)}
              />
            </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