package effects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.ld57.game.ddd;

import Global.Global;

public class Explosion extends Sprite {
	public enum State {
		MAIN
	};

	public State currentState;
	public State previousState;
	private Animation<TextureRegion> main;
	private Texture t1;
	// private TextureRegion main;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	private boolean isAnimationOver;
	private float _stateTimer;

	public float speed = 200f;
	public boolean isDead;
	public boolean isResetAnimation;
	public boolean canHit;

	private Sound boom;

	public Explosion(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x + 50;
		rect.y = (int) _y + 50;
		rect.width = 50;
		rect.height = 50;

		_sw = 50;
		_sh = 50;

		boom = Gdx.audio.newSound(Gdx.files.internal("sfx/boom.ogg"));

		// still frames
		t1 = new Texture(Gdx.files.internal("explosion.png"));
		Array<TextureRegion> frames = new Array<TextureRegion>();
		frames.add(new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (0 * _sw), (1 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (1 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (2 * _sw), (1 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (3 * _sw), (1 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (0 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (2 * _sw), (2 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (3 * _sw), (2 * _sh), _sw, _sh));
		main = new Animation<TextureRegion>(.06f, frames, PlayMode.NORMAL);
		frames.clear();// for now main is the same as onehundred
		color = Color.WHITE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main.getKeyFrame(_stateTimer, true));
	}

	public void update(State cs, float dt) {
		if (!isDead) {
			if (!isResetAnimation) {
				isResetAnimation = true;
				_stateTimer = 0;
				setRegion(main.getKeyFrame(_stateTimer));
				if (!Global.SetVolumeToZero)
					boom.play(.1f);
			}
		}
		if (main.isAnimationFinished(_stateTimer))// lets try state timer
			isDead = true;
	}

	public TextureRegion getFrame(OrthographicCamera camera, float dt) {
		currentState = getState();
		update(currentState, dt);

		TextureRegion region;
		switch (currentState) {
		case MAIN:
			region = main.getKeyFrame(_stateTimer, false);
			break;
		default:
			region = main.getKeyFrame(_stateTimer, false);
			break;
		}
		if (currentState != previousState)
			isAnimationOver = false;

		if (!isAnimationOver && currentState == previousState) {
			_stateTimer += dt;
		} else
			_stateTimer = 0;// this may sabotage me!
		previousState = currentState;

		return region;
	}

	public State getState() {
		return State.MAIN;
	}

	public void render(ddd game, OrthographicCamera camera, float dt) {// hmmm i think it just needs placed before? it
		game.batch.setColor(color);
		game.batch.draw(getFrame(camera, dt), rect.x, rect.y);
	}

	public void dispose() {
	}
}
