import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { formatNumber } from "@/lib/formatters"
import { getArcadeMostPlayedPixelPartyMode, getArcadePixelPartyModeName, getArcadePixelPartyModeStats } from "@/lib/hypixel/arcade/general"
import { NonNullStats } from "@/lib/schema/player"
import { cn } from "@/lib/utils"

export function PixelPartyStatsTable({ stats }: { stats: NonNullable<NonNullStats["Arcade"]>["pixel_party"] }) {
    return (
        <Table>
            <PixelPartyStatsTableHeader />
            <TableBody>
                <PixelPartyTableStat modeId="normal" stats={stats} />
                <PixelPartyTableStat modeId="hyper" stats={stats} />
                <PixelPartyTableStat modeId="all_modes" stats={stats} />
            </TableBody>
        </Table>
    )
}

function PixelPartyTableStat(
    { modeId, stats }: { modeId: Parameters<typeof getArcadePixelPartyModeStats>[0], stats: NonNullable<NonNullStats["Arcade"]>["pixel_party"] }
) {
    const modeName = getArcadePixelPartyModeName(modeId)
    const modeStats = getArcadePixelPartyModeStats(modeId, stats)
    const mostPlayed = getArcadeMostPlayedPixelPartyMode(stats) === modeId
    const isMostPlayed = modeId !== "all_modes" && mostPlayed

    return (
        <TableRow className={cn(modeName === "Overall" && "font-bold", isMostPlayed && "text-mc-light-purple")}>
            <TableCell>{modeName}</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{formatNumber(v)}</TableCell>
            })}
        </TableRow>
    )
}

function PixelPartyStatsTableHeader() {
    const headerElements = [
        "Mode",
        "Wins",
        "Losses",
        "WL",
        "Games Played",
        "Rounds Completed",
        "Power-Ups Collected"
    ]

    return (
        <TableHeader>
            <TableRow>
                {headerElements.map((v, i) => <TableHead key={i} className="font-bold">{v}</TableHead>)}
            </TableRow>
        </TableHeader>
    )
}
