package zero1hd.rhythmbullet.entity;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool.PooledEffect;
import com.badlogic.gdx.utils.Array;

import zero1hd.rhythmbullet.RhythmBullet;

public class CollisionDetector {
	Array<Entity> enemies;
	Array<Entity> allies;
	
	AssetManager assets;
	Preferences prefs;
	
	//Particle pools;
	ParticleEffectPool explosionEffectPool;
	Array<PooledEffect> effects = new Array<>();
	
	private int amassedPoints;
	Sound explosionSFX;
	public CollisionDetector(Array<Entity> enemies, Array<Entity> allies, AssetManager assetManager, Preferences prefs) {
		this.enemies = enemies;
		this.allies = allies;
		assets = assetManager;
		this.prefs = prefs;
		
		explosionEffectPool = new ParticleEffectPool(assets.get("explosion-s.p", ParticleEffect.class), 1, 64);
		explosionSFX = assets.get("explosion.ogg", Sound.class);
	}
	
	public void collisionCheck() {
		if ((enemies.size != 0) && (allies.size != 0)) {
			for (int f = 0; f < enemies.size; f++) {
				Entity enemy = enemies.get(f);
				if (enemy.getHitZone() != null) {
					for (int s = 0; s < allies.size; s++) {
						Entity ally = allies.get(s);
						if (ally.getHitZone() != null) {
							if (enemy.getHitZone().overlaps(ally.getHitZone())) {
								Gdx.app.debug("Collision Detector", "Collision between entities: " + enemy.getClass().getSimpleName() + " and " + ally.getClass().getSimpleName());
								
								//Play FX;
								if (ally.playCollideSFX() && enemy.playCollideSFX()) {
									explosionSFX.play(prefs.getFloat("fx vol")/100f, 1f, (enemy.getX()/RhythmBullet.GAME_AREA_WIDTH));
								}
								if (ally.playCollidePFX() && enemy.playCollidePFX()) {
									PooledEffect currentPFX;
									currentPFX = explosionEffectPool.obtain();
									currentPFX.setPosition(ally.getX() + ally.getWidth()/2f, ally.getY() + ally.getHeight()/2f);
									effects.add(currentPFX);
								}
								
								enemy.collided(ally);
								ally.collided(enemy);
								
								amassedPoints += enemy.getPoints();
								break;
							}
						}
					}
				}
			}
		}
	}
	
	
	/**
	 * draws the explosion particles where necessary.
	 * @param batch the batch used to draw the said particles
	 * @param cleanBatch whether this method should call begin and end on the batch.
	 */
	public void renderParticles(Batch batch, float delta, boolean cleanBatch) {
		if (cleanBatch) {
			batch.begin();
		}
		for (int i = 0; i < effects.size; i++) {
			effects.get(i).draw(batch, delta);
			if (effects.get(i).isComplete()) {
				effects.get(i).free();
				effects.removeIndex(i);
			}
		}
		
		if (cleanBatch) {
			batch.end();
		}
	}
	
	public int getAmassedPoints() {
		int amassedPoints = this.amassedPoints;
		this.amassedPoints = 0;
		return amassedPoints;
	}
}
