package ca.recrown.islandsurvivalcraft.world;

import java.util.Random;

import org.bukkit.block.Biome;
import org.bukkit.util.noise.SimplexOctaveGenerator;

import ca.recrown.islandsurvivalcraft.utilities.biomes.BiomeClassifications;
import ca.recrown.islandsurvivalcraft.utilities.biomes.LandBiomeInfo;
import ca.recrown.islandsurvivalcraft.utilities.biomes.OceanBiomeInfo;

public class BiomeMap {
    private final SimplexOctaveGenerator noise;


    public BiomeMap(Random random) {
        this(random, 0.02d);
    }

    public BiomeMap(Random random, double scale) {
        noise = new SimplexOctaveGenerator(random, 2);
        noise.setScale(scale);
    }

    /**
     * Attempts to procure a biome that helps transitionfrom one biome to another biome
     * for given biome.
     * @param from the biome to transition from.
     * @return the resulting transition biome.
     */
    public Biome getTransitionalBiome(Biome from) {
        String biomeName = from.name().toLowerCase();
        if (biomeName.contains("jungle")) {
            if (biomeName.contains("modified")) {
                return Biome.MODIFIED_JUNGLE_EDGE;
            }
            return Biome.JUNGLE_EDGE;
        } else if (biomeName.contains("mountain")) {
            return null;
        }
        return null;
    }

    public BiomeClassifications getClassification(float temperature) {
        if (temperature <= 0.1f) {
            return BiomeClassifications.SNOWY;
        } else if (temperature <= 0.3f) {
            return BiomeClassifications.COLD;
        } else if (temperature <= 1f) {
            return BiomeClassifications.LUSH;
        } else {
            return BiomeClassifications.WARM;
        }
    }

    public LandBiomeInfo getLandBiomeInfo(float temperature, int worldX, int worldZ) {
        return getClassification(temperature).getSelector().selectRandom((float) noise.noise(worldX, worldZ, 0.5d, 0.5d, true));
    }

    /**
     * Randomly selects an ocean biome that fits in given temperature.
     * 
     * @param temperature Minecraft temperature to select biome from.
     * @param isDeep If this ocean should be of the deep variant.
     * @param seed The seed to use to select the biome.
     * @return The randomly selected biome.
     */
    public OceanBiomeInfo getOceanBiome(float temperature) {
        BiomeClassifications classification = getClassification(temperature);
        return classification.getAssociatedOceanBiomeInfo();
    }

}