package com.jln.ld;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;

import menus.GameMenu;

public class GameAnimation implements GameEntity{
	
	private boolean destroy = false;
	private float x,y;
	private Animation animation;
	private float stateTime = 0f;
	private int type;
	
	public static final int ANIM_HEAL = 0;
	public static final int ANIM_EXPLOSION = 1;
	public static final int ANIM_DESTROYER_EXPLOSION = 2;
	public static final int ANIM_DESTROYER_SHOOT= 3;
	public static final int ANIM_DESTROYER_HUNTER01_MOVE= 4;
	public static final int ANIM_DESTROYER_HUNTER02_MOVE= 5;
	
	private int cellSize = 16;
	private int coldown;
	
	/**
	 * Constructeur
	 * @param type
	 * @param x
	 * @param y
	 */
	public GameAnimation(int type, float x, float y){
		this.x = x;
		this.y = y;
		this.type = type;
		if(type == ANIM_HEAL){
			animation = GameUtils.loadAnimation(Shared.ANIM_HEAL, 4, (int)(Math.random()*4), 0.09f,PlayMode.NORMAL);
			cellSize = 16;
		}else if(type == ANIM_EXPLOSION){
			animation = GameUtils.loadAnimation(Shared.ANIM_EXPLOSION, 8, (int)(Math.random()*16), 0.07f,PlayMode.NORMAL);
			cellSize = 16;
			coldown = (int)(Math.random()*5);
		}else if(type == ANIM_DESTROYER_EXPLOSION){
			animation = GameUtils.loadAnimation(Shared.ANIM_DESTROYER_EXPLOSION, 8, (int)(Math.random()*16), 0.07f,PlayMode.NORMAL);
			cellSize = 16;
			coldown = 0;
		}else if(type == ANIM_DESTROYER_SHOOT){
			animation = GameUtils.loadAnimation(Shared.ANIM_DESTROYER_SHOOT, 4, (int)(Math.random()*4), 0.07f,PlayMode.NORMAL);
			cellSize = 8;
			coldown = 0;
		}else if(type == ANIM_DESTROYER_HUNTER01_MOVE){
			animation = GameUtils.loadAnimation(Shared.ANIM_DESTROYER_HUNTER01_MOVE, 4, (int)(Math.random()*4), 0.08f,PlayMode.NORMAL);
			cellSize = 8;
			coldown = 0;
		}else if(type == ANIM_DESTROYER_HUNTER02_MOVE){
			animation = GameUtils.loadAnimation(Shared.ANIM_DESTROYER_HUNTER02_MOVE, 4, (int)(Math.random()*4), 0.08f,PlayMode.NORMAL);
			cellSize = 8;
			coldown = 0;
		}
		
	}
	
	/**
	 * Constructeur
	 * @param animation
	 * @param x
	 * @param y
	 */
	public GameAnimation(Animation theAnimation, int cellSize, float x, float y, int coldown){
		this.x = x;
		this.y = y;
		
		this.cellSize = cellSize;
		this.coldown = coldown;
		
		this.animation = theAnimation;
		this.animation.setPlayMode(PlayMode.NORMAL);
		stateTime =0f; 
	}
	
	/**
	 * Met a jour l'animation
	 */
	public void update(GameMenu game){
		if(coldown>0){
			coldown--;
		}else{
			if(animation.isAnimationFinished(stateTime)){
				destroy = true;
			}else{
				stateTime += Gdx.graphics.getDeltaTime(); 
			}
		}
		
	}
	
	/**
	 * Dessine l'animation
	 */
	public void render(GameMenu game){
		if(coldown>0){
			coldown--;
		}else{
			if(animation != null){
				Shared.GAME_BATCH.draw(animation.getKeyFrame(stateTime), (int)(x-(cellSize/2))+Shared.DECAL_X,(int)(y-(cellSize/2)+Shared.DECAL_Y));
			}
		}
		
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean isDestroy(){
		return destroy;
	}
}
