import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { formatNumber } from "@/lib/formatters"
import { _BedwarsStats, getBedwarsModeStats } from "@/lib/hypixel/bedwars"
import { Player } from "@/lib/schema/player"

export default function BedwarsStatTable({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    return (
        <Table>
            <BedwarsTableHeader />
            <TableBody>
                <SoloStats stats={stats} />
                <DoublesStats stats={stats} />
                <ThreesStats stats={stats} />
                <FoursStats stats={stats} />
                <CoreModeStats stats={stats} />
                <FourVFourStats stats={stats} />
                <RushDoublesStats stats={stats} />
                <Rush4sStats stats={stats} />
                <UltimateDoublesStats stats={stats} />
                <Ultimate4sStats stats={stats} />
                <LuckyDoublesStats stats={stats} />
                <Lucky4sStats stats={stats} />
                <VoidlessDoublesStats stats={stats} />
                <Voidless4sStats stats={stats} />
                <ArmedDoublesStats stats={stats} />
                <Armed4sStats stats={stats} />
                <Swap4sStats stats={stats} />
                <Underworld4sStats stats={stats} />
                <CastleStats stats={stats} />
            </TableBody>
        </Table>
    )
}

function BedwarsTableHeader() {
    const headerElements = [
        "Mode",
        "Kills",
        "Deaths",
        "KD",
        "Kills",
        "Deaths",
        "KD",
        "Wins",
        "Losses",
        "WL",
        "WS",
        "BB",
        "BL",
        "BBL"
    ]

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

function SoloStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("solo", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Solo</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function DoublesStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("doubles", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Doubles</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function ThreesStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("3s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>3v3v3v3</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function FoursStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function CoreModeStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const soloStats = getBedwarsModeStats("solo", stats as _BedwarsStats, true)
    const doublesStats = getBedwarsModeStats("doubles", stats as _BedwarsStats, true)
    const threesStats = getBedwarsModeStats("3s", stats as _BedwarsStats, true)
    const foursStats = getBedwarsModeStats("4s", stats as _BedwarsStats, true)

    const fraction = [2, 5, 8]

    const combinedStats = soloStats.map((v, i) => {
        if (v < 0) return "?"

        if (fraction.includes(i) || i === soloStats.length - 1) {
            return ((v + doublesStats[i] + threesStats[i] + foursStats[i]) / 4).toFixed(2)
        }

        return v + doublesStats[i] + threesStats[i] + foursStats[i]
    })

    return (
        <TableRow className="font-bold">
            <TableCell>Core Modes</TableCell>
            {combinedStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function FourVFourStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("4v4", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function RushDoublesStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("rush_2s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Rush Doubles</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function Rush4sStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("rush_4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Rush 4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function UltimateDoublesStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("ultimate_2s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Ultimate Doubles</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function Ultimate4sStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("ultimate_4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Ultimate 4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function LuckyDoublesStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("lucky_2s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Lucky Doubles</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function Lucky4sStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("lucky_4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Lucky 4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function VoidlessDoublesStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("voidless_2s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Voidless Doubles</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function Voidless4sStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("voidless_4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Voidless 4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function ArmedDoublesStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("armed_2s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Armed Doubles</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function Armed4sStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("armed_4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Armed 4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function Swap4sStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("swap_4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Swap 4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function Underworld4sStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("underworld_4s", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Underworld 4v4v4v4</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}

function CastleStats({ stats }: { stats: Player["player"]["stats"]["Bedwars"] }) {
    const modeStats = getBedwarsModeStats("castle", stats as _BedwarsStats)

    if (!modeStats) return null

    return (
        <TableRow>
            <TableCell>Castle</TableCell>
            {modeStats.map((v, i) => {
                return <TableCell key={i}>{typeof v === "number" ? formatNumber(v) : v}</TableCell>
            })}
        </TableRow>
    )
}
