import { useState } from "react";
import { useLocalData } from "../context/useLocalData";
import { AthleteList, ActiviteList, CoachList} from "./ressourceList";
import { Activite, Athlete, Coach } from "../classes";

export default function RessourcePanel() {
    const { user } = useLocalData();
    const [showAthletes, setShowAthletes] = useState(false);
    const [showActivites, setShowActivites] = useState(false);
    const [showCoach,setShowCoach] = useState(false);
    console.log("Rôle utilisateur:", user.role);
    if (user.role === "Athlete") return null;


    const athleteMap: Map<number, Athlete> = new Map();
    user.sessions.forEach(session => {
        session.athletes?.forEach(a => athleteMap.set(a.id, a));
    });
    const allAthletes: Athlete[] = Array.from(athleteMap.values());

    const activiteMap: Map<number, Activite> = new Map();
    user.sessions.forEach(session => {
        session.activites?.forEach(act => activiteMap.set(act.id, act));
    });
    const allActivites: Activite[] = Array.from(activiteMap.values());

    const coachMap: Map<number, Coach> = new Map();
    user.sessions.forEach(session => {
        if (session.coach) {
            coachMap.set(session.coach.id, session.coach);
        }
    });

    const allCoachs: Coach[] = Array.from(coachMap.values());

    function onAthletesClick(): void {
        setShowAthletes(prev => !prev);
        setShowActivites(false); 
        setShowCoach(false);
    }
    function onActivitiesClick(): void {
        setShowActivites(prev => !prev);
        setShowAthletes(false);
        setShowCoach(false);

    }
    function onCoachClick(): void {
        setShowCoach(prev => !prev);
        setShowActivites(false); 
        setShowAthletes(false);

    }


    return (
        <div className="ressource_panel">
            <select
                onChange={(e) => {
                    const v = (e.target as HTMLSelectElement).value;
                    if (v === "athletes") onAthletesClick();
                    else if (v === "activites") onActivitiesClick();
                    else if (v === "coach") onCoachClick();
                    else {setShowAthletes(false); setShowActivites(false); setShowCoach(false)}
                }}>
                <option>Choissisez la ressource</option>
                <option value="athletes">Athlètes</option>
                <option value="activites">Activités</option>
                {user.role === "Admin" && <option value="coach"> Coach</option>}
            </select>

            {showAthletes && (
                <div className="edt_athletes_panel">
                    <h3>Liste des athlètes</h3>
                    <AthleteList athletes={allAthletes} />
                </div>
            )}

            {showActivites && (
                <div className="edt_activites_panel">
                    <h3>Liste des activités</h3>
                    <ActiviteList activites={allActivites} />
                </div>
            )}

            {showCoach && (
                <div className="edt_coach_panel">
                    <h3>Liste des coachs</h3>
                    <CoachList coachs={allCoachs} />
                </div>
            )}



        </div>
    );
}
