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

package com.hypixel.hytale.builtin.adventure.npcobjectives.npc;

import java.util.List;
import com.hypixel.hytale.builtin.adventure.npcobjectives.NPCObjectivesPlugin;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent;
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.audiovisual.builders.BuilderActionPlayAnimation;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.builtin.adventure.npcobjectives.npc.builders.BuilderActionCompleteTask;
import com.hypixel.hytale.server.npc.corecomponents.audiovisual.ActionPlayAnimation;

public class ActionCompleteTask extends ActionPlayAnimation
{
    protected final boolean playAnimation;
    
    public ActionCompleteTask(@Nonnull final BuilderActionCompleteTask builder, @Nonnull final BuilderSupport support) {
        super(builder, support);
        this.playAnimation = builder.isPlayAnimation(support);
    }
    
    @Override
    public boolean canExecute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        final Ref<EntityStore> target = role.getStateSupport().getInteractionIterationTarget();
        final boolean targetExists = target != null && !store.getArchetype(target).contains(DeathComponent.getComponentType());
        return super.canExecute(ref, role, sensorInfo, dt, store) && targetExists;
    }
    
    @Override
    public boolean execute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        final UUIDComponent parentUuidComponent = store.getComponent(ref, UUIDComponent.getComponentType());
        assert parentUuidComponent != null;
        final Ref<EntityStore> targetPlayerReference = role.getStateSupport().getInteractionIterationTarget();
        if (targetPlayerReference == null) {
            return false;
        }
        final PlayerRef targetPlayerRefComponent = store.getComponent(targetPlayerReference, PlayerRef.getComponentType());
        if (targetPlayerRefComponent == null) {
            return false;
        }
        final List<String> activeTasks = role.getEntitySupport().getTargetPlayerActiveTasks();
        String animation = null;
        if (activeTasks != null) {
            for (int i = 0; i < activeTasks.size(); ++i) {
                animation = NPCObjectivesPlugin.updateTaskCompletion(store, targetPlayerReference, targetPlayerRefComponent, parentUuidComponent.getUuid(), activeTasks.get(i));
            }
        }
        if (this.playAnimation && animation != null) {
            this.setAnimationId(animation);
            super.execute(ref, role, sensorInfo, dt, store);
        }
        return true;
    }
}
