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

package com.hypixel.hytale.procedurallib.json;

import com.hypixel.hytale.procedurallib.logic.cell.CellType;
import com.hypixel.hytale.procedurallib.logic.cell.DistanceCalculationMode;
import com.hypixel.hytale.procedurallib.logic.ResultBuffer;
import javax.annotation.Nullable;
import com.hypixel.hytale.procedurallib.logic.cell.PointDistanceFunction;
import com.hypixel.hytale.procedurallib.property.NoiseProperty;
import com.hypixel.hytale.procedurallib.logic.CellNoise;
import com.hypixel.hytale.procedurallib.logic.cell.evaluator.PointEvaluator;
import com.hypixel.hytale.procedurallib.logic.cell.CellDistanceFunction;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import com.hypixel.hytale.procedurallib.NoiseFunction;

public class CellNoiseJsonLoader<K extends SeedResource> extends JsonLoader<K, NoiseFunction>
{
    public CellNoiseJsonLoader(@Nonnull final SeedString<K> seed, final Path dataFolder, final JsonElement json) {
        super(seed.append(".CellNoise"), dataFolder, json);
    }
    
    @Nonnull
    @Override
    public NoiseFunction load() {
        final CellDistanceFunction cellDistanceFunction = this.loadCellDistanceFunction();
        final PointEvaluator pointEvaluator = this.loadPointEvaluator();
        final CellNoise.CellFunction cellFunction = this.loadCellFunction();
        final NoiseProperty noiseLookup = this.loadNoiseLookup();
        return new LoadedCellNoise(cellDistanceFunction, pointEvaluator, cellFunction, noiseLookup, this.seed.get());
    }
    
    @Nullable
    protected CellDistanceFunction loadCellDistanceFunction() {
        return new CellDistanceFunctionJsonLoader(this.seed, this.dataFolder, this.json, null).load();
    }
    
    @Nullable
    protected PointEvaluator loadPointEvaluator() {
        return new PointEvaluatorJsonLoader(this.seed, this.dataFolder, this.json).load();
    }
    
    protected CellNoise.CellFunction loadCellFunction() {
        CellNoise.CellMode cellMode = Constants.DEFAULT_CELL_MODE;
        if (this.has("CellMode")) {
            cellMode = CellNoise.CellMode.valueOf(this.get("CellMode").getAsString());
        }
        return cellMode.getFunction();
    }
    
    @Nullable
    protected NoiseProperty loadNoiseLookup() {
        NoiseProperty noiseProperty = null;
        if (this.has("NoiseLookup")) {
            noiseProperty = new NoisePropertyJsonLoader(this.seed, this.dataFolder, this.get("NoiseLookup")).load();
        }
        return noiseProperty;
    }
    
    private static class LoadedCellNoise extends CellNoise
    {
        private final SeedResource seedResource;
        
        public LoadedCellNoise(final CellDistanceFunction cellDistanceFunction, final PointEvaluator pointEvaluator, final CellFunction cellFunction, @Nullable final NoiseProperty noiseLookup, final SeedResource seedResource) {
            super(cellDistanceFunction, pointEvaluator, cellFunction, noiseLookup);
            this.seedResource = seedResource;
        }
        
        @Nonnull
        @Override
        protected ResultBuffer.ResultBuffer2d localBuffer2d() {
            return this.seedResource.localBuffer2d();
        }
        
        @Nonnull
        @Override
        protected ResultBuffer.ResultBuffer3d localBuffer3d() {
            return this.seedResource.localBuffer3d();
        }
    }
    
    public interface Constants
    {
        public static final String KEY_JITTER = "Jitter";
        public static final String KEY_JITTER_X = "JitterX";
        public static final String KEY_JITTER_Y = "JitterY";
        public static final String KEY_JITTER_Z = "JitterZ";
        public static final String KEY_DENSITY = "Density";
        public static final String KEY_CELL_MODE = "CellMode";
        public static final String KEY_NOISE_LOOKUP = "NoiseLookup";
        public static final String KEY_DISTANCE_MODE = "DistanceMode";
        public static final String KEY_DISTANCE_RANGE = "DistanceRange";
        public static final String KEY_CELL_TYPE = "CellType";
        public static final String KEY_SKIP_CELLS = "Skip";
        public static final String KEY_SKIP_MODE = "SkipMode";
        public static final double DEFAULT_JITTER = 1.0;
        public static final double DEFAULT_DISTANCE_RANGE = 1.0;
        public static final double DEFAULT_DENSITY_LOWER = 0.0;
        public static final double DEFAULT_DENSITY_UPPER = 1.0;
        public static final DistanceCalculationMode DEFAULT_DISTANCE_MODE = DistanceCalculationMode.EUCLIDEAN;
        public static final CellNoise.CellMode DEFAULT_CELL_MODE = CellNoise.CellMode.CELL_VALUE;
        public static final CellType DEFAULT_CELL_TYPE = CellType.SQUARE;
    }
}
