package electrosphere.server.block.editing;

import org.joml.Vector3i;

import electrosphere.client.block.BlockChunkData;
import electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.interfaces.VoxelCellManager;

/**
 * Provides utilities for editing block (particularly brushes, etc)
 */
public class ServerBlockEditing {

    /**
     * The minimum value before hard setting to 0
     */
    static final float MINIMUM_FULL_VALUE = 0.01f;
    
    /**
     * Performs a block chunk edit. Basically has a sphere around the provided position that it attempts to add value to
     * @param realm The realm to modify in
     * @param worldPos The world position
     * @param voxelPos The block position within the chunk at the world position
     * @param type The new type of block
     * @param metadata The new metadata for the block
     */
    public static void editBlockChunk(Realm realm, Vector3i worldPos, Vector3i voxelPos, short type, short metadata){
        if(realm != null && realm.getDataCellManager() instanceof VoxelCellManager){
            VoxelCellManager voxelCellManager = (VoxelCellManager) realm.getDataCellManager();

            BlockChunkData data;
            if(
                voxelPos.x < BlockChunkData.CHUNK_DATA_WIDTH &&
                voxelPos.y < BlockChunkData.CHUNK_DATA_WIDTH &&
                voxelPos.z < BlockChunkData.CHUNK_DATA_WIDTH &&
                voxelPos.x >= 0 &&
                voxelPos.y >= 0 &&
                voxelPos.z >= 0 &&
                (data = voxelCellManager.getBlocksAtPosition(worldPos)) != null
            ){
                data.setType(voxelPos.x, voxelPos.y, voxelPos.z, type);
                voxelCellManager.editBlock(worldPos, voxelPos, type, metadata);
            }
        }
    }

}
