package de.steamwar.tntleague.inventory

import de.steamwar.tntleague.config.TNTLeagueConfig
import de.steamwar.tntleague.plugin
import de.steamwar.tntleague.util.*
import net.kyori.adventure.sound.Sound
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import java.util.*
import kotlin.math.ceil

class DealerInventory(player: Player): SWInventoryHolder() {

    init {
        items.forEachIndexed { index, item ->
            this[index] = item.first to {
                val price = item.second.price * if (it.isShiftClick) 5 else 1
                val amount = item.second.amount * if (it.isShiftClick) 5 else 1

                if (!player.inventory.containsAtLeast(coins, price)) {
                    player.sendMessage(translate("notEnoughCoins").error())
                    player.playSound(Sound.sound(org.bukkit.Sound.ENTITY_VILLAGER_HURT.key, net.kyori.adventure.sound.Sound.Source.MASTER, 1f, 1f))
                    return@to
                }

                player.inventory.removeItem(coins.asQuantity(price))
                player.inventory.addItem(ItemStack.of(item.first.type, amount))
            }
        }
    }

    override fun createInventory(): Inventory = plugin.server.createInventory(this, ceil(TNTLeagueConfig.config.prices.size / 9f).toInt() * 9, translate("dealer").reset())

    companion object {
        private val priceKey = NamespacedKey(plugin, "price")
        private val amountKey = NamespacedKey(plugin, "amount")
        private val coinKey = NamespacedKey(plugin, "coin")

        val coins = ItemStack(Material.RAW_GOLD).apply {
            itemMeta = itemMeta.apply {
                displayName(Component.text("Coins").bold().gold())
                persistentDataContainer.apply {
                    set(coinKey, PersistentDataType.BOOLEAN, true)
                }
            }
        }

        val items by lazy {
            val prices = TNTLeagueConfig.config.prices

            prices.map { (material, price) ->
                ItemStack(material).apply {
                    itemMeta = itemMeta.apply {
                        displayName(material.name.lowercase().replace("_", " ")
                            .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
                            .component().gray().appendSpace().append(price.amount.toString().component().yellow()))
                        amount = price.amount
                        lore(listOf(price.price.toString().component().yellow().bold().appendSpace().append(Component.text("Coins").yellow())))
                        persistentDataContainer.apply {
                            set(priceKey, PersistentDataType.INTEGER, price.price)
                            set(amountKey, PersistentDataType.INTEGER, price.amount)
                        }
                    }
                } to price
            }
        }
    }
}
