package projectzombie.world.layer.layergen;

import java.util.Random;

import gl_engine.MathHelpers;
import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec2i;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.entity.Entity;
import projectzombie.entity.EntityZombie;
import projectzombie.init.Tiles;
import projectzombie.tiles.Tile;
import projectzombie.time.GameTimer;
import projectzombie.util.math.ColorRange;
import projectzombie.util.math.TileState;
import projectzombie.util.math.random.NoiseGenerator;
import projectzombie.util.math.random.NoiseGeneratorSimplex;
import projectzombie.util.math.random.RandomHelpers;
import projectzombie.world.World;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;

public class LayerGenEarth extends LayerGen
{
	@Override
	public Vec3d getWind(Layer layer, Vec2d pos) {
		double t = GameTimer.getTime() / 1000.0;
		return new Vec3d(
				layer.noise_gens[2].eval(pos.x / 64.0, pos.y / 64.0, t) * 0.02, 0,
				layer.noise_gens[3].eval(pos.x / 64.0, pos.y / 64.0, t) * 0.02);
	}

	@Override
	public double getTemperatureStatic(Layer layer, Vec2d pos)
	{
		// Get the noise generator
		NoiseGenerator terrain_noise = layer.noise_gens[0];
		return MathHelpers.map(terrain_noise.eval(pos.x / World.BIOME_SIZE, pos.y / World.BIOME_SIZE), -1, 1, 0, 1);
	}
	
	@Override
	public double getTemperatureDynamic(Layer layer, Vec2d pos)
	{
		// Get the noise generator
		double humidity = getHumidity(layer, pos);
		double light = MathHelpers.map(
				MathHelpers.sin(GameTimer.getTime() / 14400.0 - MathHelpers.PI / 4),
				-1, 1, 0.2 * humidity, 0.2);
		NoiseGenerator terrain_noise = layer.noise_gens[0];
		return MathHelpers.map(terrain_noise.eval(pos.x / World.BIOME_SIZE, pos.y / World.BIOME_SIZE), -1, 1, 0, 0.5 + light);
	}
	
	@Override
	public double getHumidity(Layer layer, Vec2d pos)
	{
		// Get the noise generator
		NoiseGenerator terrain_noise = layer.noise_gens[1];
		return MathHelpers.map(terrain_noise.eval(pos.x / World.BIOME_SIZE, pos.y / World.BIOME_SIZE), -1, 1, 0, 1);
	}
	
	@Override
	public void init(Layer layer) {
		super.init(layer);
		
		Random rand = new Random(layer.seed);
		Random lrand = new Random(layer.lseed);
		
		layer.noise_gens = new NoiseGenerator[]
		{
				new NoiseGeneratorSimplex(rand.nextLong()),  // Temperature
				new NoiseGeneratorSimplex(rand.nextLong()),  // Humidity
				
				new NoiseGeneratorSimplex(lrand.nextLong()), // Wind
				new NoiseGeneratorSimplex(lrand.nextLong()), // Wind
				new NoiseGeneratorSimplex(lrand.nextLong()), // Water
				new NoiseGeneratorSimplex(lrand.nextLong()), // Rocks
		};
	}
	
	@Override
	public void generateChunk(Chunk chunk, Layer layer, Random rand, Vec2i c_pos)
	{
		// Is there going to be a portal in this chunk
		boolean portal = PortalSpawnrates.WorldCavePortal(rand);
		Vec2i portal_pos = null;
		if(portal) portal_pos = new Vec2i(
				RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx),
				RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my));
		
		// Loop over the layer dimensions and create land
		for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) {
			for(int y=0;y<Chunk.CHUNK_SIZE.my;y++)
			{
				Vec2d pos = new Vec2d(c_pos.x * 16 + x, c_pos.y * 16 + y);
				
				double temperature = getTemperatureStatic(layer, pos);
				double humidity = getHumidity(layer, pos);
				
				if(layer.noise_gens[4].eval(pos.x / 64.0, pos.y / 64.0) > (1 - humidity) * 1.5) {
					chunk.setFrontTile(Tiles.WATER.getDefaultState(), pos.toInt());
				}
				
				else
				{
					byte rock_type = 0;
					
					if(temperature < 0.35)
					{
						chunk.setBackTile(Tiles.SNOW.getDefaultState(), pos.toInt());
						rock_type = (byte)1;
						
						if(rand.nextDouble() > MathHelpers.map(humidity, 0, 1, 0.99, 0.8)) {
							chunk.setFrontTile(new TileState(Tiles.TREE, (byte)1), pos.toInt());
						}
					}
					
					else if(temperature > 0.65 && humidity < 0.5)
					{
						chunk.setBackTile(Tiles.SAND.getDefaultState(), pos.toInt());
						rock_type = (byte)2;
						
						if(rand.nextDouble() > MathHelpers.map(humidity, 0, 0.5, 0.98, 0.95)) {
							chunk.setFrontTile(Tiles.CACTUS.getDefaultState(), pos.toInt());
						}
					}
					
					else
					{
						chunk.setBackTile(Tiles.GRASS.getDefaultState(), pos.toInt());
						
						if(rand.nextDouble() > MathHelpers.map(humidity, 0, 1, 0.99, 0.9)) {
							chunk.setFrontTile(Tiles.TREE.getDefaultState(), pos.toInt());
						}
						
						if(rand.nextDouble() > 0.9) {
							chunk.setFrontTile(Tiles.TALL_GRASS.getDefaultState(), pos.toInt());
						}
						
						if(temperature > 0.7 && humidity > 0.7 && Math.random() > 0.9998) {
							chunk.setFrontTile(Tiles.HEMP.getDefaultState(), pos.toInt());
							while(Math.random() < 0.8) {
								chunk.setFrontTile(Tiles.HEMP.getDefaultState(),
										pos.toInt().add(new Vec2d(
										MathHelpers.map(Math.random(), 0, 1, -2, 3),
										MathHelpers.map(Math.random(), 0, 1, -2, 3)).toInt()));
							}
						}
					}
					
					if(rand.nextDouble() > 0.98) {
						chunk.setFrontTile(new TileState(Tiles.ROCK, rock_type), pos.toInt());
					}
				}
			}
		}
		
		if(portal) {
			chunk.setBackTile(new TileState(Tiles.PORTAL_DOWN, (short)1), portal_pos);
			chunk.setFrontTile(Tiles.LADDER.getDefaultState(), portal_pos);
			//chunk.setFrontTile(Tiles.BOSS_PORTAL.getDefaultState(), portal_pos);
		}
	}

	@Override
	public void spawnEntities(Layer layer, Random rand)
	{
		if(rand.nextDouble() > 0.98 && getEarthLight() < -0.3)
		{
			Vec3d ppos = Main.player.getPos();
			
			Entity zombie = new EntityZombie(new Vec3d(
					RandomHelpers.randrange(rand, (int)ppos.x - 128, (int)ppos.x + 128), 0,
					RandomHelpers.randrange(rand, (int)ppos.y - 128, (int)ppos.y + 128)),
					new Vec3d(0, 0, 0));
			
			boolean exists = false;
			
			for(Entity e : layer.getChunk(zombie.getPos().xz()).entities) {
				if(e.getClass() == zombie.getClass()) {
					exists = true;
					break;
				}
			}
			
			if(!exists && zombie.getPos().squareDistance(ppos) > 64) {
				layer.spawnEntity(zombie);
			}
		}
	}
	
	@Override
	public TileState getTileDestroyed(Layer layer, Vec2i pos)
	{
		double temperature = getTemperatureStatic(layer, pos.toDouble());
		double humidity = getHumidity(layer, pos.toDouble());
		
		if(temperature < 0.3) return Tiles.SNOW.getDefaultState();
		if(temperature > 0.7 && humidity < 0.5) return Tiles.SAND.getDefaultState();
		
		return Tiles.DIRT.getDefaultState();
	}
	
	private double getEarthLight() {
		return MathHelpers.sin(((MathHelpers.TWO_PI * (GameTimer.getTime() % 720000)) / 72000.0) + MathHelpers.PI/4);
	}
	
	@Override
	public boolean hasSun() {
		return true;
	}
	
	@Override
	public double getSunPosition() {
		return (GameTimer.getTime() % 720000) / 200.0 + 45;
	}
	
	@Override
	public ColorRange getLightLevel()
	{
		double light = getEarthLight();
		
		ColorRange daylightRange = new ColorRange(
				new Vec3d(31/255.0, 15/255.0, 0),
				new Vec3d(205/255.0, 191/255.0, 162/255.0));
		
		Vec3d daylight = daylightRange.getColor(light);
		return new ColorRange(daylight.multiply(7/8.0), daylight);
	}
}
