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

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

import com.hypixel.hytale.server.npc.util.IAnnotatedComponent;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.npc.movement.controllers.MotionController;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
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.BuilderActionWithDelay;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.utility.builders.BuilderActionTimeout;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.npc.instructions.Action;
import com.hypixel.hytale.server.npc.util.IAnnotatedComponentCollection;
import com.hypixel.hytale.server.npc.corecomponents.ActionWithDelay;

public class ActionTimeout extends ActionWithDelay implements IAnnotatedComponentCollection
{
    protected final boolean delayAfter;
    @Nullable
    protected final Action action;
    
    public ActionTimeout(@Nonnull final BuilderActionTimeout builderActionTimeout, @Nonnull final BuilderSupport builderSupport) {
        super(builderActionTimeout, builderSupport);
        this.action = builderActionTimeout.getAction(builderSupport);
        this.delayAfter = builderActionTimeout.isDelayAfter();
    }
    
    @Override
    public boolean canExecute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        if (!super.canExecute(ref, role, sensorInfo, dt, store) || (this.action != null && !this.action.canExecute(ref, role, sensorInfo, dt, store))) {
            return false;
        }
        if (!this.isDelaying() && this.isDelayPrepared()) {
            this.startDelay(role.getEntitySupport());
        }
        return !this.isDelaying();
    }
    
    @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);
        if (this.action != null) {
            this.action.execute(ref, role, sensorInfo, dt, store);
        }
        this.prepareDelay();
        return true;
    }
    
    @Override
    public void registerWithSupport(final Role role) {
        if (this.action != null) {
            this.action.registerWithSupport(role);
        }
        if (this.delayAfter) {
            this.clearDelay();
        }
        else {
            this.prepareDelay();
        }
    }
    
    @Override
    public void motionControllerChanged(@Nullable final Ref<EntityStore> ref, @Nonnull final NPCEntity npcComponent, final MotionController motionController, @Nullable final ComponentAccessor<EntityStore> componentAccessor) {
        if (this.action != null) {
            this.action.motionControllerChanged(ref, npcComponent, motionController, componentAccessor);
        }
    }
    
    @Override
    public void loaded(final Role role) {
        if (this.action != null) {
            this.action.loaded(role);
        }
    }
    
    @Override
    public void spawned(final Role role) {
        if (this.action != null) {
            this.action.spawned(role);
        }
    }
    
    @Override
    public void unloaded(final Role role) {
        if (this.action != null) {
            this.action.unloaded(role);
        }
    }
    
    @Override
    public void removed(final Role role) {
        if (this.action != null) {
            this.action.removed(role);
        }
    }
    
    @Override
    public void teleported(final Role role, final World from, final World to) {
        if (this.action != null) {
            this.action.teleported(role, from, to);
        }
    }
    
    @Override
    public void clearOnce() {
        super.clearOnce();
        if (this.delayAfter) {
            this.clearDelay();
        }
        else {
            this.prepareDelay();
        }
    }
    
    @Override
    public int componentCount() {
        return (this.action != null) ? 1 : 0;
    }
    
    @Nullable
    @Override
    public IAnnotatedComponent getComponent(final int index) {
        if (index >= this.componentCount()) {
            throw new IndexOutOfBoundsException();
        }
        return this.action;
    }
}
