// 
// 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.server.npc.movement.controllers.MotionController;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import java.util.Objects;
import com.hypixel.hytale.server.npc.movement.Steering;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.component.ComponentAccessor;
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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.utility.builders.BuilderMotionSequence;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.npc.util.IAnnotatedComponentCollection;
import com.hypixel.hytale.server.npc.corecomponents.MotionBase;
import com.hypixel.hytale.server.npc.instructions.Motion;

public abstract class MotionSequence<T extends Motion> extends MotionBase implements IAnnotatedComponentCollection
{
    protected final boolean looped;
    protected final boolean restartOnActivate;
    protected final T[] steps;
    protected boolean finished;
    protected int index;
    @Nullable
    protected T activeMotion;
    
    public MotionSequence(@Nonnull final BuilderMotionSequence<T> builder, final T[] steps) {
        this.restart();
        this.looped = builder.isLooped();
        this.restartOnActivate = builder.isRestartOnActivate();
        this.steps = steps;
    }
    
    @Override
    public void activate(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (this.restartOnActivate) {
            this.deactivate(ref, role, componentAccessor);
            this.restart();
        }
        if (this.finished) {
            return;
        }
        this.doActivate(ref, role, componentAccessor);
    }
    
    @Override
    public void deactivate(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (this.activeMotion != null) {
            this.activeMotion.deactivate(ref, role, componentAccessor);
            this.activeMotion = null;
        }
    }
    
    @Override
    public boolean computeSteering(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nullable final InfoProvider sensorInfo, final double dt, @Nonnull final Steering desiredSteering, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (this.finished) {
            desiredSteering.clear();
            return false;
        }
        final T currentActiveMotion = this.activeMotion;
        do {
            Objects.requireNonNull(this.activeMotion, "Active motion not set");
            if (this.activeMotion.computeSteering(ref, role, sensorInfo, dt, desiredSteering, componentAccessor)) {
                return true;
            }
            if (this.index + 1 < this.steps.length) {
                this.activateNext(ref, this.index + 1, role, componentAccessor);
            }
            else {
                if (!this.looped) {
                    break;
                }
                this.activateNext(ref, 0, role, componentAccessor);
            }
        } while (this.activeMotion != currentActiveMotion);
        this.deactivate(ref, role, componentAccessor);
        this.finished = true;
        return false;
    }
    
    @Override
    public void registerWithSupport(final Role role) {
        for (final T step : this.steps) {
            step.registerWithSupport(role);
        }
    }
    
    @Override
    public void motionControllerChanged(@Nullable final Ref<EntityStore> ref, @Nonnull final NPCEntity npcComponent, final MotionController motionController, @Nullable final ComponentAccessor<EntityStore> componentAccessor) {
        for (final T step : this.steps) {
            step.motionControllerChanged(ref, npcComponent, motionController, componentAccessor);
        }
    }
    
    @Override
    public void loaded(final Role role) {
        for (final T step : this.steps) {
            step.loaded(role);
        }
    }
    
    @Override
    public void spawned(final Role role) {
        for (final T step : this.steps) {
            step.spawned(role);
        }
    }
    
    @Override
    public void unloaded(final Role role) {
        for (final T step : this.steps) {
            step.unloaded(role);
        }
    }
    
    @Override
    public void removed(final Role role) {
        for (final T step : this.steps) {
            step.removed(role);
        }
    }
    
    @Override
    public void teleported(final Role role, final World from, final World to) {
        for (final T step : this.steps) {
            step.teleported(role, from, to);
        }
    }
    
    @Override
    public int componentCount() {
        return this.steps.length;
    }
    
    @Override
    public IAnnotatedComponent getComponent(final int index) {
        return this.steps[index];
    }
    
    @Override
    public void setContext(final IAnnotatedComponent parent, final int index) {
        for (int i = 0; i < this.steps.length; ++i) {
            this.steps[i].setContext(parent, i);
        }
    }
    
    public void restart() {
        this.index = 0;
        this.finished = false;
    }
    
    protected void doActivate(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (this.steps.length == 0) {
            throw new IllegalArgumentException("Motion sequence must have steps!");
        }
        if (this.index < 0 || this.index >= this.steps.length) {
            throw new IndexOutOfBoundsException(String.format("Motion sequence index out of range (%s) must be less than size (%s)", this.index, this.steps.length));
        }
        Objects.requireNonNull(this.activeMotion = this.steps[this.index], "Active motion must not be null");
        this.activeMotion.activate(ref, role, componentAccessor);
    }
    
    protected void activateNext(@Nonnull final Ref<EntityStore> ref, final int newIndex, @Nonnull final Role role, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        this.activeMotion.deactivate(ref, role, componentAccessor);
        this.index = newIndex;
        this.doActivate(ref, role, componentAccessor);
    }
}
