'use client';

import { useContext } from 'react';

import { UserContext } from '@/context/user';
import Card from '@/ui/Card';

export default function Page() {
  const { data: me, isLoading } = useContext(UserContext);
  return (
    <section className="w-full flex-col space-y-4">
      <header>
        <h1 className="text-xl font-semibold">Tableau de bord</h1>
        <p className="text-highlight-secondary">Ceci est la page d&apos;accueil du dashboard</p>
      </header>
      <main className="flex-col space-y-4">
        <div className="w-full flex-col justify-between space-x-0 space-y-4 md:flex md:flex-row md:space-x-6 md:space-y-0">
          <Card
            isLoading={isLoading}
            icon="pie-chart-line"
            title="Puzzles résolus"
            data={me?.completions ?? 0}
            link="/dashboard/puzzles"
          />
          <Card
            isLoading={isLoading}
            icon="award-line"
            title="Badges obtenus"
            data={me?.badges?.length ?? 'Aucun'}
            link="/dashboard/badges"
          />
          <Card
            isLoading={isLoading}
            icon="bar-chart-line"
            title="Rang actuel"
            data={me?.rank ?? 'Non classé'}
            link="/dashboard/leaderboard"
          />
        </div>
        <div className="grid grid-cols-1 gap-4">
          <div className="flex flex-col space-y-4">
            <header>
              <h2 className="text-lg font-semibold">Derniers puzzles</h2>
              <p className="text-highlight-secondary">
                Voici les derniers puzzles que vous avez résolus ou essayer de résoudres
              </p>
            </header>
            <div className="h-full max-h-96 overflow-y-scroll rounded-lg border-2 border-highlight-primary bg-primary-700 p-4 shadow-md">
              <ul className="flex flex-col space-y-2">
                {me?.completionsList && me.completionsList.length > 0 ? (
                  me?.completionsList
                    .sort(
                      (a, b) =>
                        a.score - b.score ||
                        a.tries - b.tries ||
                        a.puzzleName.localeCompare(b.puzzleName)
                    )
                    .map((completion, key) => {
                      return (
                        <li key={key} className="flex justify-between space-x-2">
                          <div className="flex items-center space-x-4">
                            <div className="flex items-center space-x-2">
                              <span className="text-lg">{completion.puzzleName}</span>
                            </div>
                          </div>
                          <div className="flex items-center space-x-4">
                            <div className="flex flex-col">
                              <span className="text-sm font-semibold">
                                Essai{completion.tries > 1 ? 's' : ''}
                              </span>
                              <span className="text-right text-lg text-highlight-secondary">
                                {completion.tries}
                              </span>
                            </div>
                            <div className="flex flex-col">
                              <span className="text-sm font-semibold">Score</span>
                              <span className="text-right text-lg text-highlight-secondary">
                                {completion.score}
                              </span>
                            </div>
                          </div>
                        </li>
                      );
                    })
                ) : (
                  <li className="m-auto flex items-center justify-center">
                    <span className="text-lg text-highlight-secondary">
                      {isLoading ? 'Chargement en cours...' : 'Aucun puzzles'}
                    </span>
                  </li>
                )}
              </ul>
            </div>
          </div>
        </div>
      </main>
    </section>
  );
}
