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 LibraryMusicIcon from '@mui/icons-material/LibraryMusic';
import Divider from '@mui/material/Divider';
import { makeStyles } from '@material-ui/core'
import { useEffect, useMemo } 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: {
    cursor:'pointer',
    '&:hover': {
      backgroundColor: '#f0f0f0',
    }
  },  
})

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

const AudioList = ({ messagesMemo,value,date,sort,setDisabled,handleScrollToTheMessage }: IAudioList) => {
  const classes = useStyles()

  const filtered =  useMemo(() => {
    return messagesMemo.filter(({type}:{type:string}) => type === 'audio')
    }, [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(({ message, createdAt, fullType,_id,companionId }) =>
          <div key={createdAt}>
            <ListItem onClick={() => handleScrollToTheMessage(_id,companionId)}
              alignItems="flex-start" className={classes.listItem}>
              <ListItemAvatar>
                <LibraryMusicIcon 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 Audio by request: ${value}`} />}
      {!value && !date && filteredAndSorted.length === 0 && <AlertInfo name='You do not have Audio yet!'/>}
   </>)
}
export default AudioList