package net.minecraft.world.entity.ai.village.poi;

import com.mojang.datafixers.kinds.Applicative;
import java.util.Objects;
import com.mojang.datafixers.kinds.App;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Registry;
import com.mojang.serialization.Codec;
import net.minecraft.core.BlockPos;

public class PoiRecord {
    private final BlockPos pos;
    private final PoiType poiType;
    private int freeTickets;
    private final Runnable setDirty;
    
    public static Codec<PoiRecord> codec(final Runnable runnable) {
        return (Codec<PoiRecord>)RecordCodecBuilder.create(instance -> ((RecordCodecBuilder.Instance)instance).group((App)BlockPos.CODEC.fieldOf("pos").forGetter(aya -> aya.pos), (App)Registry.POINT_OF_INTEREST_TYPE.fieldOf("type").forGetter(aya -> aya.poiType), (App)Codec.INT.fieldOf("free_tickets").withDefault(0).forGetter(aya -> aya.freeTickets), (App)RecordCodecBuilder.point(runnable)).apply(instance, PoiRecord::new));
    }
    
    private PoiRecord(final BlockPos fu, final PoiType ayc, final int integer, final Runnable runnable) {
        this.pos = fu.immutable();
        this.poiType = ayc;
        this.freeTickets = integer;
        this.setDirty = runnable;
    }
    
    public PoiRecord(final BlockPos fu, final PoiType ayc, final Runnable runnable) {
        this(fu, ayc, ayc.getMaxTickets(), runnable);
    }
    
    protected boolean acquireTicket() {
        if (this.freeTickets <= 0) {
            return false;
        }
        --this.freeTickets;
        this.setDirty.run();
        return true;
    }
    
    protected boolean releaseTicket() {
        if (this.freeTickets >= this.poiType.getMaxTickets()) {
            return false;
        }
        ++this.freeTickets;
        this.setDirty.run();
        return true;
    }
    
    public boolean hasSpace() {
        return this.freeTickets > 0;
    }
    
    public boolean isOccupied() {
        return this.freeTickets != this.poiType.getMaxTickets();
    }
    
    public BlockPos getPos() {
        return this.pos;
    }
    
    public PoiType getPoiType() {
        return this.poiType;
    }
    
    @Override
    public boolean equals(final Object object) {
        return this == object || (object != null && this.getClass() == object.getClass() && Objects.equals(this.pos, ((PoiRecord)object).pos));
    }
    
    @Override
    public int hashCode() {
        return this.pos.hashCode();
    }
}
