import { makeStyles, TextField } from '@material-ui/core'
import ListItemAvatar from '@mui/material/ListItemAvatar';
import Avatar from '@mui/material/Avatar';
import AddAPhotoIcon from '@mui/icons-material/AddAPhoto';
import { useEffect} from 'react';
import { useDropzone } from 'react-dropzone';
import { format,firstLetter } from '../../../../../helpers'
import { IAuthorizationState } from '../../../../../typescript/redux/authorization/interfaces'

const useStyles = makeStyles({
  container: {
    display: 'flex',
    alignItems: 'center',
    alignContent:'center',
    flexDirection: 'column',
    width: '100%',
    margin: '0 auto',
    padding: 20,
    paddingTop:0,
    position: 'relative',
    backgroundColor: '#ffffff',
  },
  imgWrapper: {
    cursor: 'pointer',
    marginBottom: 30,
    position: 'relative',
  },
  imgDropZoneOverlay: {
    position: 'absolute',
    width: 120,
    height: 120,
    borderRadius: '50%',
    backgroundColor: '#6868687d',
    left: 0,
    zIndex: 1,
    display: 'flex',
    justifyContent: 'center',
    alignContent: 'center',
    alignItems:'center'
  },
  addPhoto: {
    color: '#ffffff',
    transform: 'scale(1.1)',
    '&:hover': {
      transform: 'scale(1.3)',
    },
  },
  avatarArrow: {
    cursor: 'pointer',
    alignSelf: 'flex-end',
    '&:hover': {
      backgroundColor: 'rgb(62, 149, 231)'
    }
  }
})

interface IEditList {
  user: IAuthorizationState,
  name: string,
  setName: any,
  lastName: string,
  setLastName: any,
  openBtn: boolean,
  setOpenBtn: any,
  file:any,
  setFile: (file:any) => void
}

const EditList = (props: IEditList) => {
  const classes = useStyles()
  const { user, name, setName, lastName, setLastName,
    openBtn, setOpenBtn,setFile } = props
  const { avatarUrl, color } = user
  const { getRootProps, getInputProps, acceptedFiles } = useDropzone({
        noDrag: true,
        accept:'image/*'
    });

  const handleTextField = (e: React.ChangeEvent<HTMLInputElement>) => {
  !openBtn&&setOpenBtn(true)
  const value = format(e.target.value)
  const name = e.target.name
    switch (name) {
      case 'name':
        setName(value)
        break;
      case 'lastName':
        setLastName(value)
        break;
      default:
        break;
    }
  }

  useEffect(() => {
    if (acceptedFiles.slice(-1)[0]) {
      setFile(acceptedFiles.slice(-1)[0])
      setOpenBtn(true)
    }
  }, [setFile,setOpenBtn,acceptedFiles])

  return (
    <div className={classes.container} >     
      <ListItemAvatar className={classes.imgWrapper}>
        <div {...getRootProps({ className: classes.imgDropZoneOverlay })}>
          <AddAPhotoIcon className={classes.addPhoto} fontSize='large' />
          <input {...getInputProps()}/>
        </div>
         <Avatar alt={name} src={avatarUrl?`http://localhost:3000/${avatarUrl}`:undefined}
           sx={{ background: color, width: 120, height: 120}}>
           {`${firstLetter(name)}${firstLetter(lastName)}`}
         </Avatar>
      </ListItemAvatar>                 
      <TextField
        id="name"
        name='name'
        label="Name"
        value={name}
        fullWidth
        variant='outlined'
        onChange={handleTextField}
        style={{marginBottom:20}}
        />
      <TextField
        id="lastName"
        name='lastName'
        label="LastName"
        value={lastName}
        fullWidth
        variant='outlined'
        onChange={handleTextField}
      />        
    </div>
  )
};

export default EditList;