import { useState, useEffect } from 'react';
import progressService from '../services/serviceProgress';

const useProgress = () => {
    const [students, setStudents] = useState([]);
    const [classes, setClasses] = useState([]);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);

    const [searchStudent, setSearchStudent] = useState("");
    const [sortStudent, setSortStudent] = useState("");
    const [pageStudent, setCurrentPageStudent] = useState(1);
    const [limitStudent, setLimitStudent] = useState(20);
    const [totalPagesStudent, setTotalPagesStudent] = useState(0);
    const [totalDataStudent, setTotalDataStudent] = useState(null);

    const [searchClass, setSearchClass] = useState("");
    const [sortClass, setSortClass] = useState("");
    const [pageClass, setCurrentPageClass] = useState(1);
    const [limitClass, setLimitClass] = useState(1000);
    const [totalPagesClass, setTotalPagesClass] = useState(0);
    const [totalDataClass, setTotalDataClass] = useState(null);

    const [selectedId, setSelectedId] = useState('');
    const [selected, setSelected] = useState([]);
    const [show, setShow] = useState(false);
    const [loadingModal, setLoadingModal] = useState(true);
    
    const fetchDataStudents = async () => {
        setLoading(true);
        try {
            const student = await progressService.fetchDataStudent(searchStudent, sortStudent, pageStudent, limitStudent);
            setStudents(student.payload.monitorings);
            setTotalPagesStudent(student.payload.totalPages);
            setTotalDataStudent(student.payload.totalItems);
        } catch (err) {
            setError(err);
        } finally {
            setLoading(false);
        }
    };

    const fetchDataClasses = async () => {
        setLoading(true);
        try {
            const classes = await progressService.fetchDataClass(searchClass, sortClass, pageClass, limitClass);
            setClasses(classes.payload.classes);
            setTotalDataClass(classes.payload.totalItems);
        } catch (err) {
            setError(err);
        } finally {
            setLoading(false);
        }
    };

    const handleSearchChangeStudents = () => {
        fetchDataStudents();
        setCurrentPageStudent(1);
    }

    const handlePageChangeStudents = (pages) => {
        setCurrentPageStudent(pages);
    }

    const handleLimitsChangeStudents = (e) => {
        setLimitStudent(e.target.value);
        setCurrentPageStudent(1);
    }

    const handleSearchChangeClasses = () => {
        fetchDataClasses();
        setCurrentPageClass(1);
    }

    const handlePageChangeClasses = (pages) => {
        setCurrentPageClass(pages);
    }

    const handleLimitsChangeClasses = (e) => {
        setLimitClass(e.target.value);
        setCurrentPageClass(1);
    }

    useEffect(() => {
        fetchDataStudents();
    }, [pageStudent, limitStudent]);

    useEffect(() => {
        fetchDataClasses();
    }, [pageClass, limitClass]);

    const getClassTopic = async (id) => {
        setLoadingModal(true);
        try {
            const classes = await progressService.getTopicByClass(id);
            setSelected(classes.payload.data);
        } catch (err) {
            console.log(err);
        }finally{
            setLoadingModal(false);
        }
    };
    
    const handleShow = (data) => {
        setSelectedId(data);
        getClassTopic(data);
        setShow(true);
    };
    const handleClose = () => {setShow(false); setSelected([])};

    return { 
        students, 
        classes,
        loading, 
        error, 

        totalPagesStudent,
        totalDataStudent,
        setSearchStudent,
        pageStudent, 
        handlePageChangeStudents,
        handleLimitsChangeStudents,
        handleSearchChangeStudents,

        totalPagesClass,
        totalDataClass,
        setSearchClass,
        pageClass, 
        handlePageChangeClasses,
        handleLimitsChangeClasses,
        handleSearchChangeClasses,

        selectedId,
        selected,
        show,
        loadingModal,
        handleShow,
        handleClose,
    };
};

export default useProgress;
