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

package com.hypixel.hytale.server.npc.corecomponents.audiovisual;

import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import java.util.Map;
import java.util.HashMap;
import com.hypixel.hytale.server.core.asset.type.model.config.ModelAsset;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderActionBase;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import com.hypixel.hytale.server.npc.corecomponents.audiovisual.builders.BuilderActionModelAttachment;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.ActionBase;

public class ActionModelAttachment extends ActionBase
{
    @Nonnull
    protected final String slot;
    @Nonnull
    protected final String attachment;
    
    public ActionModelAttachment(@Nonnull final BuilderActionModelAttachment builder, @Nonnull final BuilderSupport support) {
        super(builder);
        this.slot = builder.getSlot(support);
        this.attachment = builder.getAttachment(support);
    }
    
    @Override
    public boolean execute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        super.execute(ref, role, sensorInfo, dt, store);
        setModelAttachment(ref, this.slot, this.attachment, store);
        return true;
    }
    
    private static void setModelAttachment(@Nonnull final Ref<EntityStore> ref, @Nonnull final String slot, @Nullable final String attachment, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (slot.isEmpty()) {
            throw new IllegalArgumentException("Slot must be specified!");
        }
        final ModelComponent modelComponent = componentAccessor.getComponent(ref, ModelComponent.getComponentType());
        assert modelComponent != null;
        final NPCEntity npcComponent = componentAccessor.getComponent(ref, NPCEntity.getComponentType());
        assert npcComponent != null;
        Model model = modelComponent.getModel();
        final float scale = model.getScale();
        final ModelAsset modelAsset = ModelAsset.getAssetMap().getAsset(model.getModelAssetId());
        final Map<String, String> randomAttachments = (model.getRandomAttachmentIds() != null) ? new HashMap<String, String>(model.getRandomAttachmentIds()) : new HashMap<String, String>();
        if (attachment == null || attachment.isEmpty()) {
            randomAttachments.remove(slot);
        }
        else {
            randomAttachments.put(slot, attachment);
        }
        model = Model.createScaledModel(modelAsset, scale, randomAttachments);
        componentAccessor.putComponent(ref, ModelComponent.getComponentType(), new ModelComponent(model));
        final Role role = npcComponent.getRole();
        if (role != null) {
            role.updateMotionControllers(ref, model, model.getBoundingBox(), componentAccessor);
        }
    }
}
