import { useEffect, useState } from 'react';
import { Activite, Admin, Athlete, Coach, Session, User } from '../../classes';
import { dateToString, hoursToString } from '../edt';
import '../style/objectList.css';
import { Modal } from '../Modal';
import Loading from '../loading';
import {delay, getSessionsOfUserAPI} from "../../requetes";
import CreateActivite from '../createActivite';
import { useLocalData } from '../../context/useLocalData';
import ObjectSession from './session';
import StatAthlete from '../StatsAthlete';

type Props = {
    user:User;

}

function ObjectUser({user}:Props){

    //const {user,setUser} = useLocalData());
    const [open, setOpen] = useState<boolean>(false);
    const [open2, setOpen2] = useState<boolean>(false);
    const [loading,setLoading] = useState<boolean>(false);

    // Initialisation sécurisée des sessions
    function getInitialSessions(): Session[] {
        if (user instanceof Athlete || user instanceof Coach) return [...(user.sessions || [])];
        return [];
    }

    const [sessions, setSessions] = useState<Session[]>(getInitialSessions());




    function handleOpen(): void {
        setOpen(!open);
    }

    function handleDeleteSession(session:Session): void {
        if(user instanceof Coach || user instanceof Athlete){
            user.sessions.splice(user.sessions.indexOf(session), 1);
            setSessions([...user.sessions])
        }

    }

    function handleAddSession(): void {
        if(user instanceof Athlete){
            setOpen2(true)
        }
    }

     async function updateSession(){
        if(user instanceof Athlete || user instanceof Coach){
            user.sessions = await getSessionsOfUserAPI(user);
            setLoading(false);
        }
    }


    useEffect(() => {
        if(open){
            setLoading(true);
            updateSession()
        }
    },[open])

    useEffect(() => {
        if(!loading){
            if(user instanceof Athlete){
                setSessions([...user.sessions])
            }
            if(user instanceof Coach){
                setSessions([...user.sessions])
            }
        }
    },[loading])


    function returnSession(session: Session|null){
        if(session!==null){
            if(user instanceof Athlete){
                user.sessions.push(session);
                setSessions([...user.sessions])
            }
            if(user instanceof Coach){
                user.sessions.push(session);
                setSessions([...user.sessions])
            }
        }
        setOpen2(false);
    }

    return(
        <div>
            <div className="object" onClick={() => handleOpen()}>
                    <div>{user.prenom} {user.nom}</div>
                    
                    {/* <div>{user2.role}</div> */}
            </div>
            {open && 
                <Modal isOpen={open} onClose={() => setOpen(false)}>
                    <div className="object_modal">
                        <div>{user.prenom}</div>
                        <div>{user.nom}</div>
                        {(user instanceof Athlete || user instanceof Coach) &&
                            <div className='padding'>
                                <div className='list_object_modal'>
                                    <div>Sessions :</div>
                                    {user.sessions.map((session,index)=>(
                                        <ObjectSession session={session}></ObjectSession>
                                    ))}
                                </div>
                            </div>
                        }
                            
                        {user instanceof Athlete && (
                            <div className="stats-container">
                                <StatAthlete athlete={user}/>
                            </div>
                        )}
                    </div>
                </Modal>
            }  
        </div>      
    )
}

export default ObjectUser
