import { useState } from "react";
import { useLocalData } from "../context/useLocalData";
import { AthleteList, ActiviteList, CoachList, SessionList, LigneList} from "./ressourceList";
import { Activite, Athlete, Coach , Session, Ligne } from "../classes";
import {calculTempsDeJeuParLigne} from "../utils/ligneUtils";
import { keyboard } from "@testing-library/user-event/dist/keyboard";
import { unescapeLeadingUnderscores } from "typescript";


	export type keyWord = "athletes" | "activites" | "coachs" | "sessions"| "lignes";

	export default function RessourcePanel() {
	    const { user } = useLocalData();
		const [value,setValue] = useState<keyWord>("athletes");
	    console.log("Rôle utilisateur:", user.role);
		console.log("name", user.nom);
	    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());

	    const sessionMap: Map<number, Session> = new Map();
	    user.sessions.forEach(session => {
		sessionMap.set(session.id, session);
	    });

	    const allSessions: Session[] = Array.from(sessionMap.values());

	    const ligneMap: Map<number, Ligne> = new Map();
	    user.sessions.forEach(session => {
		if (session.ligne) {

		    session.ligne.forEach(ligne => {
		        ligneMap.set(ligne.id, ligne);
		    });
		}
	    });

	    
		const allLignes: Ligne[] = Array.from(ligneMap.values());

		// Calculer le temps de jeu pour chaque ligne
		const tempsDeJeuParLigne: Map<number, number> = new Map();
		allLignes.forEach(ligne => {
			const tempsTotal = calculTempsDeJeuParLigne(allSessions, ligne);
			tempsDeJeuParLigne.set(ligne.id, tempsTotal);
			ligne.tempsDeJeu = tempsTotal;
		});



	    return (
		<div className="ressource_panel">
		    <div>
		        Sélectionner une ressource:
		    </div>
		    <select
		        onChange={(e) => {
		            const v = (e.target as HTMLSelectElement).value;
					setValue(v as keyWord)
		        }}>
		        <option value="athletes">Athlètes</option>
		        <option value="activites">Activités</option>
		        {user.role === "Admin" && <option value="coachs"> Coachs</option>}
		        <option value="sessions"> Sessions</option>
		        <option value="lignes"> Lignes</option>

		    </select>

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

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

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

		    {value==="sessions" && (
		        <div className="edt_sessions_panel">
		            <h3>Liste des sessions</h3>
		            <SessionList sessions={allSessions} />
		        </div>
		    )}

		    {value==="lignes" && (
		        <div className="edt_lignes_panel">
		            <h3>Liste des lignes</h3>
		            <LigneList lignes={allLignes} tempsDeJeuParLigne={tempsDeJeuParLigne}/>
		        </div>
		    )}



		</div>
	    );
	}
