import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { formatNumber } from "@/lib/formatters"
import { getSpeedUHCBestMode, getSpeedUHCModeName, getSpeedUHCModeStats } from "@/lib/hypixel/speeduhc/general"
import { NonNullStats } from "@/lib/schema/player"
import { cn } from "@/lib/utils"

export function SpeedUHCModeStatsTable({ stats }: { stats: NonNullable<NonNullStats["SpeedUHC"]> }) {
    return (
        <Table>
            <SpeedUHCModeStatsTableHeader />
            <TableBody>
                <SpeedUHCStatRow modeId="solo_normal" stats={stats} />
                <SpeedUHCStatRow modeId="solo_insane" stats={stats} />
                <SpeedUHCStatRow modeId="team_normal" stats={stats} />
                <SpeedUHCStatRow modeId="team_insane" stats={stats} />
                <SpeedUHCStatRow modeId="all_modes" stats={stats} />
            </TableBody>
        </Table>
    )
}

function SpeedUHCStatRow({ modeId, stats }: { modeId: Parameters<typeof getSpeedUHCModeStats>[0], stats: NonNullable<NonNullStats["SpeedUHC"]> }) {
    const modeStats = getSpeedUHCModeStats(modeId, stats)
    const modeName = getSpeedUHCModeName(modeId)
    const isBest = getSpeedUHCBestMode(stats) === modeId

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

function SpeedUHCModeStatsTableHeader() {
    const headerElements = [
        "Mode",
        "Kills",
        "Deaths",
        "KD",
        "Wins",
        "Losses",
        "WL"
    ]

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