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

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

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 com.hypixel.hytale.server.npc.movement.Steering;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.math.random.RandomExtra;
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 com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.timer.builders.BuilderMotionTimer;
import com.hypixel.hytale.server.npc.corecomponents.MotionBase;
import com.hypixel.hytale.server.npc.instructions.Motion;

public abstract class MotionTimer<T extends Motion> extends MotionBase
{
    protected final T motion;
    protected final double atLeastSeconds;
    protected final double atMostSeconds;
    protected double activeTime;
    protected double timeToLive;
    
    public MotionTimer(@Nonnull final BuilderMotionTimer<T> builder, @Nonnull final BuilderSupport builderSupport, final T motion) {
        final double[] timerRange = builder.getTimerRange(builderSupport);
        this.atLeastSeconds = timerRange[0];
        this.atMostSeconds = timerRange[1];
        this.motion = motion;
    }
    
    @Override
    public void activate(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        this.activeTime = 0.0;
        this.timeToLive = RandomExtra.randomRange(this.atLeastSeconds, this.atMostSeconds);
        this.motion.activate(ref, role, componentAccessor);
    }
    
    @Override
    public void deactivate(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        this.motion.deactivate(ref, role, componentAccessor);
    }
    
    @Override
    public boolean computeSteering(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role support, @Nullable final InfoProvider sensorInfo, final double dt, @Nonnull final Steering desiredSteering, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (this.activeTime >= this.timeToLive) {
            return false;
        }
        this.activeTime += dt;
        if (!this.motion.computeSteering(ref, support, sensorInfo, dt, desiredSteering, componentAccessor)) {
            this.activeTime = this.timeToLive;
            return false;
        }
        return true;
    }
    
    @Override
    public void registerWithSupport(final Role role) {
        this.motion.registerWithSupport(role);
    }
    
    @Override
    public void motionControllerChanged(@Nullable final Ref<EntityStore> ref, @Nonnull final NPCEntity npcComponent, final MotionController motionController, @Nullable final ComponentAccessor<EntityStore> componentAccessor) {
        this.motion.motionControllerChanged(ref, npcComponent, motionController, componentAccessor);
    }
    
    @Override
    public void loaded(final Role role) {
        this.motion.loaded(role);
    }
    
    @Override
    public void spawned(final Role role) {
        this.motion.spawned(role);
    }
    
    @Override
    public void unloaded(final Role role) {
        this.motion.unloaded(role);
    }
    
    @Override
    public void removed(final Role role) {
        this.motion.removed(role);
    }
    
    @Override
    public void teleported(final Role role, final World from, final World to) {
        this.motion.teleported(role, from, to);
    }
}
