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

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

import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nullable;
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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.world.builders.BuilderActionSetLeashPosition;
import com.hypixel.hytale.server.npc.corecomponents.ActionBase;

public class ActionSetLeashPosition extends ActionBase
{
    protected final boolean toTarget;
    protected final boolean toCurrent;
    
    public ActionSetLeashPosition(@Nonnull final BuilderActionSetLeashPosition builderActionSetLeashPosition) {
        super(builderActionSetLeashPosition);
        this.toCurrent = builderActionSetLeashPosition.isToCurrent();
        this.toTarget = builderActionSetLeashPosition.isToTarget();
    }
    
    @Override
    public boolean execute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nullable final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        super.execute(ref, role, sensorInfo, dt, store);
        if (this.toCurrent) {
            setLeashPosition(ref, ref, store);
        }
        else if (this.toTarget && sensorInfo != null) {
            final Ref<EntityStore> targetRef = sensorInfo.hasPosition() ? sensorInfo.getPositionProvider().getTarget() : null;
            if (targetRef != null) {
                setLeashPosition(ref, targetRef, store);
            }
        }
        return true;
    }
    
    protected static void setLeashPosition(@Nonnull final Ref<EntityStore> ref, @Nonnull final Ref<EntityStore> targetRef, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final NPCEntity selfNpcComponent = componentAccessor.getComponent(ref, NPCEntity.getComponentType());
        assert selfNpcComponent != null;
        final TransformComponent entityTransformComponent = componentAccessor.getComponent(targetRef, TransformComponent.getComponentType());
        assert entityTransformComponent != null;
        final Vector3f entityBodyRotation = entityTransformComponent.getRotation();
        selfNpcComponent.getLeashPoint().assign(entityTransformComponent.getPosition());
        selfNpcComponent.setLeashPitch(entityBodyRotation.getPitch());
        selfNpcComponent.setLeashHeading(entityBodyRotation.getYaw());
    }
}
