package com.snapgames.s2de.core.fx.particles.emitter;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import com.snapgames.s2de.Game;
import com.snapgames.s2de.core.collider.BoundingBox;
import com.snapgames.s2de.core.components.Component;
import com.snapgames.s2de.core.entities.GameEntity;
import com.snapgames.s2de.core.fx.particles.Particle;
import com.snapgames.s2de.core.system.math.Vector2D;

/**
 * This the basic for all Emitter. only th emit() and the update() methods can
 * differ from on eEmitter to Another.
 * 
 * @author Frédéric Delorme
 *
 */
public abstract class AbstractEmitter extends GameEntity {

	/**
	 * List of particles for this emitter.
	 */
	public List<Particle> particles = new ArrayList<Particle>();
	public List<Component> partBehaviors = new ArrayList<Component>();
	public int maxNbParticles = 200;
	public int nbEmittedParticles = 10;

	public abstract void update(float dt, Rectangle viewport);

	public abstract List<Particle> emit(float x, float y, int nb, List<Component> behaviors);

	public int particleLife = 100;
	public int particleExpireTime = 15;
	// public BoundingBox boundingBox = new BoundingBox(0, 0, 0, 0);

	public AbstractEmitter(Game g, String name, Vector2D position, int width, int height, float rotation) {
		super(g, name, position, width, height, rotation);
	}

	public AbstractEmitter(Game g, String name, Vector2D pos, int w, int h, int r, Vector2D s, Vector2D a) {
		super(g, name, pos, w, h, r, s, a);
	}

	public AbstractEmitter(Game g, String name) {
		super(g, name);
	}

	public AbstractEmitter(Game g, String name, Component... components) {
		super(g, name, components);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.snapgames.s2de.sample.entities.GameEntity#render(java.awt.Graphics2D)
	 */
	@Override
	public void render(Graphics2D g) {

		for (Particle particle : particles) {
			particle.render(g);
		}
		if (game.debug) {
			if (game.drawBoundingBox) {
				g.setColor(Color.YELLOW);
				g.drawRect((int) (boundingBox.x - offset.x), (int) (boundingBox.y - offset.y), (int) (boundingBox.w),
						(int) (boundingBox.h));
			}
			g.setColor(Color.BLUE);
			g.fillArc((int) (position.x - offset.x), (int) (position.y - offset.y), 8, 8, 0, 360);
			g.setColor(Color.YELLOW);
			String text = String.format("p:%04d/M:%04d", nbEmittedParticles, maxNbParticles);
			g.drawString(name, position.x + 20, position.y);
			g.drawString(text, position.x + 20, position.y + 20);
			// showMetaData(g, position.x, position.y);
		}
	}

	/**
	 * Compute bounding box with a new particle.
	 * 
	 * @param p
	 */
	protected void computeBoundingBox(Particle p, int i) {
		if (i == 0) {
			boundingBox = new BoundingBox(0, 0, 0, 0);
		}
		if (boundingBox.x == 0 && boundingBox.x == 0 && boundingBox.w == 0 && boundingBox.h == 0) {
			boundingBox.x = p.position.x;
			boundingBox.y = p.position.y;
			boundingBox.w = p.width;
			boundingBox.h = p.height;
		}
		if (p.position.x < boundingBox.x) {
			boundingBox.x = p.position.x;
		}
		if (p.position.y < boundingBox.y) {
			boundingBox.y = p.position.y;
		}
		if (p.position.x + p.width > boundingBox.x + boundingBox.w
		// || (p.position.x < boundingBox.x + boundingBox.w && p.position.x >
		// boundingBox.x)
		) {
			boundingBox.w = p.position.x + p.width - boundingBox.x;
		}
		if (p.position.y + p.height > boundingBox.y + boundingBox.h
		// || (p.position.y < boundingBox.y + boundingBox.h) && p.position.y >
		// boundingBox.y
		) {
			boundingBox.h = (p.position.y + p.height - boundingBox.y);
		}
	}

}