import DisplayName from "@/components/player/displayname"
import { Accordion } from "@/components/ui/accordion"
import { Card, CardContent } from "@/components/ui/card"
import { env } from "@/lib/env/server"
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, ShieldAlert } from "lucide-react"
import { Metadata } from "next"
import { Suspense } from "react"
import { PlayerPageLoadText } from "./_client"
import Sidebar from "./_components/Sidebar"
import BedwarsStats from "./_stats/bedwars/bedwars"
import BuildBattleStats from "./_stats/build-battle/build-battle"
import CopsAndCrimsStats from "./_stats/copsandcrims/copsandcrims"
import DuelsStats from "./_stats/duels/duels"
import MegaWallsStats from "./_stats/megawalls/megawalls"
import MurderMysteryStats from "./_stats/murder-mystery/murder-mystery"
import PitStats from "./_stats/pit/pit"
import SkyWarsStats from "./_stats/skywars/skywars"
import TNTGamesStats from "./_stats/tnt-games/tnt-games"
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 function PlayerPage({ params }: PageProps<"/player/[ign]">) {
    const maintenance = env.MAINTENANCE_MODE

    if (maintenance) {
        return (
            <div className="flex flex-col gap-2 justify-center items-center h-screen">
                <ShieldAlert className="size-30" />
                <h1 className="text-xl">Not available right now. This is just so I could have a front page for Hypixel Production API Key.</h1>
            </div>
        )
    }

    return (
        <Suspense
            fallback={
                <div className="flex flex-col justify-center items-center h-screen">
                    <Loader2Icon className="animate-spin size-30" />
                    <PlayerPageLoadText />
                </div>
            }
        >
            <SuspendedPage params={params} />
        </Suspense>
    )
}

async function SuspendedPage({ params }: Pick<PageProps<"/player/[ign]">, "params">) {
    const { ign: pign } = await params

    const mc = await getUuid(pign)
    if (!mc) {
        return (
            <div className="flex flex-col items-center min-h-screen">
                <h1 className="mt-35">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-35">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 pb-5">
            <DisplayName
                ign={player.displayname}
                uuid={player.uuid}
                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}
                prefix={player.prefix}
            />
            <h1>
                {player.uuid}
            </h1>
            <div className="flex flex-col gap-6 px-6 pb-4 mt-8 w-full max-w-7xl md:flex-row">
                <Sidebar
                    level={level}
                    ign={pign}
                    player={player}
                    guild={guild ?? undefined}
                    rank={player.newPackageRank}
                    specialRank={player.rank}
                    eulaCoins={player.eulaCoins}
                />
                {player.stats !== undefined ?
                    (
                        <div className="w-full md: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} />
                                <PitStats stats={player.stats.Pit} />
                                <TNTGamesStats stats={player.stats.TNTGames} />
                                <MegaWallsStats stats={player.stats.MegaWalls} />
                                <CopsAndCrimsStats stats={player.stats.CopsAndCrims} />
                            </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>
    )
}
