/*
 * Decompiled with CFR 0.152.
 */
package com.evilco.mc.nbt.tag;

import com.evilco.mc.nbt.error.UnexpectedTagTypeException;
import com.evilco.mc.nbt.stream.NbtInputStream;
import com.evilco.mc.nbt.stream.NbtOutputStream;
import com.evilco.mc.nbt.tag.AbstractTag;
import com.evilco.mc.nbt.tag.IAnonymousTagContainer;
import com.evilco.mc.nbt.tag.ITag;
import com.evilco.mc.nbt.tag.TagType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;

public class TagList
extends AbstractTag
implements IAnonymousTagContainer {
    protected List<ITag> tagList;

    public TagList(@Nonnull String name) {
        super(name);
        this.tagList = new ArrayList<ITag>();
    }

    public TagList(@Nonnull String name, @Nonnull List<ITag> tagList) {
        super(name);
        Preconditions.checkNotNull(tagList, "tagList");
        this.tagList = tagList;
    }

    public TagList(@Nonnull NbtInputStream inputStream, boolean anonymous) throws IOException {
        super(inputStream, anonymous);
        this.tagList = new ArrayList<ITag>();
        byte type = inputStream.readByte();
        TagType tagType = TagType.valueOf(type);
        int size = inputStream.readInt();
        if (tagType == TagType.END) {
            return;
        }
        for (int i = 0; i < size; ++i) {
            this.addTag(inputStream.readTag(tagType, true));
        }
    }

    @Override
    public void addTag(@Nonnull ITag tag) {
        this.tagList.add(tag);
    }

    @Override
    public List<ITag> getTags() {
        return ((ImmutableList.Builder)new ImmutableList.Builder().addAll(this.tagList)).build();
    }

    @Override
    public <T extends ITag> List<T> getTags(Class<T> tagClass) throws UnexpectedTagTypeException {
        ImmutableList.Builder builder = new ImmutableList.Builder();
        for (ITag tag : this.tagList) {
            if (!tagClass.isInstance(tag)) {
                throw new UnexpectedTagTypeException("The list entry should be of type " + tagClass.getSimpleName() + ", but is of type " + tag.getClass().getSimpleName());
            }
            builder.add(tag);
        }
        return builder.build();
    }

    @Override
    public byte getTagID() {
        return TagType.LIST.typeID;
    }

    @Override
    public void removeTag(@Nonnull ITag tag) {
        this.tagList.remove(tag);
    }

    @Override
    public void setTag(int i, @Nonnull ITag tag) {
        this.tagList.set(i, tag);
    }

    @Override
    public void write(NbtOutputStream outputStream, boolean anonymous) throws IOException {
        super.write(outputStream, anonymous);
        outputStream.writeByte(this.tagList.size() > 0 ? this.tagList.get(0).getTagID() : TagType.END.typeID);
        outputStream.writeInt(this.tagList.size());
        for (ITag tag : this.tagList) {
            tag.write(outputStream, true);
        }
    }
}
