package electrosphere.game.client.terrain.cache;

import electrosphere.game.client.world.ClientWorldData;
import electrosphere.game.terrain.processing.TerrainInterpolator;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author amaterasu
 */
public class LoadingChunk {
    
    int worldX, worldY;
    int totalNumMessages;
    int numReceivedMessages = 0;
    
    ClientWorldData clientWorldData;
    
    float[][] macroValues;
    long[][] randomizer;
    List<ChunkModification> modification = new ArrayList();
    
    public LoadingChunk(int worldX, int worldY, int numMessages, ClientWorldData clientWorldData){
        this.worldX = worldX;
        this.worldY = worldY;
        this.totalNumMessages = numMessages;
        this.clientWorldData = clientWorldData;
    }
    
    public void addMacroValues(float[][] macroValues){
        this.macroValues = macroValues;
    }
    
    public void addRandomizer(long[][] randomizer){
        this.randomizer = randomizer;
    }
    
    public void addModification(int worldX, int worldY, int locationX, int locationY, float value){
//        System.out.println("Client add modification"); 
        ChunkModification newModification = new ChunkModification(worldX, worldY, locationX, locationY, value);
        modification.add(newModification);
    }
    
    public void incrementMessageCount(){
        numReceivedMessages++;
    }
    
    public boolean isComplete(){
        return numReceivedMessages >= totalNumMessages && macroValues != null && randomizer != null;
    }
    
    public float[][] exportFloats(){
        float[][] heightmap =  TerrainInterpolator.getBicubicInterpolatedChunk(
                macroValues,
                randomizer,
                clientWorldData.getDynamicInterpolationRatio(),
                clientWorldData.getRandomDampener()
        );
        if(modification != null){
            for(ChunkModification modification : modification){
//                System.out.println("Apply modification");
                heightmap = modification.applyModification(heightmap);
            }
        }
        return heightmap;
    }

    public int getWorldX() {
        return worldX;
    }

    public int getWorldY() {
        return worldY;
    }
    
    
    
}
