package sh.grimmer.bettershulkers.render;

import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.item.ItemStack;
import sh.grimmer.bettershulkers.BetterShulkers;
import sh.grimmer.bettershulkers.util.ShulkerBoxHelper;

import org.jetbrains.annotations.Nullable;

public class ShulkerHintData {

    @Nullable
    private Component label;
    @Nullable
    private ItemStack displayItem;

    public ShulkerHintData(@Nullable Component label, @Nullable ItemStack displayItem) {
        this.label = label;
        this.displayItem = displayItem;
    }

    public ShulkerHintData() {
    }

    @Nullable
    public Component getLabel() {
        return label;
    }

    @Nullable
    public FormattedCharSequence getLabelFormatted() {
        return label != null ? label.getVisualOrderText() : null;
    }

    @Nullable
    public ItemStack getDisplayItem() {
        return displayItem;
    }

    public void setLabel(@Nullable Component label) {
        this.label = label;
    }

    public void setDisplayItem(@Nullable ItemStack displayItem) {
        this.displayItem = displayItem;
    }

    public static ShulkerHintData fromShulkerBox(NonNullList<ItemStack> contents, @Nullable Component name) {
        ShulkerHintData data = new ShulkerHintData();
        data.setDisplayItem(determineDisplayItem(contents));
        if (BetterShulkers.CONFIG.showLabels) {
            data.setLabel(name);
        }
        return data;
    }

    @Nullable
    private static ItemStack determineDisplayItem(NonNullList<ItemStack> contents) {
        return switch (BetterShulkers.CONFIG.itemDisplayType) {
            case NONE -> null;
            case SINGLE_TYPE -> ShulkerBoxHelper.getSingleTypeItem(contents);
            case BULK -> ShulkerBoxHelper.getBulkItem(contents);
            case FIRST_ITEM -> contents.stream().filter(s -> !s.isEmpty()).findFirst().orElse(null);
        };
    }
}
