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

package com.hypixel.hytale.builtin.portals.interactions;

import com.hypixel.hytale.protocol.WaitForDataFrom;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.builtin.instances.InstancesPlugin;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import com.hypixel.hytale.builtin.portals.utils.CursedItems;
import com.hypixel.hytale.builtin.portals.resources.PortalWorld;
import com.hypixel.hytale.protocol.InteractionState;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.interaction.interaction.CooldownHandler;
import com.hypixel.hytale.math.vector.Vector3i;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.entity.InteractionContext;
import com.hypixel.hytale.protocol.InteractionType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import javax.annotation.Nonnull;
import java.time.Duration;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.client.SimpleBlockInteraction;

public class ReturnPortalInteraction extends SimpleBlockInteraction
{
    @Nonnull
    public static final Duration MINIMUM_TIME_IN_WORLD;
    @Nonnull
    public static final Duration WARNING_TIME;
    @Nonnull
    public static final BuilderCodec<ReturnPortalInteraction> CODEC;
    private static final Message MESSAGE_PORTALS_ATTUNING_TO_WORLD;
    private static final Message MESSAGE_PORTALS_DEVICE_NOT_IN_PORTAL_WORLD;
    
    @Override
    protected void interactWithBlock(@Nonnull final World world, @Nonnull final CommandBuffer<EntityStore> commandBuffer, @Nonnull final InteractionType type, @Nonnull final InteractionContext context, @Nullable final ItemStack itemInHand, @Nonnull final Vector3i targetBlock, @Nonnull final CooldownHandler cooldownHandler) {
        final Ref<EntityStore> ref = context.getEntity();
        final Player playerComponent = commandBuffer.getComponent(ref, Player.getComponentType());
        if (playerComponent == null) {
            context.getState().state = InteractionState.Failed;
            return;
        }
        final long elapsedNanosInWorld = playerComponent.getSinceLastSpawnNanos();
        if (elapsedNanosInWorld < ReturnPortalInteraction.MINIMUM_TIME_IN_WORLD.toNanos()) {
            if (elapsedNanosInWorld > ReturnPortalInteraction.WARNING_TIME.toNanos()) {
                playerComponent.sendMessage(ReturnPortalInteraction.MESSAGE_PORTALS_ATTUNING_TO_WORLD);
            }
            context.getState().state = InteractionState.Failed;
            return;
        }
        final PortalWorld portalWorld = commandBuffer.getResource(PortalWorld.getResourceType());
        if (!portalWorld.exists()) {
            playerComponent.sendMessage(ReturnPortalInteraction.MESSAGE_PORTALS_DEVICE_NOT_IN_PORTAL_WORLD);
            context.getState().state = InteractionState.Failed;
            return;
        }
        CursedItems.uncurseAll(playerComponent.getInventory().getCombinedEverything());
        InstancesPlugin.exitInstance(ref, commandBuffer);
    }
    
    @Override
    protected void simulateInteractWithBlock(@Nonnull final InteractionType type, @Nonnull final InteractionContext context, @Nullable final ItemStack itemInHand, @Nonnull final World world, @Nonnull final Vector3i targetBlock) {
        context.getState().state = InteractionState.Failed;
    }
    
    @Nonnull
    @Override
    public WaitForDataFrom getWaitForDataFrom() {
        return WaitForDataFrom.Server;
    }
    
    static {
        MINIMUM_TIME_IN_WORLD = Duration.ofSeconds(15L);
        WARNING_TIME = Duration.ofSeconds(4L);
        CODEC = BuilderCodec.builder(ReturnPortalInteraction.class, ReturnPortalInteraction::new, SimpleBlockInteraction.CODEC).build();
        MESSAGE_PORTALS_ATTUNING_TO_WORLD = Message.translation("server.portals.attuningToWorld");
        MESSAGE_PORTALS_DEVICE_NOT_IN_PORTAL_WORLD = Message.translation("server.portals.device.notInPortalWorld");
    }
}
