/*Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana

This file is part of Plataforma Integrada MEC.

Plataforma Integrada MEC is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Plataforma Integrada MEC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Plataforma Integrada MEC.  If not, see <http://www.gnu.org/licenses/>.*/

import React, {useState} from 'react';
import styled from 'styled-components'
import Button from '@material-ui/core/Button';
import Menu from '@material-ui/core/Menu';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import MenuItem from '@material-ui/core/MenuItem';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import OpenIcon from '@material-ui/icons/OpenInNew';
import ReportIcon from '@material-ui/icons/Error';
import PersonAddIcon from '@material-ui/icons/PersonAdd';
import {putRequest} from './HelperFunctions/getAxiosConfig'
import ReportModal from './ReportModal.js'
import {Link} from 'react-router-dom'

export default function SimpleMenu(props) {
    // const {state} = useContext(Store)
    const [anchorEl, setAnchorEl] = React.useState(null);

    function handleClick(event) {
        setAnchorEl(event.currentTarget);
    }

    function handleClose() {
        setAnchorEl(null);
    }
    const handleFollow = (followerID) => {

        putRequest(`/users/${followerID}/follow`, {}, (data) => {console.log(data)}, (error) => {console.log(error)})
    }

    const [reportModal, toggleReportModal] = useState(false)
    const handleModal = (value) => {
        toggleReportModal(value)
    }

  return (
      <>
      {
          reportModal &&
          <ReportModal open={reportModal} handleClose={() => handleModal(false)}
                  form="user" complainableId={props.followableID}
                  complainableType={"User"}
                  {...props}/>
      }
     <div style={{fontSize: "12px", display : "flex", flexDirection : "column", justifyContent : "center"}}>
          <ButtonNoWidth aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick} style={{color : "#666"}}>
            <MoreVertIcon style={{color : "#666"}}/>
          </ButtonNoWidth>
          <Menu
            id="simple-menu"
            anchorEl={anchorEl}
            keepMounted
            open={Boolean(anchorEl)}
            onClose={handleClose}
          >
            <StyledMenuItem onClick={handleClose}>
                <Link to={"/usuario-publico/" + props.followableID}>
                    <ListItemIcon><OpenIcon /></ListItemIcon>Abrir
                </Link>
            </StyledMenuItem>

            {
                props.followed ?
                (
                    <StyledMenuItem onClick={() => {handleFollow(props.followableID);handleClose()}}>
                        <ListItemIcon><ReportIcon /></ListItemIcon>Deixar de Seguir
                    </StyledMenuItem>
                )
                :
                (
                    <StyledMenuItem onClick={() => {handleFollow(props.followableID);handleClose()}}>
                        <ListItemIcon><PersonAddIcon /></ListItemIcon>Seguir
                    </StyledMenuItem>
                )
            }


            <StyledMenuItem onClick={() => {handleModal(true); handleClose()}}>
                <ListItemIcon><ReportIcon /></ListItemIcon>Reportar
            </StyledMenuItem>
          </Menu>
        </div>
    </>
  );
}

const ButtonNoWidth = styled(Button)`
    width : 24px !important;
    min-width : 24px !important;
    max-height : 24px !important;
    padding : 0 !important;
    background-color : #fff !important;
    color : #a5a5a5 !important;
    border : 0 !important;

    .MuiButton-root {
        width : 24px !important;
        min-width : 12px !important;
    }

    .MuiSvgIcon-root {
        padding-right : 0 !important;
        vertical-align : middle;
    }

    .MuiButton-label {
        padding-left : 4px !important;
    }
    `

const StyledMenuItem = styled(MenuItem)`
    color : #666 !important;
    .MuiSvgIcon-root {
        vertical-align : middle !important;
    }
    a {
        text-decoration : none !important;
        color : #666 !important;
    }
`