/*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 { 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 {Store} from '../Store.js'
import axios from 'axios'
import {apiDomain, apiUrl} from '../env';
import Grid from '@material-ui/core/Grid';
import RadioGroup from '@material-ui/core/RadioGroup';
import Radio from '@material-ui/core/Radio';
import FormControl from '@material-ui/core/FormControl';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import CloseModalButton from './CloseModalButton'
import {getAxiosConfig} from './HelperFunctions/getAxiosConfig'

export default function ModalConfirmarCuradoriaOpen (props) {

    const handleCancel = () => {
        props.handleClose()
        props.cancel()
    }

    const transformReportCriteria = (criteria) => {
        let newArr = []
        criteria.map((criterium) =>
            newArr.push({"question_id" : criterium.id, "accepted" : criterium.accepted})
        )
        return newArr
    }

    const handleConfirmation = () => {
        let config = getAxiosConfig()

        let payload = {
            "submission" : {
                "justification" : props.justificativa,
                "answers" : transformReportCriteria(props.reportCriteria)
            }
        }

        axios.post( (`${apiUrl}/submissions/` + props.recursoId + '/answer'), payload, config).then(
            (response) => {
                if ( response.headers['access-token'] ) {
                    sessionStorage.setItem('@portalmec/accessToken', response.headers['access-token'])
                }
                console.log(response.data)
                props.finalizeCuratorshipFlow()
            }, (error) => {console.log(error)}
        )
    }

    return (
        <StyledModal
            aria-labelledby="transition-modal-title"
            aria-describedby="transition-modal-description"
            open={props.open}
            centered="true"
            onClose={props.handleClose}
            closeAfterTransition
            BackdropComponent={Backdrop}
            BackdropProps={{
                timeout: 500,
            }}
        >
            <Fade in={props.open}>
                <Container recusado={!props.aceito}>
                    <Header>
                        <span style={{width:"32px"}}/>
                        <h2>Recurso a ser {props.aceito ? 'aprovado' : 'recusado'}</h2>
                        <CloseModalButton handleClose={props.handleClose}/>
                    </Header>
                    <Content>
                        {
                            props.aceito ?
                            (
                                <p>Este recurso será publicado na plataforma. Você confirma essa avaliação?
                                </p>
                            )
                            :
                            (
                                <>
                                <p>Agradecemos a sua contribuição. Você avaliou que o recurso não está em conformidade com o(s) seguinte(s) critério(s):
                                </p>
                                {
                                    props.reportCriteria.filter((criterium) => criterium.accepted === false).map( (criterium) =>
                                        <p key={criterium.id} className="reason-offensive">{criterium.text}</p>
                                    )
                                }
                                <p>Você confirma essa avaliação? Ao confirmar, o recurso não será publicado na plataforma.</p>
                                </>
                            )
                        }
                            <ButtonsDiv>
                                {
                                    props.aceito ?
                                    (
                                        <ButtonEnviarAvaliar onClick={() => {handleConfirmation()}}>SIM, CONFIRMAR</ButtonEnviarAvaliar>
                                    )
                                    :
                                    (
                                        <ButtonEnviarAvaliar onClick={() => {handleConfirmation()}}>SIM, CONFIRMAR</ButtonEnviarAvaliar>
                                    )
                                }
                                <GreyButton onClick={handleCancel}>NÃO, ALTERAR AVALIAÇÃO</GreyButton>
                            </ButtonsDiv>
                    </Content>
                </Container>
            </Fade>
        </StyledModal>
    )
}


const Content = styled.div`
    padding : 30px;
    overflow : visible;
    max-width : 100%;
    color : #666;
    font-size : 16px;
    text-align : start;
    .reason-offensive {
        font-weight : 700;
    }

    p {
        margin : 0 0 10px;
    }
`

const Header = styled.div`
    display : flex;
    flex-direction : row;
    align-items : center;
    max-height : none;
    justify-content : space-between;
    color : #666;

    h2 {
        font-size : 30px;
        margin-top : 20px;
        margin-bottom : 10px;
        font-weight : lighter;
    }
`

const StyledCloseModalButton = styled(Button)`
    display : inline-block;
    position : relative;
    float : right !important;
    margin-right : 4px !important;
    background : transparent !important;
    min-width: 0 !important;
    width : 40px;
`

const StyledModal = styled(Modal)`
    .djXaxP{
        margin : 0 !important;
    }
    display : flex;
    align-items: center;
    justify-content : center;
    text-align : center;
    padding : 10px !important;
    max-width : none;
    max-height : none;
`

const Container = styled.div`
    box-sizing : border-box;
    box-shadow : 0 7px 8px -4px rgba(0,0,0,.2),0 13px 19px 2px rgba(0,0,0,.14),0 5px 24px 4px rgba(0,0,0,.12);
    background-color : #fff;
    align : center;
    display : flex;
    flex-direction : column;
    min-width : 240px;
    position : relative;
    border-radius : 4px;
    max-width : 100%;
    max-height : ${props => props.recusado ? 'none' : '370px'};

    @media screen and (max-width : 699px) {
        overflow : ${props => props.recusado ? 'scroll' : 'visible'};
        width : 100%;
        height : 100%;
    }

    p {
        margin : 0 0 10px;
    }
`
const ButtonsDiv = styled.div`
    display : flex;
    max-width : 100%;
    text-align : center;
    align-items : center;
    justify-content : center;
    padding-top : 20px;

    @media screen and (min-width : 990px) {
        flex-direction : row;
    }
    @media screen and (max-width : 989px) {
        flex-direction : column;
    }
`
const GreyButton = styled(Button)`
    &:hover {
        background-color : rgba(158,158,158,0.2) !important;
    }
    max-height : 36px !important;

    background-color : transparent !important;
    color : #666 !important;
    text-decoration : none !important;
    outline : none !important;
    text-align : center !important;
    font-weight : 600 !important;
    .MuiButton-label {
        padding-left : 16px !important;
        padding-right : 16px !important;
    }
    @media screen and (max-width : 989px) {
        margin-top : 10px !important;
    }
`

const ButtonEnviarAvaliar = styled(Button)`
    color : rgba(255,255,255,0.87) !important;
    box-shadow : 0 2px 5px 0 rgba(0,0,0,.26) !important;
    font-weight : 600 !important;
    background-color : #ff7f00 !important;
    margin-left : 8px !important;
    margin-right : 8px !important;

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