import { getColor } from "@/lib/colors"
import { Player } from "@/lib/schema/player"
import { Wifi, WifiOff } from "lucide-react"
import Link from "next/link"

type NewPackageRank = Player["player"]["newPackageRank"]
type MonthlyPackageRank = Player["player"]["monthlyPackageRank"]
type RankColor = Player["player"]["monthlyRankColor"]

export default function DisplayName(
    { ign, rank, monthly, rankColor, plusColor, guildTag, tagColor, specialRank, lastLogin, lastLogout }: {
        ign: string
        rank: NewPackageRank
        monthly: MonthlyPackageRank
        rankColor: RankColor
        plusColor: string | undefined
        guildTag: string | undefined
        tagColor: string | undefined
        specialRank: string | undefined
        lastLogin: number | undefined
        lastLogout: number | undefined
    }
) {
    return (
        <div className="flex items-end mt-25">
            <h1 className="text-3xl font-bold">
                <PlayerRank rank={rank} monthly={monthly} plusColor={plusColor} rankColor={rankColor} specialRank={specialRank} />{" "}
                <PlayerIGN ign={ign} rank={rank} monthly={monthly} rankColor={rankColor} specialRank={specialRank} />{" "}
                <GuildTag tag={guildTag} tagColor={tagColor} ign={ign} />
            </h1>
            <OnlineStatus lastLogin={lastLogin} lastLogout={lastLogout} />
        </div>
    )
}

function PlayerIGN(
    { ign, rank, monthly, rankColor, specialRank }: {
        ign: string
        rank: NewPackageRank
        monthly: MonthlyPackageRank
        rankColor: RankColor
        specialRank: string | undefined
    }
) {
    if (specialRank) {
        if (specialRank === "YOUTUBER") {
            return <span className="text-mc-red">{ign}</span>
        }

        if (specialRank === "STAFF") {
            return <span className="text-mc-red">{ign}</span>
        }
    }

    if (monthly === "SUPERSTAR") {
        if (rankColor === "GOLD") {
            return <span className="text-mc-gold">{ign}</span>
        } else {
            return <span className="text-mc-aqua">{ign}</span>
        }
    }

    switch (rank) {
        case "VIP":
            return <span className="text-mc-green">{ign}</span>
        case "VIP_PLUS":
            return <span className="text-mc-green">{ign}</span>
        case "MVP":
            return <span className="text-mc-aqua">{ign}</span>
        case "MVP_PLUS":
            return <span className="text-mc-aqua">{ign}</span>
        default:
            return <span className="text-mc-gray">{ign}</span>
    }
}

function PlayerRank(
    { rank, monthly, plusColor, rankColor, specialRank }: {
        rank: NewPackageRank
        monthly: MonthlyPackageRank
        plusColor?: string
        rankColor: RankColor
        specialRank: string | undefined
    }
) {
    if (specialRank) {
        if (specialRank === "YOUTUBER") {
            return (
                <>
                    <span className="text-mc-red">[</span>
                    <span className="text-mc-white">YOUTUBE</span>
                    <span className="text-mc-red">]</span>
                </>
            )
        }

        if (specialRank === "STAFF") {
            return (
                <>
                    <span className="text-mc-red">[</span>
                    <span className="text-mc-gold">ዞ</span>
                    <span className="text-mc-red">]</span>
                </>
            )
        }
    }

    if (monthly === "SUPERSTAR") {
        if (rankColor === "GOLD") {
            return (
                <>
                    <span className="text-mc-gold">[MVP</span>
                    <span className={getColor(plusColor)}>++</span>
                    <span className="text-mc-gold">]</span>
                </>
            )
        } else {
            return (
                <>
                    <span className="text-mc-aqua">[MVP</span>
                    <span className={getColor(plusColor)}>++</span>
                    <span className="text-mc-aqua">]</span>
                </>
            )
        }
    }

    switch (rank) {
        case "VIP":
            return <span className="text-mc-green">[VIP]</span>
        case "VIP_PLUS":
            return (
                <>
                    <span className="text-mc-green">[VIP</span>
                    <span className="text-mc-gold">+</span>
                    <span className="text-mc-green">]</span>
                </>
            )
        case "MVP":
            return <span className="text-mc-aqua">[MVP]</span>
        case "MVP_PLUS":
            return (
                <>
                    <span className="text-mc-aqua">[MVP</span>
                    <span className={getColor(plusColor)}>+</span>
                    <span className="text-mc-aqua">]</span>
                </>
            )
        default:
            return null
    }
}

function GuildTag({ tag, tagColor, ign }: { tag?: string, tagColor?: string, ign: string }) {
    if (!tag) return null

    const color = getColor(tagColor, "text", "gray")

    return (
        <Link href={`/guild/${ign}`}>
            <span className={color}>[{tag}]</span>
        </Link>
    )
}
}
