package zero1hd.rhythmbullet.entity.enemies;

import java.util.HashMap;

import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;

import zero1hd.rhythmbullet.entity.Entity;

public class VoidCircle extends Entity {
	private float timer;
	private float endRadius;
	private float currentRadius;
	private float growthRate;
	private boolean begin;
	private float maxTime;
	private Sound sound;
	
	@Override
	public void preInit() {
		sprite = new Sprite(assets.get("void_circle.png", Texture.class));
		simple = false;
		enemy = true;
		sound = assets.get("disintegrate.ogg", Sound.class);
		super.preInit();
	}
	
	public void init(float endRadius, float x, float y, float growthRate, float warningTime) {
		timer = warningTime;
		maxTime = warningTime;
		this.endRadius = endRadius;
		setSize(2f*endRadius, 2f*endRadius);
		setPosition(x-getWidth()/2f, y-getHeight()/2f);
		this.growthRate = growthRate;
	}
	
	
	/**
	 * warningTime, endRadius, growthRate, x, y;
	 */
	@Override
	public void init(HashMap<String, Float> params) {
		timer = params.get("warningTime");
		maxTime = timer;
		endRadius = params.get("endRadius");
		setSize(2f*endRadius, 2f*endRadius);
		setPosition(params.get("x")-getWidth()/2f, params.get("y")-getHeight()/2f);
		growthRate = params.get("growthRate");
		super.init(params);
	}
	
	@Override
	public void act(float delta) {
		toFront();
		if (begin) {
			sprite.setSize(2*currentRadius, 2*currentRadius);
			sprite.setColor(0f,0f,0f,1f);
		} else {
			sprite.setSize(2*endRadius, 2*endRadius);
			sprite.setColor(1f,0.52f,0.32f,0.1f*(timer/maxTime));
		}
		
		if (timer > 0) {
			timer -= delta;
		} else {
			begin = true;
			if (currentRadius < endRadius) {
				growCurrentRadius(delta*growthRate);
			} else {
				endRadius = -1f;
				if (currentRadius > 0) {
					growCurrentRadius(delta*-growthRate);
				} else {
					dead = true;
				}
			}
		}
		super.act(delta);
	}
	
	@Override
	public void draw(Batch batch, float parentAlpha) {
		super.draw(batch, parentAlpha);
	}
	
	@Override
	public void reset() {
		currentRadius = 0;
		growthRate = 0;
		timer = 0;
		maxTime = 0;
		endRadius = 0;
		begin = false;
		setSize(0, 0);
		
		super.reset();
	}
	
	public void growCurrentRadius(float radius) {
		currentRadius += radius;
		float length = (float) Math.sqrt(2*(currentRadius*currentRadius));
		if (length < 0) {
			length = 0;
		}
		hitbox.setSize(length, length);
	}

	@Override
	public void collided(Entity entity) {
		if (begin) {
			sound.play(prefs.getFloat("fx vol"));
		}
	}
	
	@Override
	public boolean playCollidePFX() {
		return false;
	}
	
	@Override
	public boolean playCollideSFX() {
		return false;
	}
}
