package eu.mhsl.minecraft.pixelblocks;

import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import eu.mhsl.minecraft.pixelblocks.utils.HeadUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

public class PixelBlockItem {
    public static final NamespacedKey recipeKey = new NamespacedKey(Main.plugin(), "pixelblock");
    public static NamespacedKey idProperty = new NamespacedKey(Main.plugin(), "id");
    public static NamespacedKey ownerProperty = new NamespacedKey(Main.plugin(), "owner");
    public static UUID emptyBlockUUID = UUID.fromString("98fdf0ae-c3ab-4ef7-ae25-efd518d600de");

    public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQ" +
        "ubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";

    public record BlockInfo(UUID id, @Nullable UUID owner) {
        public boolean hasOwner() {
            return owner != null;
        }
    }

    public static void setBlockId(@NotNull ItemStack item, @NotNull UUID id) {
        ItemMeta meta = item.getItemMeta();
        meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, id.toString());
        item.setItemMeta(meta);
    }

    public static @Nullable BlockInfo getBlockInfo(ItemStack item) {
        PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer();
        if(!container.has(idProperty)) return null;
        UUID blockId = UUID.fromString(Objects.requireNonNull(container.get(idProperty, PersistentDataType.STRING)));
        UUID ownerId = container.has(ownerProperty)
            ? UUID.fromString(Objects.requireNonNull(container.get(ownerProperty, PersistentDataType.STRING)))
            : null;
        return new BlockInfo(blockId, ownerId);
    }

    public static @NotNull ItemStack getBlockAsItem(@NotNull PixelBlock block, @NotNull Locale locale) {
        String ownerName = Optional.ofNullable(Bukkit.getOfflinePlayer(block.getOwnerUUID()).getName()).orElseGet(() -> block.getOwnerUUID().toString());

        ItemStack itemStack = HeadUtil.getCustomTextureHead(itemTexture);
        ItemMeta meta = itemStack.getItemMeta();
        meta.setMaxStackSize(1);
        meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, block.getBlockUUID().toString());
        meta.getPersistentDataContainer().set(ownerProperty, PersistentDataType.STRING, block.getOwnerUUID().toString());
        meta.displayName(Translations.render(Component.translatable("pixelblocks.item.name.owned", Component.text(ownerName)), locale));
        meta.lore(List.of(
            Translations.render(Component.translatable("pixelblocks.item.lore.owner", Component.text(ownerName)), locale),
            Translations.render(Component.translatable("pixelblocks.item.lore.edit-hint"), locale),
            Component.text(block.getBlockUUID().toString()).color(NamedTextColor.DARK_GRAY)
        ));
        itemStack.setItemMeta(meta);

        return itemStack;
    }

    public static @NotNull ItemStack getEmptyPixelBlock() {
        return getEmptyPixelBlock(Translations.defaultLocale);
    }

    public static @NotNull ItemStack getEmptyPixelBlock(@NotNull Locale locale) {
        ItemStack item = HeadUtil.getCustomTextureHead(itemTexture);
        ItemMeta meta = item.getItemMeta();
        meta.setMaxStackSize(1);
        meta.displayName(Translations.render(Component.translatable("pixelblocks.item.name.empty"), locale));
        meta.lore(List.of(
            Translations.render(Component.translatable("pixelblocks.item.lore.first-placer"), locale),
            Translations.render(Component.translatable("pixelblocks.item.lore.edit-hint"), locale)
        ));
        meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, emptyBlockUUID.toString());
        item.setItemMeta(meta);
        return item;
    }

    public static @NotNull Recipe getRecipe() {
        ShapedRecipe recipe = new ShapedRecipe(recipeKey, getEmptyPixelBlock());
        recipe.shape("AEA", "BCB", "DDD");
        recipe.setIngredient('A', Material.GLASS);
        recipe.setIngredient('B', Material.DIAMOND_BLOCK);
        recipe.setIngredient('C', Material.HEART_OF_THE_SEA);
        recipe.setIngredient('D', Material.GRASS_BLOCK);
        recipe.setIngredient('E', Material.END_CRYSTAL);
        return recipe;
    }
}

