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

package com.hypixel.hytale.procedurallib.json;

import com.hypixel.hytale.procedurallib.logic.cell.HexCellDistanceFunction;
import com.hypixel.hytale.procedurallib.logic.cell.GridCellDistanceFunction;
import com.hypixel.hytale.procedurallib.logic.cell.CellType;
import javax.annotation.Nullable;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import com.hypixel.hytale.procedurallib.logic.cell.PointDistanceFunction;
import com.hypixel.hytale.procedurallib.logic.cell.MeasurementMode;
import com.hypixel.hytale.procedurallib.logic.cell.CellDistanceFunction;

public class CellDistanceFunctionJsonLoader<K extends SeedResource> extends JsonLoader<K, CellDistanceFunction>
{
    protected final MeasurementMode measurementMode;
    protected final PointDistanceFunction pointDistanceFunction;
    
    public CellDistanceFunctionJsonLoader(@Nonnull final SeedString<K> seed, final Path dataFolder, final JsonElement json, @Nullable final PointDistanceFunction pointDistanceFunction) {
        this(seed, dataFolder, json, MeasurementMode.CENTRE_DISTANCE, pointDistanceFunction);
    }
    
    public CellDistanceFunctionJsonLoader(@Nonnull final SeedString<K> seed, final Path dataFolder, final JsonElement json, final MeasurementMode measurementMode, @Nullable final PointDistanceFunction pointDistanceFunction) {
        super(seed.append(".CellDistanceFunction"), dataFolder, json);
        this.measurementMode = measurementMode;
        this.pointDistanceFunction = pointDistanceFunction;
    }
    
    @Override
    public CellDistanceFunction load() {
        final CellDistanceFunction distanceFunction = this.loadDistanceFunction();
        return switch (this.measurementMode) {
            default -> throw new MatchException(null, null);
            case CENTRE_DISTANCE -> distanceFunction;
            case BORDER_DISTANCE -> new CellBorderDistanceFunctionJsonLoader(this.seed, this.dataFolder, this.json, distanceFunction).load();
        };
    }
    
    @Nonnull
    protected CellType loadCellType() {
        CellType cellType = CellNoiseJsonLoader.Constants.DEFAULT_CELL_TYPE;
        if (this.has("CellType")) {
            cellType = CellType.valueOf(this.get("CellType").getAsString());
        }
        return cellType;
    }
    
    @Nonnull
    protected CellDistanceFunction loadDistanceFunction() {
        return switch (this.loadCellType()) {
            default -> throw new MatchException(null, null);
            case SQUARE -> GridCellDistanceFunction.DISTANCE_FUNCTION;
            case HEX -> HexCellDistanceFunction.DISTANCE_FUNCTION;
        };
    }
}
