package ca.recrown.islandsurvivalcraft.world;

import java.util.Random;

import org.bukkit.World;

import ca.recrown.islandsurvivalcraft.world.generation.chunks.IslandWorldChunkGenerator;

public class WorldInfo {
    private final WorldInfoManager manager;
	private volatile Random random;
    private volatile int seaLevel;
    private volatile int worldHeight;
    private volatile boolean initialized;
    private volatile long seed;
    private volatile BiomeMap biomeMap;
    private volatile TemperatureMap tempMap;
    private volatile IslandWorldMap islandMap;
    private volatile IslandWorldChunkGenerator generator;

    /**
     * Will initialize with the known information extracted from the given world if
     * world is not null. If the world is null, this world info will not be
     * initialized and awaits initialization from the generator.
     * 
     * @param world The world this object is describing. May be null to create a
     *              non-initialized info object.
     */
    public WorldInfo(WorldInfoManager manager, World world) {
        this.manager = manager;
        if (world != null) {
            initialize(world, null);
        }
    }

    public void initialize(World world, IslandWorldChunkGenerator chunkGenerator) {
        if (initialized)
            throw new IllegalStateException("This world information object has already been initialized.");
        this.initialized = true;
        this.seed = world.getSeed();
        this.random = new Random(seed);
        this.biomeMap = new BiomeMap(random);
        this.tempMap = new TemperatureMap(random);
        this.islandMap = new IslandWorldMap(random);
        this.worldHeight = world.getMaxHeight();
        this.seaLevel = world.getSeaLevel();
        if (chunkGenerator != null) {
            this.generator = chunkGenerator;
            this.generator.initialize();
            this.manager.register(world, this);
        } else {
            generator = new IslandWorldChunkGenerator(this);
        }
    }

    /**
     * @return whether or not this world info is initialized.
     */
    public boolean isInitialized() {
        return initialized;
    }

    /**
     * @return the biomeMap
     */
    public BiomeMap getBiomeMap() {
        return biomeMap;
    }

    /**
     * @return the generator
     */
    public IslandWorldChunkGenerator getGenerator() {
        return generator;
    }

    /**
     * @return the islandMap
     */
    public IslandWorldMap getIslandMap() {
        return islandMap;
    }

    /**
     * @return the manager
     */
    public WorldInfoManager getManager() {
        return manager;
    }

    /**
     * @return the random
     */
    public Random getRandom() {
        return random;
    }

    /**
     * @return the seaLevel
     */
    public int getSeaLevel() {
        return seaLevel;
    }

    /**
     * @return the seed
     */
    public long getSeed() {
        return seed;
    }

    /**
     * @return the tempMap
     */
    public TemperatureMap getTempMap() {
        return tempMap;
    }

    /**
     * @return the worldHeight
     */
    public int getWorldHeight() {
        return worldHeight;
    }
}