/*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, {useContext} from 'react';
import { makeStyles } from '@material-ui/styles';
import { Button } from '@material-ui/core';
import Modal from '@material-ui/core/Modal';
import Backdrop from '@material-ui/core/Backdrop';
import Fade from '@material-ui/core/Fade';
import styled from 'styled-components'
import SignUpContainer from './SignUpContainer.js'
import {Store} from '../Store.js'
import axios from 'axios'
import {apiUrl} from '../env';

const StyledModalSignUp = styled(Modal)`
    .djXaxP{
        margin : 0 !important;
    }

    display : flex;
    align-items: center;
    justify-content : center;
    text-align : center;
    padding : 10px !important;
    border-radius : 4px;

`

const useStyles = makeStyles(theme => ({
  modal: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    textAlign: "center",
    maxBlockSize: "350px",
    width: '100%',
    minBlockSize: "100px",
  },
  paper: {
    backgroundColor: "theme.palette.background.paper",
    border: '2px solid #000',
    boxShadow: " 0px 3px 5px -1px rgba(0,0,0,0.2),0px 5px 8px 0px rgba(0,0,0,0.14),0px 1px 14px 0px rgba(0,0,0,0.12);",
    align:"center",
  },
}));

export default function SignUpModal (props) {
    const { state, dispatch } = useContext(Store)

    const handleLoginInfo = (newLogin) => {

        axios.post(`${apiUrl}/auth`,
            {
            name : newLogin.nomeCompleto,
            email: newLogin.email,
            password : newLogin.senha,
            password_confirmation : newLogin.senha
        }
        ).then( (response) => {
            dispatch ( {
                type: 'USER_SIGNED_UP',
                userLoggedIn: !state.userIsLoggedIn,
                login: {
                    username : newLogin.username,
                    email : newLogin.email,
                    accessToken : response.headers['access-token'],
                    client : response.headers.client
                    }
                }
            )
            props.handleClose()
            }, (error) => {
                console.log(':(')
            }
        )
    }

    //useEffect(()=>{console.log(state.currentUser)},[state.currentUser])

    return (
        <StyledModalSignUp
            aria-labelledby="transition-modal-title"
            aria-describedby="transition-modal-description"
            open={props.open}
            animation={true}
            centered={true}
            onClose={props.handleClose}
            closeAfterTransition
            BackdropComponent={Backdrop}
            BackdropProps={{
                timeout: 500,
            }}
        >
            <Fade in={props.open}>
                <SignUpContainer
                 handleClose={props.handleClose}
                 openLogin={props.openLogin}
                 handleLoginInfo = {handleLoginInfo}
                />
            </Fade>
        </StyledModalSignUp>
    )
}