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

package com.hypixel.hytale.builtin.adventure.teleporter.system;

import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.ComponentType;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.Rotation;
import com.hypixel.hytale.server.core.universe.world.chunk.section.BlockSection;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.builtin.teleport.TeleportPlugin;
import com.hypixel.hytale.builtin.teleport.Warp;
import java.time.Instant;
import com.hypixel.hytale.math.vector.Transform;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.RotationTuple;
import com.hypixel.hytale.math.util.ChunkUtil;
import java.util.UUID;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.builtin.adventure.teleporter.component.Teleporter;
import com.hypixel.hytale.builtin.adventure.teleporter.util.CannedWarpNames;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.modules.block.BlockModule;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.modules.interaction.components.PlacedByInteractionComponent;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.component.system.RefChangeSystem;

public class CreateWarpWhenTeleporterPlacedSystem extends RefChangeSystem<ChunkStore, PlacedByInteractionComponent>
{
    @Override
    public void onComponentAdded(@Nonnull final Ref<ChunkStore> ref, @Nonnull final PlacedByInteractionComponent placedBy, @Nonnull final Store<ChunkStore> chunkStore, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
        final World world = chunkStore.getExternalData().getWorld();
        final EntityStore entityStore = world.getEntityStore();
        final UUID whoPlacedUuid = placedBy.getWhoPlacedUuid();
        final Ref<EntityStore> whoPlacedRef = entityStore.getRefFromUUID(whoPlacedUuid);
        if (whoPlacedRef == null || !whoPlacedRef.isValid()) {
            return;
        }
        final PlayerRef playerRefComponent = entityStore.getStore().getComponent(whoPlacedRef, PlayerRef.getComponentType());
        final String language = (playerRefComponent == null) ? null : playerRefComponent.getLanguage();
        if (language == null) {
            return;
        }
        final BlockModule.BlockStateInfo blockStateInfoComponent = commandBuffer.getComponent(ref, BlockModule.BlockStateInfo.getComponentType());
        assert blockStateInfoComponent != null;
        final Ref<ChunkStore> chunkRef = blockStateInfoComponent.getChunkRef();
        if (chunkRef == null || !chunkRef.isValid()) {
            return;
        }
        final WorldChunk worldChunk = chunkStore.getComponent(chunkRef, WorldChunk.getComponentType());
        if (worldChunk == null) {
            return;
        }
        final String cannedName = CannedWarpNames.generateCannedWarpName(ref, language);
        if (cannedName == null) {
            return;
        }
        createWarp(worldChunk, blockStateInfoComponent, cannedName);
        final Teleporter teleporterComponent = commandBuffer.getComponent(ref, Teleporter.getComponentType());
        assert teleporterComponent != null;
        teleporterComponent.setOwnedWarp(cannedName);
    }
    
    public static void createWarp(@Nonnull final WorldChunk worldChunk, @Nonnull final BlockModule.BlockStateInfo blockStateInfo, @Nonnull final String name) {
        final int chunkBlockX = worldChunk.getX() << 5;
        final int chunkBlockZ = worldChunk.getZ() << 5;
        final int index = blockStateInfo.getIndex();
        final int x = chunkBlockX + ChunkUtil.xFromBlockInColumn(index);
        final int y = ChunkUtil.yFromBlockInColumn(index);
        final int z = chunkBlockZ + ChunkUtil.zFromBlockInColumn(index);
        final BlockChunk blockChunkComponent = worldChunk.getBlockChunk();
        assert blockChunkComponent != null;
        final BlockSection section = blockChunkComponent.getSectionAtBlockY(y);
        final int rotationIndex = section.getRotationIndex(x, y, z);
        final RotationTuple rotationTuple = RotationTuple.get(rotationIndex);
        final Rotation rotationYaw = rotationTuple.yaw();
        final float warpRotationYaw = (float)rotationYaw.getRadians() + (float)Math.toRadians(180.0);
        final Vector3d warpPosition = new Vector3d(x, y, z).add(0.5, 0.65, 0.5);
        final Transform warpTransform = new Transform(warpPosition, new Vector3f(Float.NaN, warpRotationYaw, Float.NaN));
        final String warpId = name.toLowerCase();
        final Warp warp = new Warp(warpTransform, name, worldChunk.getWorld(), "*Teleporter", Instant.now());
        final TeleportPlugin teleportPlugin = TeleportPlugin.get();
        teleportPlugin.getWarps().put(warpId, warp);
        teleportPlugin.saveWarps();
    }
    
    @Override
    public void onComponentSet(@Nonnull final Ref<ChunkStore> ref, @Nullable final PlacedByInteractionComponent oldComponent, @Nonnull final PlacedByInteractionComponent newComponent, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
    }
    
    @Override
    public void onComponentRemoved(@Nonnull final Ref<ChunkStore> ref, @Nonnull final PlacedByInteractionComponent component, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
    }
    
    @Nonnull
    @Override
    public ComponentType<ChunkStore, PlacedByInteractionComponent> componentType() {
        return PlacedByInteractionComponent.getComponentType();
    }
    
    @Nullable
    @Override
    public Query<ChunkStore> getQuery() {
        return (Query<ChunkStore>)Query.and(PlacedByInteractionComponent.getComponentType(), Teleporter.getComponentType(), BlockModule.BlockStateInfo.getComponentType());
    }
}
