import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { formatNumber, formatSecondsToTime } from "@/lib/formatters"
import { getAllBlitzKitStats, getBlitzKitLevel, getBlitzKitName, getBlitzMostPlayedKit } from "@/lib/hypixel/blitz/general"
import { romanize } from "@/lib/hypixel/general"
import { NonNullStats } from "@/lib/schema/player"
import { cn } from "@/lib/utils"

export function BlitzModeStatsTable({ stats }: { stats: NonNullable<NonNullStats["Blitz"]> }) {
    return (
        <Table>
            <TableHeader>
                <TableRow>
                    <TableHead>Mode</TableHead>
                    <TableHead>Kills</TableHead>
                    <TableHead>Wins</TableHead>
                </TableRow>
            </TableHeader>
            <TableBody>
                <TableRow>
                    <TableCell>Solo Normal</TableCell>
                    <TableCell>{formatNumber(stats.kills_solo_normal)}</TableCell>
                    <TableCell>{formatNumber(stats.wins_solo_normal)}</TableCell>
                </TableRow>
                <TableRow>
                    <TableCell>Teams Normal</TableCell>
                    <TableCell>{formatNumber(stats.kills_teams_normal)}</TableCell>
                    <TableCell>{formatNumber(stats.wins_teams_normal)}</TableCell>
                </TableRow>
            </TableBody>
        </Table>
    )
}

export function BlitzKitStatsTableHeader() {
    const headerElements = [
        "Kit",
        "Exp",
        "Prestige",
        "Kills",
        "Wins",
        "Losses",
        "WL",
        "Time Played"
    ]

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

export function BlitzKitStatsTable({ stats }: { stats: NonNullable<NonNullStats["Blitz"]> }) {
    const kitStats = getAllBlitzKitStats(stats)
    return (
        <Table>
            <BlitzKitStatsTableHeader />
            <TableBody>
                {kitStats.map((r, i) => {
                    const { id, nums } = r
                    const kitName = getBlitzKitName(id)
                    const kitLevel = getBlitzKitLevel(id, stats)
                    const mostPlayed = getBlitzMostPlayedKit(stats)?.id
                    return (
                        <TableRow className={cn(mostPlayed === id && "text-mc-light-purple font-bold")} key={i}>
                            <TableCell className={cn(kitLevel === 10 && "text-mc-dark-red")}>{`${kitName} ${romanize(kitLevel)}`}</TableCell>
                            {nums.map((v, j) => {
                                if (j === nums.length - 1) {
                                    return <TableCell key={j}>{formatSecondsToTime(v)}</TableCell>
                                }
                                if (j === 1) {
                                    return <TableCell key={j}>{v === 0 ? "-" : romanize(v)}</TableCell>
                                }
                                return <TableCell key={j}>{formatNumber(v)}</TableCell>
                            })}
                        </TableRow>
                    )
                })}
            </TableBody>
        </Table>
    )
}
