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

package com.hypixel.hytale.server.core.entity;

import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.asset.type.itemanimation.config.ItemPlayerAnimations;
import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import com.hypixel.hytale.server.core.universe.world.PlayerUtil;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.protocol.packets.entities.PlayAnimation;
import com.hypixel.hytale.server.core.modules.entity.tracker.NetworkId;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.component.ComponentAccessor;
import javax.annotation.Nullable;
import com.hypixel.hytale.protocol.AnimationSlot;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;

public class AnimationUtils
{
    public static void playAnimation(@Nonnull final Ref<EntityStore> ref, @Nonnull final AnimationSlot animationSlot, @Nullable final String animationId, final boolean sendToSelf, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        playAnimation(ref, animationSlot, null, animationId, sendToSelf, componentAccessor);
    }
    
    public static void playAnimation(@Nonnull final Ref<EntityStore> ref, @Nonnull final AnimationSlot animationSlot, @Nullable final String itemAnimationsId, @Nullable final String animationId, final boolean sendToSelf, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        Model model = null;
        final ModelComponent modelComponent = componentAccessor.getComponent(ref, ModelComponent.getComponentType());
        if (modelComponent != null) {
            model = modelComponent.getModel();
        }
        if (animationSlot != AnimationSlot.Action && animationId != null && model != null && !model.getAnimationSetMap().containsKey(animationId)) {
            Entity.LOGGER.at(Level.WARNING).atMostEvery(1, TimeUnit.MINUTES).log("Missing animation '%s' for Model '%s'", animationId, model.getModelAssetId());
            return;
        }
        final NetworkId networkIdComponent = componentAccessor.getComponent(ref, NetworkId.getComponentType());
        assert networkIdComponent != null;
        final PlayAnimation animationPacket = new PlayAnimation(networkIdComponent.getId(), itemAnimationsId, animationId, animationSlot);
        if (sendToSelf) {
            PlayerUtil.forEachPlayerThatCanSeeEntity(ref, (playerRef, playerRefComponent, ca) -> playerRefComponent.getPacketHandler().writeNoCache(animationPacket), componentAccessor);
        }
        else {
            PlayerUtil.forEachPlayerThatCanSeeEntity(ref, (playerRef, playerRefComponent, ca) -> playerRefComponent.getPacketHandler().writeNoCache(animationPacket), ref, componentAccessor);
        }
    }
    
    public static void playAnimation(@Nonnull final Ref<EntityStore> ref, @Nonnull final AnimationSlot animationSlot, @Nonnull final String itemAnimationsId, @Nonnull final String animationId, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        playAnimation(ref, animationSlot, itemAnimationsId, animationId, false, componentAccessor);
    }
    
    public static void playAnimation(@Nonnull final Ref<EntityStore> ref, @Nonnull final AnimationSlot animationSlot, @Nullable final ItemPlayerAnimations itemAnimations, @Nonnull final String animationId, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final String itemAnimationsId = (itemAnimations != null) ? itemAnimations.getId() : null;
        playAnimation(ref, animationSlot, itemAnimationsId, animationId, false, componentAccessor);
    }
    
    public static void stopAnimation(@Nonnull final Ref<EntityStore> ref, @Nonnull final AnimationSlot animationSlot, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        stopAnimation(ref, animationSlot, false, componentAccessor);
    }
    
    public static void stopAnimation(@Nonnull final Ref<EntityStore> ref, @Nonnull final AnimationSlot animationSlot, final boolean sendToSelf, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final NetworkId networkIdComponent = componentAccessor.getComponent(ref, NetworkId.getComponentType());
        assert networkIdComponent != null;
        final PlayAnimation animationPacket = new PlayAnimation(networkIdComponent.getId(), null, null, animationSlot);
        if (sendToSelf) {
            PlayerUtil.forEachPlayerThatCanSeeEntity(ref, (playerRef, playerRefComponent, ca) -> playerRefComponent.getPacketHandler().write(animationPacket), componentAccessor);
        }
        else {
            PlayerUtil.forEachPlayerThatCanSeeEntity(ref, (playerRef, playerRefComponent, ca) -> playerRefComponent.getPacketHandler().write(animationPacket), ref, componentAccessor);
        }
    }
    
    public static void playAnimation(@Nonnull final Ref<EntityStore> ref, @Nonnull final AnimationSlot animationSlot, @Nullable final String animationId, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        playAnimation(ref, animationSlot, animationId, false, componentAccessor);
    }
}
