/*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, {Component} from "react";
import GoogleLogin from 'react-google-login'
import { Button } from '@material-ui/core';
//import FacebookLogin from 'react-facebook-login';
import CloseIcon from '@material-ui/icons/Close';
import styled from 'styled-components'
import {device} from './device.js'
import LabeledCheckbox from './Checkbox.js'
import FormInput from "./FormInput.js"
import axios from 'axios'
import {apiUrl} from '../env';
//arrumar isso
const responseGoogle = (response) => {
  console.log(response);
  axios.post((`${apiUrl}/auth/google_oauth2`), response).then(
      (response) => {console.log(response.data)},
      (error) => {console.log(error)}
  )
}

class LoginContainer extends Component {
    constructor (props) {
        super(props);

        this.state = {
            email : "",
            senha : "",
            checkboxChecked : true
        };
        this.handleChecked = this.handleChecked.bind(this)
    }

    switchModal = (e) => {
        e.preventDefault()
        this.props.handleClose()
        this.props.openSignUp()
    }

    handleChange = e => {
        this.setState({[e.target.name]: e.target.value})
    }

    onSubmit = (e) => {
        e.preventDefault();

        this.props.handleLoginInfo(this.state);

        this.setState({
            email: "",
            senha: ""
        })
    }

    handleChecked = (e) => {
        this.setState({
            checkboxChecked : !this.state.checkboxChecked
        })
    }

    render () {
        return (
            <ContainerStyled >
                <DialogHeaderStyled>
                    <H2Styled> Entrar
                        <StyledCloseModalButton onClick={this.props.handleClose}>
                        <CloseIcon/>
                        </StyledCloseModalButton>
                    </H2Styled>
                </DialogHeaderStyled>

                <div style={{paddingTop: "20px"}}>
                    <div style={{marginTop:"0"}}>
                        <StyledGoogleLoginButton
                            clientId="288460085642-k4veg4fo8kddvjer8b055n9da5qtgha7.apps.googleusercontent.com"
                            onSuccess={responseGoogle}
                            onFailure={responseGoogle}
                            cookiePolicy={'single_host_origin'}
                        >
                            <span style={{textTransform:"none", fontSize:"13px"}}>Usando o Google</span>
                        </StyledGoogleLoginButton>
                    </div>
                </div>

                <H3Styled>
                    <RightSideStrikedH3/>
                    <span style={{verticalAlign:"middle"}}>ou</span>
                    <LeftSideStrikedH3/>
                </H3Styled>

                <form ref="form" onSubmit={this.onSubmit}>
                    <FormInput
                        inputType={"text"}
                        name={"email"}
                        value={this.state.email}
                        placeholder={"E-mail *"}
                        handleChange={e => this.handleChange(e)}
                    />
                    <br/>
                    <FormInput
                        inputType={"password"}
                        name={"senha"}
                        value={this.state.senha}
                        placeholder={"Senha *"}
                        handleChange={e => this.handleChange(e)}
                    />
                    <br/>

                    <RememberRecover>
                        <LabeledCheckbox label={<StyledSpan>Lembrar-me</StyledSpan>} onchange={this.handleChecked}/>
                        <span style={{paddingTop:"15px"}}>Esqueceu a senha?<a href="recuperar-senha" style={{textAlign: "right", color:"#4cd0e1"}}>Clique aqui!</a></span>
                    </RememberRecover>

                    <ConfirmContainerStyled>
                        <StyledLoginButton onClick={e => this.onSubmit(e)} variant="contained">
                            <span style={{borderRadius:"3px", boxSizing:"border-box", fontFamily:"Roboto, sans serif", fontWeight:"500", color:"#fff"}}>ENTRAR</span>
                        </StyledLoginButton>
                    </ConfirmContainerStyled>
                </form>

                <DialogFooterStyled>
                    <span style={{textAlign:"center", fontSize: "14px", color:"rgb(102, 102, 102)"}}>Ainda não tem cadastro? <StyledAnchor href="" onClick={e => this.switchModal(e)}>CADASTRE-SE</StyledAnchor></span>
                </DialogFooterStyled>
            </ContainerStyled>
        )
    }
}

export default LoginContainer

const ContainerStyled = styled.div`
    box-sizing : border-box;
    background-color : white;
    max-width : none;
    align : center;
    padding-left : 25px;
    padding-right:25px;
    padding-bottom:10px;
    border-radius: 4px;
    @media ${device.mobileM} {
        width : 100%;
        height : 100%;
    }
`

const DialogHeaderStyled = styled.div`
    text-align : center;
    display : inline-flex;
    justify-content : space-between;
    padding : 10px 26px 0 26px;
    height : 64px;
`

const DialogFooterStyled = styled.div`
    box-sizing : border-box;
    font-family : 'Roboto', sans serif;
    margin : 20px -20px;
    padding-top : 20px;
    border-top : 1px #e5e5e5 solid;
    justify-content : center;
    text-align : center;
    line-height : 1.42857143
`

const H2Styled = styled.h2`
    align-self : center;
    color : #666;
    font-size : 26px;
    font-weight : 100;
    font-family: 'Roboto', sans serif, 'Helvetica Neue', Helvetica, Arial, sans-serif !important;
    justify-content: space-between;
    padding: 10px 26px 0 26px;
    text-align: center;
    letter-spacing: .005em;
`

const RightSideStrikedH3 = styled.div`
    display :  inline-block;
    border-bottom: 1px dotted #666;
    vertical-align :  middle;
    font-weight : 500;
    margin-right : 5px;
    width : 45%;
`

const LeftSideStrikedH3 = styled.div`
    display :  inline-block;
    border-bottom: 1px dotted #666;
    vertical-align :  middle;
    font-weight : 500;
    margin-left : 5px;
    width : 45%;
`
const StyledGoogleLoginButton = styled(GoogleLogin)`
    background-color: fff;
    border : 1px solid rgb(66, 133, 244);
    box-shadow: 0 0 0 1px #4285f4 !important;
    :hover {
        background-color: #f4f4f4 !important;
    }
`

const H3Styled = styled.h3`
    overflow : hidden;
    text-align : center;
    font-size : 14px;
    color : #666;
    margin : 10px 0;
`

const StyledAnchor = styled.a`
    color : #00bcd4;
    text-decoration : none;
`
const StyledCloseModalButton = styled(Button)`
    display : inline-block;
    position : relative;
    float : right !important;
    margin-right : -8px;
    background : transparent;
    min-width: 0 !important;
    width : 40px;
`
const ConfirmContainerStyled = styled.div`
    display :  flex;
    margin-top : 10px;
    align-items : center;
    justify-content : center;
    box-sizing : border-box;
`
const StyledLoginButton = styled(Button)`
    background-color: #00bcd4 !important;
    box-shadow : none !important;
    outline: none !important;
    border : 0 !important;
    overflow : hidden !important;
    width : 35% !important;
    display : inline-block !important;
    font-family : 'Roboto', sans serif !important;
    font-size: 14px !important;
    height : 36px !important;
    align-items : center !important;
    border-radius: 3px !important;
    align-self : 50% !important;
    :hover {
        background-color : #00acc1 !important;
    }
`

const RememberRecover = styled.div`
    display : flex;
    justify-content : space-between;
    font-size: 12px;
    font-weight : 400;
    margin-bottom: 16px;
`
const StyledSpan = styled.span`
    font-size: 12px;
    font-weight: 400;
    padding-top: 2px;
`