package net.minecraft.world.level;

import java.util.AbstractList;
import com.google.common.collect.Lists;
import java.util.Iterator;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.core.BlockPos;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import net.minecraft.resources.ResourceLocation;
import java.util.function.Function;
import java.util.List;

public class ChunkTickList<T> implements TickList<T> {
    private final List<ScheduledTick<T>> ticks;
    private final Function<T, ResourceLocation> toId;
    
    public ChunkTickList(final Function<T, ResourceLocation> function, final List<TickNextTickData<T>> list, final long long3) {
        this(function, (List)list.stream().map(bqs -> new ScheduledTick(bqs.getType(), bqs.pos, (int)(bqs.triggerTick - long3), bqs.priority)).collect(Collectors.toList()));
    }
    
    private ChunkTickList(final Function<T, ResourceLocation> function, final List<ScheduledTick<T>> list) {
        this.ticks = list;
        this.toId = function;
    }
    
    @Override
    public boolean hasScheduledTick(final BlockPos fu, final T object) {
        return false;
    }
    
    @Override
    public void scheduleTick(final BlockPos fu, final T object, final int integer, final TickPriority bqt) {
        this.ticks.add(new ScheduledTick<T>(object, fu, integer, bqt));
    }
    
    @Override
    public boolean willTickThisTick(final BlockPos fu, final T object) {
        return false;
    }
    
    public ListTag save() {
        final ListTag lk2 = new ListTag();
        for (final ScheduledTick<T> a4 : this.ticks) {
            final CompoundTag le5 = new CompoundTag();
            le5.putString("i", this.toId.apply((T)((ScheduledTick<Object>)a4).type).toString());
            le5.putInt("x", a4.pos.getX());
            le5.putInt("y", a4.pos.getY());
            le5.putInt("z", a4.pos.getZ());
            le5.putInt("t", a4.delay);
            le5.putInt("p", a4.priority.getValue());
            ((AbstractList<CompoundTag>)lk2).add(le5);
        }
        return lk2;
    }
    
    public static <T> ChunkTickList<T> create(final ListTag lk, final Function<T, ResourceLocation> function2, final Function<ResourceLocation, T> function3) {
        final List<ScheduledTick<T>> list4 = Lists.newArrayList();
        for (int integer5 = 0; integer5 < lk.size(); ++integer5) {
            final CompoundTag le6 = lk.getCompound(integer5);
            final T object7 = function3.apply(new ResourceLocation(le6.getString("i")));
            if (object7 != null) {
                final BlockPos fu8 = new BlockPos(le6.getInt("x"), le6.getInt("y"), le6.getInt("z"));
                list4.add(new ScheduledTick<T>(object7, fu8, le6.getInt("t"), TickPriority.byValue(le6.getInt("p"))));
            }
        }
        return new ChunkTickList<T>(function2, list4);
    }
    
    public void copyOut(final TickList<T> bqr) {
        this.ticks.forEach(a -> bqr.scheduleTick(a.pos, (T)a.type, a.delay, a.priority));
    }
    
    static class ScheduledTick<T> {
        private final T type;
        public final BlockPos pos;
        public final int delay;
        public final TickPriority priority;
        
        private ScheduledTick(final T object, final BlockPos fu, final int integer, final TickPriority bqt) {
            this.type = object;
            this.pos = fu;
            this.delay = integer;
            this.priority = bqt;
        }
        
        @Override
        public String toString() {
            return this.type + ": " + this.pos + ", " + this.delay + ", " + this.priority;
        }
    }
}
