package projectzombie.world.layer.layergen;

import java.util.Random;

import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec2i;
import gl_engine.vec.Vec3d;
import projectzombie.entity.EntityBoss;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Tiles;
import projectzombie.util.ColorRange;
import projectzombie.util.TileState;
import projectzombie.util.math.random.NoiseGenerator;
import projectzombie.util.math.random.NoiseGeneratorSimplex;
import projectzombie.util.math.random.RandomHelpers;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;

public class LayerGenBossArena extends LayerGen implements LayerGenRememberPlayerPos
{
	private final int size = 10;
	private Vec3d player_pos = new Vec3d(0, 0, 0);
	private Random rand = new Random();
	
	@Override
	public void init(Layer layer) {
		super.init(layer);
		
		layer.noise_gens = new NoiseGenerator[] {
				new NoiseGeneratorSimplex(rand),
		};
	}
	
	public LayerGenBossArena() {
		this.destroyOnLeave = true;
	}

	@Override
	public void generateChunk(Chunk chunk, Layer layer, Random rand, Vec2i pos)
	{
		// Fill with stone
		for(int i=0;i<Chunk.CHUNK_INDEX;i++) {
			chunk.setBackTile(Tiles.STONE.getDefaultState(), i);
		}
		
		for(int x=0;x<16;x++) {
			for(int y=0;y<16;y++)
			{
				// Get the tile position
				Vec2i tpos = new Vec2i(pos.x * 16 + x, pos.y * 16 + y);
				
				chunk.setTemperature(tpos, 0.8);
				chunk.setHumidity(tpos, 0);
				
				// Arena walls
				if(layer.noise_gens[0].eval(tpos.x / 8.0, tpos.y / 8.0) > tpos.distance(new Vec2i(0, 0)) / 10 - 1.25) {
					chunk.setBackTile(Tiles.STONE.getDefaultState(), tpos);
				}
				
				else {
					chunk.setBackTile(Tiles.WALL.getDefaultState(), tpos);
				}
				
				if(tpos.x == 0 && tpos.y == 0) {
					chunk.spawnEntity(new EntityBoss(new Vec3d(0, 0, 0), new Vec3d(0, 0, 0)));
				}
			}
		}
		
		int t = 16;
		for(int i=0;i<t;i++)
		{
			Vec2i lava_flow_pos = new Vec2i(
					RandomHelpers.randrange(rand, 16),
					RandomHelpers.randrange(rand, 16));
			
			if(
					chunk.getFrontTile(lava_flow_pos).tile == Tiles.VOID &&
					chunk.getBackTile(lava_flow_pos).tile == Tiles.STONE) {
				chunk.setFrontTile(Tiles.LAVA_FLOW.getDefaultState(), lava_flow_pos);
			}
		}
	}

	@Override
	public void spawnEntities(Layer layer, Random rand) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public TileState getTileDestroyed(Layer layer, Vec2i pos) {
		return Tiles.STONE.getDefaultState();
	}

	@Override
	public ColorRange getLightLevel() {
		return new ColorRange(new Vec3d(0, 0, 0), new Vec3d(0.08, 0.04, 0.02));
	}

	@Override
	public Vec3d getPlayerPos() {
		return player_pos;
	}

	@Override
	public void spawnPlayer(EntityPlayer player) {
		this.player_pos = player.getPos();
		player.setPos(new Vec3d(
				RandomHelpers.randrange(rand, -size + 2, size - 2), 0,
				RandomHelpers.randrange(rand, -size + 2, size - 2)));
	}
	
	@Override
	public double getTemperature(Layer layer, Vec2d pos) {
		return 0.8;
	}

}
