package de.steamwar.tntleague.util

import de.steamwar.tntleague.game.TNTLeagueTeam
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.ComponentLike
import net.kyori.adventure.text.TranslatableComponent
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.format.TextDecoration
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.kyori.adventure.translation.GlobalTranslator
import org.bukkit.entity.Player
import java.util.Locale

val prefix = Component.text("Steam").yellow()
    .append(Component.text("War").darkGray())
    .appendSpace()

val tntLeaguePrefix = Component.text("TNT").color(NamedTextColor.DARK_RED)
    .append(Component.text("League").color(NamedTextColor.GOLD))

val tntLeagueChatPrefix: Component = tntLeaguePrefix
    .append(Component.text("»").darkGray())
    .appendSpace()

fun TranslatableComponent.basic(): Component = tntLeagueChatPrefix.append(this.gray())

fun TranslatableComponent.error(): Component = tntLeagueChatPrefix.append(this.red())

fun TranslatableComponent.success(): Component = tntLeagueChatPrefix.append(this.green())

fun String.component(): Component = Component.text(this)

fun Component.bold(): Component = this.decorate(TextDecoration.BOLD)

fun String.bold(): Component = this.component().bold()

fun Component.yellow(): Component = this.color(NamedTextColor.YELLOW)

fun String.yellow(): Component = this.component().yellow()

fun Component.red(): Component = this.color(NamedTextColor.RED)

fun String.red(): Component = this.component().red()

fun Component.green(): Component = this.color(NamedTextColor.GREEN)

fun String.green(): Component = this.component().green()

fun Component.gray(): Component = this.color(NamedTextColor.GRAY)

fun String.gray(): Component = this.component().gray()

fun Component.darkGray(): Component = this.color(NamedTextColor.DARK_GRAY)

fun String.darkGray(): Component = this.component().darkGray()

fun Component.gold(): Component = this.color(NamedTextColor.GOLD)

fun translate(key: String, vararg args: ComponentLike): TranslatableComponent = Component.translatable(key, *args).decoration(TextDecoration.ITALIC, false)

fun Component.reset(): Component = this.style(Style.empty())

fun Component.colorByTeam(team: TNTLeagueTeam?) = when (team) {
    null -> this.gray()
    else -> this.color(team.color)
}

fun Component.translate(locale: Locale): Component = GlobalTranslator.render(this, locale)

fun Component.translate(p: Player): Component = this.translate(p.locale())

fun Component.toLegacy(): String = LegacyComponentSerializer.legacySection().serialize(this)
