/*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, useEffect} from 'react'
import axios from 'axios'
import {apiUrl} from '../../../env';
import LoadingSpinner from '../../LoadingSpinner.js'
import Template from '../PanelComponents/TemplateRecurso.js'
import TemplateCuradoria from '../PanelComponents/TemplateCuradoria.js'

export default function TabPanelAtividades (props) {
    const [loading, handleLoading] = useState(true)

    const [learningObjects, setLearningObjects] = useState([]);
    const [learningObjectsSlice, setLearningObjectsSlice] = useState([])

    const [drafts, setDrafts] = useState([]);
    const [draftsSlice, setDraftsSlice] = useState([])

    const [curating, setCurating] = useState([]);
    const [curatingSlice, setCuratingSlice] = useState([])

    useEffect( () => {
        axios.all([
            axios.get((`${apiUrl}/users/` + props.id + '/learning_objects'), props.config),
            axios.get((`${apiUrl}/users/` + props.id + '/drafts'), props.config),
            axios.get((`${apiUrl}/users/` + props.id + '/submissions?status=submitted'), props.config)
        ])
        .then( (responseArr) => {
                console.log('responseArr Meus recursos: ', responseArr)
                if (responseArr[0].headers['access-token']) {
                    sessionStorage.setItem('@portalmec/accessToken', responseArr[0].headers['access-token'])
                }

                setLearningObjects(responseArr[0].data)
                setLearningObjectsSlice(responseArr[0].data.slice(0, 4))

                setDrafts(responseArr[1].data)
                setDraftsSlice(responseArr[1].data.slice(0, 4))

                setCurating(responseArr[2].data)
                setCuratingSlice(responseArr[2].data.slice(0, 4))

                handleLoading(false)
            },
            (error) => {
                handleLoading(false)
                console.log('error while running axios all')
            }
        )
    }, [])

    const showMoreLearnObj = () => {
        var sliceLength = learningObjectsSlice.length
        setLearningObjectsSlice(learningObjects.slice(0, sliceLength + 4))
    }

    const showAllLearnObj = () => {setLearningObjectsSlice(learningObjects)}

    const showMoreDrafts = () => {
        var sliceLength = draftsSlice.length
        setDraftsSlice(drafts.slice(0, sliceLength + 4))
    }

    const showAllDrafts = () => {setDraftsSlice(drafts)}

    const showMoreCurating = () => {
        var sliceLength = curatingSlice.length
        setCuratingSlice(curating.slice(0, sliceLength + 4))
    }

    const showAllCurating = () => {setCuratingSlice(curating)}

    return (
        <>
            {
                loading ?
                (
                    <LoadingSpinner text={'Carregando Recursos'}/>
                )
                :
                (
                    [
                        <React.Fragment>
                            <Template
                                length = {learningObjects.length}
                                titleText = {learningObjects.length == 1 ? "Recurso Publicado" : "Recursos Publicados"}
                                noContentText={"Você ainda não publicou nenhum Recurso!"}
                                slice={learningObjectsSlice}
                                showMore={showMoreLearnObj}
                                showAll={showAllLearnObj}
                            />

                            <Template
                                length = {drafts.length}
                                titleText = {drafts.length == 1 ? "Rascunho Publicado" : "Rascunhos Publicados"}
                                noContentText={"Você não tem nenhum recurso sendo editado."}
                                slice={draftsSlice}
                                showMore={showMoreDrafts}
                                showAll={showAllDrafts}
                            />

                            <TemplateCuradoria
                                length={curating.length}
                                titleText = {curating.length === 1 ? "Recurso sendo avaliado pela curadoria" : "Recursos sendo avaliados pela curadoria"}
                                noContentText={"Você não tem nenhum recurso sendo avaliado pelos curadores."}
                                sliceArr={curatingSlice}
                                showMore={showMoreCurating}
                                showAll={showAllCurating}
                            />
                        </React.Fragment>
                    ]
                )
            }
        </>
    )
}