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

package com.hypixel.hytale.builtin.beds.respawn;

import com.hypixel.hytale.protocol.packets.interface_.Page;
import com.hypixel.hytale.server.core.entity.entities.player.data.PlayerRespawnPointData;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.ui.builder.EventData;
import com.hypixel.hytale.protocol.packets.interface_.CustomUIEventBindingType;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.ui.builder.UIEventBuilder;
import com.hypixel.hytale.server.core.ui.builder.UICommandBuilder;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.protocol.InteractionType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.meta.state.RespawnBlock;
import com.hypixel.hytale.math.vector.Vector3i;

public class SetNameRespawnPointPage extends RespawnPointPage
{
    private final Vector3i respawnBlockPosition;
    private final RespawnBlock respawnBlock;
    
    public SetNameRespawnPointPage(@Nonnull final PlayerRef playerRef, final InteractionType interactionType, final Vector3i respawnBlockPosition, final RespawnBlock respawnBlock) {
        super(playerRef, interactionType);
        this.respawnBlockPosition = respawnBlockPosition;
        this.respawnBlock = respawnBlock;
    }
    
    @Override
    public void build(@Nonnull final Ref<EntityStore> ref, @Nonnull final UICommandBuilder commandBuilder, @Nonnull final UIEventBuilder eventBuilder, @Nonnull final Store<EntityStore> store) {
        commandBuilder.append("Pages/NameRespawnPointPage.ui");
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
        assert playerRefComponent != null;
        final World world = store.getExternalData().getWorld();
        final PlayerRespawnPointData[] respawnPoints = playerComponent.getPlayerConfigData().getPerWorldData(world.getName()).getRespawnPoints();
        String respawnPointName = null;
        if (respawnPoints != null) {
            for (final PlayerRespawnPointData respawnPoint : respawnPoints) {
                if (respawnPoint.getBlockPosition().equals(this.respawnBlockPosition)) {
                    respawnPointName = respawnPoint.getName();
                    break;
                }
            }
        }
        if (respawnPointName == null) {
            commandBuilder.set("#NameInput.Value", Message.translation("server.customUI.defaultRespawnPointName").param("name", playerRefComponent.getUsername()));
        }
        else {
            commandBuilder.set("#NameInput.Value", respawnPointName);
        }
        eventBuilder.addEventBinding(CustomUIEventBindingType.Activating, "#SetButton", EventData.of("@RespawnPointName", "#NameInput.Value"));
        eventBuilder.addEventBinding(CustomUIEventBindingType.Activating, "#CancelButton", EventData.of("Action", "Cancel"));
    }
    
    @Override
    public void handleDataEvent(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store, @Nonnull final RespawnPointEventData data) {
        final String respawnPointName = data.getRespawnPointName();
        if (respawnPointName != null) {
            this.setRespawnPointForPlayer(ref, store, this.respawnBlockPosition, this.respawnBlock, respawnPointName, new PlayerRespawnPointData[0]);
        }
        else if ("Cancel".equals(data.getAction())) {
            final Player playerComponent = store.getComponent(ref, Player.getComponentType());
            playerComponent.getPageManager().setPage(ref, store, Page.None);
        }
    }
}
