import { makeStyles } from "@material-ui/core/styles";
import { styled } from '@mui/material/styles';
import { useState, useRef } from "react";
import Typography from '@mui/material/Typography';
import DownloadForOfflineIcon from '@mui/icons-material/DownloadForOffline';
import ImageIcon from '@mui/icons-material/Image';
import Button from '@mui/material/Button';
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import Divider from '@mui/material/Divider';
import CheckBoxIcon from '@mui/icons-material/CheckBox';
import Checkbox from '@mui/material/Checkbox';
import PushPinIcon from '@mui/icons-material/PushPin';
import ListItemText from '@mui/material/ListItemText';
import ListItemIcon from '@mui/material/ListItemIcon';
import ZoomOutIcon from '@mui/icons-material/ZoomOut';
import ZoomInIcon from '@mui/icons-material/ZoomIn';
import CloseIcon from '@mui/icons-material/Close';
import ReplyIcon from '@mui/icons-material/Reply';
import DoneAllIcon from '@mui/icons-material/DoneAll';
import Avatar from '@mui/material/Avatar';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { removeMessageById,updateMessageById,pinMessageById } from "../../../../../../api-data";
import { timeStampMessage, timeStampEU,handleDownload,copied,emojisArr,firstLetter, slicedWord,prodAwsS3 } from '../../../../../../helpers'

const StyledMenu = styled((props:any) => (
  <Menu
    elevation={0}
    anchorOrigin={{
      vertical: 'top',
      horizontal: 'right',
    }}
    transformOrigin={{
      vertical: 'bottom',
      horizontal: 'right',
    }}
    {...props}
  />
))(({ theme }:any) => ({
  '& .MuiPaper-root': {
    borderRadius: 10,
    marginTop: theme.spacing(0),
    minWidth: 220,
    color:
      theme.palette.mode === 'light' ? 'rgb(55, 65, 81)' : theme.palette.grey[500],
    boxShadow:
      'rgb(255, 255, 255) 0px 0px 0px 0px, rgba(0, 0, 0, 0.05) 0px 0px 0px 1px, rgba(0, 0, 0, 0.1) 0px 10px 15px -3px, rgba(0, 0, 0, 0.05) 0px 4px 6px -2px',
    '& .MuiMenu-list': {
       padding: '4px 4px',
    },
    '& .MuiMenuItem-root': {
      marginBottom: theme.spacing(1),
      '& .MuiSvgIcon-root': {
        fontSize: 21,
        color: theme.palette.text.secondary,
        marginRight: theme.spacing(2),
      }
    },
  },
}));

const useStyles = makeStyles({
  container: {
    display: "flex",
    alignItems: 'flex-end',
    alignContent: 'flex-end',
    flexDirection:'column',
    borderRadius: 7,
    position: 'relative',
    padding:'10px 22px 10px 0px'
  },
  wrapper: {
    position: 'relative',
    display: 'flex',
    alignItems: 'start',
    alignContent: 'start',
    flexDirection: 'column',
    width: 400,
    borderRadius: 7,
    padding: 5,
  },
  wrapperInner: {
    display: 'flex',
    alignItems: 'center',
    alignContent: 'center',
  },
  informationWrapper: {
    display: 'flex',
    alignItems: 'center',
    alignContent: 'center',
    justifyContent: 'flex-end',
    zIndex: 2,
    width:'100%',
  },
  time: {
    fontSize: ".65em",
    fontWeight:600,
  }, 
  image: {
    borderRadius: 7,
    width: 348,
    maxHeight: 'auto',
    cursor: 'pointer',
    padding: '10px 0px 10px 0px'
  },   
  overlay: {
    position: 'fixed',
    top: 0,
    left: 0,
    width: '100vw',
    minHeight: '100vh',
    zIndex: 100,
    backgroundColor: 'rgba(104, 105, 104, 0.6)',
    overflow: 'auto',
    boxSizing: 'border-box',
    display: 'flex',
    justifyContent: 'center',
    alignContent: 'center',
    alignItems: 'center'
  },
  topBar: {
    position: 'fixed',
    top: 0,
    left: 0,
    height: '7vh',
    width: '100vw',
    display: 'flex',
    alignContent: 'center',
    alignItems: 'center',
    justifyContent: 'space-between',
    backgroundColor: 'rgba(65, 65, 65, 0.9)',
    zIndex: 10,
    padding: '0px 20px'
  },
  wrapperCredentials: {
    display: 'flex',
    alignContent: 'center',
    alignItems: 'center',
  },    
  wrapperIcons: {
    display: 'flex',
    alignContent: 'center',
    alignItems: 'center',
  },  
  magnifying : {
    marginLeft:5,
    cursor: 'pointer',
    color: '#e9e7e7',
    padding: 0,
    '&:hover': {
      color: '#ffffff',
      transform: 'scale(1.1)'
    }
  }, 
  downloadIcon: {
    marginLeft:5,
    cursor: 'pointer',
    color: '#e9e7e7',
    padding: 0,
    borderRadius: '50%',
    '&:hover': {
      backgroundColor: '#ffffff',
      color: '#b8b7b7',
    }
  },
  iconCloseOverlay: {
    marginLeft:5,
    cursor: 'pointer',
    color: '#e9e7e7',
    padding: 0,
    '&:hover': {
      color: '#ffffff',
      transform: 'rotate(180deg)',
      transition: 'all 250ms ease-out ',
    }
  },
  wrapperImage: {
    borderRadius: 5,
    maxWidth:500,
    maxHeight: 750,
    display: 'flex',
    overflow: 'auto'
  },
  innerImage: {
    objectFit: 'cover',
  },     
  modalDelete: {
    background: '#ffffff',
    position: 'absolute',
    content:'',
    width: '20%',
    height:'auto',
    left: '40%',
    bottom: '48.5%',
    borderRadius: 10,
    padding: 10,
    display: 'flex',
    flexDirection:'column'
  },  
  overlayDelete: {
    position: 'fixed',
    top: 0,
    left: 0,
    width: '100vw',
    height: '100vh',
    zIndex: 100,
    backgroundColor: 'rgba(104, 105, 104, 0.6)',
    overflowY: 'hidden',
  },
  emojiTitle: {
      position: "absolute",
      fontSize: "1.7em",
      fontWeight:600,
      bottom: '0.2rem',
      left: -40,
  },
  emojiCompanionTitle: {
      position: "absolute",
      fontSize: "1.7em",
      fontWeight:600,
      bottom: '2.2rem',
      left: -40,
  },   
  emoji: {
		cursor: 'pointer',
		fontSize: '1.7rem',
    transition: 'all 0.3s',
    '&:hover': {
      transform: 'scale(1.5)'
    }
  },
  emojiActive: {
    cursor: 'pointer',
    fontSize: '1.2rem',
    animation: `$emoji 0.6s ease-out`,
		animationDirection: 'forwards',
		animationIterationCount: 1,
  },  
  '@keyframes emoji': {
	  '5%': { transform: 'translateY(1rem)'},
	  '10%': { transform: 'translateY(0) scale(1)',opacity: 1},
	  '50%': { transform: 'translateY(-4rem) scale(1.5) rotateY(90deg)'},
	  '80%': {opacity: 0},
	  '100%': {transform: 'translateY(-8rem) scale(2) rotateY(180deg)',opacity: 0},
  },
  iconClose: {
    '&:hover': {
      transform: 'rotate(180deg)',
      transition: 'all 250ms ease-out ',
    }
  },
  checkboxSelect: {
    position: 'absolute',
    right: -64,
    top: -10,
    pointerEvents: 'auto'
  },
  folderIcon: {
    marginRight: 7,
    color: '#54b0fc',
    cursor: 'pointer',
    '&:hover': {
      color: '#016cc3'
    },
  },
 avatarIcon: {
    position: 'absolute',
    right: -54,
    bottom: 0,
  },
 tongueOne: {
  content: "''",
  position: "absolute",
  width: "0",
  height: "0",
  borderRight: "15px solid transparent",
  borderLeft: "15px solid transparent",
  bottom: '0px',
  right: "-15px",
  zIndex:1
},
tongueTwo: {
  content: "''",
  position: "absolute",
  width: "0",
  height: "0",
  borderRight: "16px solid transparent",
  borderLeft: "16px solid transparent",
  bottom: "0px",
  right: "-17px",
  zIndex:1
 },    
});

const label = { inputProps: { 'aria-label': 'Checkbox demo' } };

interface IMessageRightImage {
  url: string,
  tongue: boolean,
  watched: boolean,
  avatarUrl: string,
  color: string,
  createdAt:string,
  fullType: string,
  caption: string,
  emoji: string,
  emojiCompanion: string,
  pinned: boolean,
  isSomeSelected: boolean,
  isSelected:(_id:string) => boolean,
  handleSelected: (_id:string) => void,  
  _id: string,
  name: string,
  lastName: string,
  nightMode: boolean,
  handleReply: (_id: string) => void,
  handleForward: (_id: string) => void,
}

const MessageRightImage = ({url,tongue,watched,avatarUrl,color,createdAt,fullType,caption,emoji,emojiCompanion,pinned,isSomeSelected,isSelected,handleSelected,_id,name,lastName,nightMode,handleReply,handleForward}:IMessageRightImage) => {
  const classes = useStyles();
  const [watch, setWatch] = useState<boolean>(false)
  const [anchorEl, setAnchorEl] = useState<any>(null);
  const [selected, setSelected] = useState<boolean>(false);
  const [modal, setModal] = useState<boolean>(false)
  const [scale, setScale] = useState<number>(1)
  const refView = useRef<null | any>(null)
  const open = Boolean(anchorEl);
  const checked = isSelected(_id)
  const handleIncreaseScale = () => {
    if (scale < 5) {
      refView.current.style.transform = `scale(${scale+0.25},${scale+0.25})`
      refView.current.style.transformOrigin = `${50 / scale + 0.25}px ${50 / scale + 0.25}px`
      setScale(scale+0.25)
    }
  }
  const handleDecreaseScale = () => {
    if (scale >= 0.5) {
      refView.current.style.transform = `scale(${scale-0.25},${scale-0.25})`
      refView.current.style.transformOrigin = `${50/scale-0.25}px ${50/scale-0.25}px`
      setScale(scale-0.25)
    }
  } 
  const handleOpenWatch = () => !watch&&setWatch(true)
  const handleCloseWatch = (e:any) => {
    const id = e.target.id
    if (id === 'overlay' || id === 'close') {
      setWatch(false)
      refView.current.style.transform = 'scale(1)'
      setScale(1)
    }
  }  
  const handleClose = (type: string | undefined): void => {
    if (type === 'copy') copied('Link')
    if (type === 'delete') setModal(true)
    setAnchorEl(null)
    setSelected(false)
  }

  const handleDeleteModal = (e: any) => {
    const id = e.target.id
    if (id === 'overlay' || id === 'cancel') return setModal(false)
    if (id === 'delete') {
      removeMessageById(_id)
      setModal(false)
    }
  }  
  const handleContextMenu = (e: React.MouseEvent<HTMLDivElement>):void => {
    e.preventDefault()
    setAnchorEl(e.currentTarget)
    setSelected(true)
  }
  
  const handleEmojiMenu = ({ target }: any): void => {
    const idEmoji = target.id
    if (idEmoji === emoji) {updateMessageById(_id,'')  
    } else updateMessageById(_id,idEmoji)
  } 

  return (watch ?
    <div onClick={handleCloseWatch} id='overlay' className={classes.overlay}>
      <div className={classes.topBar}>
        <div className={classes.wrapperCredentials}>
          <ListItemIcon >
            <Avatar alt={name} src={url?url:undefined}
              sx={{ background: color, width: 44, height: 44 }}>
              {!url&&`${firstLetter(name)}${firstLetter(lastName)}`}
            </Avatar>
          </ListItemIcon> 
          <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
            ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
            primaryTypographyProps={{ color: '#ffffff' }}
            secondary={timeStampEU(createdAt)} secondaryTypographyProps={{ color: '#ffffff' }} />
        </div>
        <div className={classes.wrapperIcons}>
          <ZoomOutIcon onClick={handleDecreaseScale}
            className={classes.magnifying} fontSize='large' />
          <ZoomInIcon onClick={handleIncreaseScale}
            className={classes.magnifying} fontSize='large' />
          <DownloadForOfflineIcon onClick={() => handleDownload(url, fullType)}
            className={classes.downloadIcon} fontSize='large' />
          <CloseIcon id='close' onClick={handleCloseWatch}
            className={classes.iconCloseOverlay} fontSize='large' />
        </div>        
      </div>
      <div ref={refView} className={classes.wrapperImage}>
        <img className={classes.innerImage}
          width='100%' height='auto' alt='imageItem' src={url} />
      </div>
    </div>  :
    <div className={classes.container}>
      <div onContextMenu={(e) => handleContextMenu(e)} className={classes.wrapper}
       style={{backgroundColor:selected?'#ced8d7':'#deffa9',pointerEvents:isSomeSelected?'none':'auto'}}>
        <Typography style={{color: "#26afee"}} variant="h6" align="right">
          {`${firstLetter(name)}${slicedWord(name, 15, 1)} 
          ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
        </Typography>
        <div className={classes.wrapperInner}>
          <ImageIcon onClick={() => handleDownload(url, fullType)}
            className={classes.folderIcon} fontSize='large' />
          <img onClick={handleOpenWatch} className={classes.image} alt='message pic' src={url}
            style={{ backgroundColor: url ? '' : color }} width='300' height='400' />
        </div>
        <ListItemText style={{wordBreak:'break-word'}}  secondary={caption} secondaryTypographyProps={{color: "#000000"}}/>
        <div className={classes.informationWrapper}>
           <div className={classes.time} style={{ color: watched?'#18bd03':'#414141'}}>{timeStampMessage(createdAt)}</div>
           {watched&&<DoneAllIcon style={{ color: '#18bd03',marginLeft:5}} fontSize='small' />}
        </div>
        {tongue&&<div className={classes.avatarIcon}>
          <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
              sx={{ background: color, width: 40, height: 40 }}>
              {!avatarUrl&&`${firstLetter(name)}${firstLetter(lastName)}`}
          </Avatar>
        </div>}
        {tongue&&<span className={classes.tongueOne} style={{borderBottom: `15px solid ${selected?'#ced8d7':'#deffa9'}`}}></span>}
        {tongue&&<span className={classes.tongueTwo} style={{borderBottom: `17px solid ${selected?'#ced8d7':'#deffa9'}`}}></span>}
        {emojiCompanion && <div className={classes.emojiCompanionTitle}>{emojisArr[Number(emojiCompanion)]}</div>}
        {emoji && <div className={classes.emojiTitle}>{emojisArr[Number(emoji)]}</div>}
        {isSomeSelected && <div className={classes.checkboxSelect}><Checkbox {...label} checked={checked} sx={{ color: nightMode ? '#ffffff' : '#00ff48', '&.Mui-checked': { color: nightMode ? '#ffffff' : '#00ff48' } }}
        onClick={() => handleSelected(_id)}/></div>}
      <StyledMenu id="demo-positioned-menu" aria-labelledby="demo-positioned-button"
          anchorEl={anchorEl} open={open} onClose={handleClose}>
          <MenuItem onClick={handleEmojiMenu} style={{ cursor: 'none' }} >
            {emojisArr.map((el:string, i:number) =>
              <div key={el} className={emoji === String(i)?classes.emojiActive:classes.emoji} id={String(i)}>{el}</div>)}
          </MenuItem>
        <Divider />
        <MenuItem onClick={() => {
          handleReply(_id)
          handleClose(undefined)
        }}>
          <ReplyIcon />
          Reply
        </MenuItem>
        <MenuItem onClick={() => {
            handleForward(_id)
            handleClose(undefined)
        }}>
            <ReplyIcon style={{transform :'rotateY(180deg)'}} />
            Forward
        </MenuItem>          
        <CopyToClipboard onCopy={() => handleClose('copy')} text={url}>
          <MenuItem>
            <ContentCopyIcon />
             Copy Link
          </MenuItem>
        </CopyToClipboard>
        <MenuItem onClick={() => {
          pinMessageById(_id, !pinned)
          handleClose(undefined)
        }}>
            {pinned ?
              <CloseIcon className={classes.iconClose} /> :
              <PushPinIcon />}
             {pinned?'Unpin':'Pin'}
        </MenuItem>           
        <MenuItem onClick={() => {
            handleSelected(_id)
            handleClose(undefined)
          }}>
            <CheckBoxIcon />
            Select
        </MenuItem>
        <MenuItem style={{color:'#f02a2a'}} onClick={() => handleClose('delete')}>
            <DeleteOutlineIcon style={{color:'#f02a2a'}}/>
            Delete
        </MenuItem>        
      </StyledMenu>
      {modal &&
      <div onClick={handleDeleteModal} className={classes.overlayDelete} id='overlay'>
        <div className={classes.modalDelete}>
          <h3 style={{color: '#2c2c2c'}}>Delete message</h3>
          <p style={{ color: '#050505' }}>Are you sure you want to delete message?</p>
          <Button id='delete' variant="text" color="error" style={{fontWeight:500,fontSize:22}}>
            DELETE MESSAGE
          </Button>         
          <Button id='cancel' variant="text" style={{fontWeight:500,fontSize:22}}>
             CANCEL
          </Button>
        </div>  
      </div>}         
      </div>
    </div>   
)};

export  default  MessageRightImage
