import DisplayName from "@/components/player/displayname"
import { Accordion } from "@/components/ui/accordion"
import { Card, CardContent } from "@/components/ui/card"
import { getGuild } from "@/lib/hypixel/api/guild"
import { getUuid } from "@/lib/hypixel/api/mojang"
import { getPlayer } from "@/lib/hypixel/api/player"
import { getExactLevel } from "@/lib/hypixel/general/level"
import { Loader2Icon } from "lucide-react"
import { Metadata } from "next"
import { Suspense } from "react"
import Sidebar from "./_components/Sidebar"
import BedwarsStats from "./_stats/bedwars/bedwars"
import BuildBattleStats from "./_stats/build-battle/build-battle"
import DuelsStats from "./_stats/duels/duels"
import MurderMysteryStats from "./_stats/murder-mystery/murder-mystery"
import SkyWarsStats from "./_stats/skywars/skywars"
import UHCStats from "./_stats/uhc/uhc"

export async function generateMetadata({ params }: { params: Promise<{ ign: string }> }): Promise<Metadata> {
    const { ign } = await params
    const user = await getUuid(ign)
    return { title: user !== null ? user.name : "Player not found" }
}

export default async function PlayerPage({ params }: PageProps<"/player/[ign]">) {
    const { ign } = await params

    return (
        <Suspense
            fallback={
                <div className="flex flex-col justify-center items-center h-screen">
                    <Loader2Icon className="animate-spin size-30" />
                    <p>{`Loading stats for ${ign}`}</p>
                </div>
            }
        >
            <SuspendedPage ign={ign} />
        </Suspense>
    )
}

async function SuspendedPage({ ign: pign }: { ign: string }) {
    const mc = await getUuid(pign)
    if (!mc) {
        return (
            <div className="flex flex-col items-center min-h-screen">
                <h1 className="mt-25">Player not found</h1>
            </div>
        )
    }

    const player = await getPlayer(mc.id)

    if (!player) {
        return (
            <div className="flex flex-col items-center min-h-screen">
                <h1 className="mt-25">Player hasn&apos;t joined hypixel</h1>
            </div>
        )
    }

    const guild = await getGuild(mc.id)
    const level = getExactLevel(player.networkExp)

    return (
        <div className="flex flex-col items-center">
            <DisplayName
                ign={player.displayname}
                rank={player.newPackageRank}
                monthly={player.monthlyPackageRank}
                rankColor={player.monthlyRankColor}
                plusColor={player.rankPlusColor}
                guildTag={guild?.tag}
                tagColor={guild?.tagColor}
                specialRank={player.rank}
                lastLogin={player.lastLogin}
                lastLogout={player.lastLogout}
            />
            <h1>
                {player.uuid}
            </h1>
            <div className="flex gap-6 px-6 mt-8 w-full max-w-7xl">
                <Sidebar
                    level={level}
                    ign={pign}
                    player={player}
                    guild={guild ?? undefined}
                    rank={player.newPackageRank}
                    specialRank={player.rank}
                    eulaCoins={player.eulaCoins}
                />
                {player.stats !== undefined ?
                    (
                        <div className="pb-4 w-3/4">
                            <Accordion type="multiple" className="space-y-4">
                                <BedwarsStats stats={player.stats.Bedwars} />
                                <SkyWarsStats
                                    stats={player.stats.SkyWars}
                                    achievements_skywars_opal_obsession={player.achievements?.["skywars_opal_obsession"] ?? 0}
                                />
                                <DuelsStats stats={player.stats.Duels} />
                                <MurderMysteryStats stats={player.stats.MurderMystery} />
                                <BuildBattleStats stats={player.stats.BuildBattle} />
                                <UHCStats stats={player.stats.UHC} />
                            </Accordion>
                        </div>
                    ) :
                    (
                        <div className="w-3/4">
                            <Card>
                                <CardContent className="flex justify-center">
                                    <h1 className="text-xl font-bold">
                                        No stats avaiable. If they are staff then they most likely have their api off.
                                    </h1>
                                </CardContent>
                            </Card>
                        </div>
                    )}
            </div>
        </div>
    )
}
