// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.adventure.npcreputation;

import com.hypixel.hytale.component.RemoveReason;
import java.util.Iterator;
import com.hypixel.hytale.builtin.tagset.TagSetPlugin;
import com.hypixel.hytale.builtin.tagset.config.NPCGroup;
import java.util.Map;
import com.hypixel.hytale.builtin.adventure.reputation.assets.ReputationGroup;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Holder;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.builtin.adventure.reputation.ReputationGroupComponent;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.HolderSystem;

public class NPCReputationHolderSystem extends HolderSystem<EntityStore>
{
    private final ComponentType<EntityStore, ReputationGroupComponent> reputationGroupComponentType;
    private final ComponentType<EntityStore, NPCEntity> npcEntityComponentType;
    @Nonnull
    private final Query<EntityStore> query;
    
    public NPCReputationHolderSystem(final ComponentType<EntityStore, ReputationGroupComponent> reputationGroupComponentType, final ComponentType<EntityStore, NPCEntity> npcEntityComponentType) {
        this.reputationGroupComponentType = reputationGroupComponentType;
        this.npcEntityComponentType = npcEntityComponentType;
        this.query = (Query<EntityStore>)Query.and(npcEntityComponentType, Query.not((Query<Object>)reputationGroupComponentType));
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
    
    @Override
    public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
        final NPCEntity npcEntity = holder.getComponent(this.npcEntityComponentType);
        final int npcTypeIndex = npcEntity.getNPCTypeIndex();
        for (final Map.Entry<String, ReputationGroup> reputationEntry : ReputationGroup.getAssetMap().getAssetMap().entrySet()) {
            for (final String npcGroup : reputationEntry.getValue().getNpcGroups()) {
                final int index = NPCGroup.getAssetMap().getIndex(npcGroup);
                if (index == Integer.MIN_VALUE) {
                    throw new IllegalArgumentException("Unknown npc group! " + npcGroup);
                }
                if (TagSetPlugin.get(NPCGroup.class).tagInSet(index, npcTypeIndex)) {
                    holder.addComponent(this.reputationGroupComponentType, new ReputationGroupComponent(reputationEntry.getKey()));
                    return;
                }
            }
        }
    }
    
    @Override
    public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
    }
}
