package com.acethewildfire.acesbs.util;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.item.ModItems;
import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.AdvancementEntry;
import net.minecraft.advancement.AdvancementProgress;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.stat.StatFormatter;
import net.minecraft.stat.Stats;
import net.minecraft.util.Identifier;

import java.util.Objects;

public class ModStats {

    public static final Identifier CIGARETTES_USED = createStat(Identifier.of(AcesBS.MOD_ID, "cigarettes_used"));

    public static Identifier createStat(Identifier id){
        Registry.register(Registries.CUSTOM_STAT, id, id);
        Stats.CUSTOM.getOrCreateStat(id, StatFormatter.DEFAULT);

        return id;
    }

    public static void grantAdvancement(ServerPlayerEntity player, String id) {

        AdvancementEntry advancement =
                Objects.requireNonNull(player.getEntityWorld().getServer()).getAdvancementLoader().get(Identifier.ofVanilla(id));

        if (advancement == null) {
            AcesBS.LOGGER.info("Granting null");
            return;
        };

        AcesBS.LOGGER.info("Granting {}", advancement.toString());

        AdvancementProgress progress = player.getAdvancementTracker().getProgress(advancement);

        if (!progress.isDone()) {
            for (String criterion : progress.getUnobtainedCriteria()) {
                player.getAdvancementTracker().grantCriterion(advancement, criterion);
            }
        }
    }

    public static void registerStats() {
    }
}
