package utilidades;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

public class Personaje {
	Gifs standby;
	Gifs medio;
	Gifs alto;
	Gifs bajo;
	Gifs actual;
	public static final int STAND=0;
	public static final int ALTO=1;
	public static final int MEDIO=2;
	public static final int BAJO=3;
	private int keyR;
	private int keyL;
	int x;
	int y;
	float contadorCombo;
	float tempCombo;
	private float vida;
	Rectangle caja;
	Rectangle enemigo;
	boolean position;
	ShapeRenderer shapeDrawer;
	String combo;
	int caracter;
	public Personaje(String caracter, boolean position) {
		this.tempCombo=0;
		this.combo="";
		shapeDrawer=new ShapeRenderer();
		//PolygonShape shape = new PolygonShape(1);
		this.vida=100;
		this.position=position;
		if(position) {
			this.x=10;
			this.y=250;
		}else {
			this.x=10;
			this.y=150;
		}
		this.caja=new Rectangle(x, y,70,70);
		this.keyR=Input.Keys.D;
		this.keyL=Input.Keys.A;
		switch (caracter) {
		case "ryu":
			this.caracter=1;
			this.ryu(position);
			break;
		case "ken":
			this.caracter=2;
			this.ken(position);
			break;
		case "cam":
			this.caracter=3;
			this.cam(position);
		default:
			break;
		}
		this.actual=this.standby;
	}
	
	public void cambiarEstado(int estado) {
		this.boost(estado);
		switch(estado) {
		case STAND:
			this.actual=this.standby;
			break;
		case ALTO:
			this.actual=this.alto;
			this.combo+=estado;
			break;
		case MEDIO:
			this.actual=this.medio;
			this.combo+=estado;
			break;
		case BAJO:
			this.actual=this.bajo;
			this.combo+=estado;
			break;
		}
	}
	
	public void boost(int estado) {
		switch(caracter) {
		case 1:
			if(estado==Personaje.MEDIO) {
				if(contadorCombo<2) {
					contadorCombo++;
				}
			}else if(estado==Personaje.ALTO) {
				if(contadorCombo>=2) {
					contadorCombo=0;
					this.tempCombo=5;
				}
			}else {
				contadorCombo=0;
			}
			break;
		case 2:
			
			break;
		case 3:
			
			break;
		default:
			break;
		}		
	}

	void aplicarBoost() {
		this.tempCombo=10;
	}
	
	public void recivir(float danio) {
		if(this.caja.overlaps(enemigo)) {
			this.vida-=danio;
		}
	}
	
	public float atacar(float delta) {
		if(this.actual==this.medio) {
			if(tempCombo>0) {
				return delta*20;
			}else {
				return delta*10;
			}
		}
		if(this.actual==this.alto) {
			if(tempCombo>0) {
				return delta*30;
			}else {
				return delta*15;
			}
		}
			return 0;
	}
	
	public void mover() {
		
		if (Gdx.input.isKeyPressed(this.keyR)) {
			if(this.position) {
				this.y++;
			}else {
				if(!this.caja.overlaps(enemigo)) {
					this.y++;
				}
			}
		}
		if (Gdx.input.isKeyPressed(this.keyL)) {
			if(!this.position) {
				this.y--;
			}else {
				if(!this.caja.overlaps(enemigo)) {
					this.y--;
				}
			}
		}
		this.caja.setPosition(x, y);
	}
	
	public void draw(SpriteBatch batch, float delta) {
		batch.end();
		shapeDrawer.begin(ShapeType.Filled);
		shapeDrawer.setColor(Color.GREEN);
		if(this.position) {
			shapeDrawer.rect(550-(this.vida-100)*3, 400, this.vida*3, 10);
		}else {
			shapeDrawer.rect(45, 400, this.vida*3, 10);
		}
		shapeDrawer.end();
		batch.begin();
		this.actual.avanzar(5, delta);
		this.actual.draw(batch, this.y, this.x);
		batch.end();
		if(tempCombo>0) {
			Gdx.gl.glEnable(GL20.GL_BLEND);
			Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
			shapeDrawer.begin(ShapeType.Filled);
			shapeDrawer.setColor(1f,0f,0f,0.3f);
			if(this.position) {
				shapeDrawer.circle(y+100, x+80, 80);
			}else {
				shapeDrawer.circle(y+50, x+80, 80);
			}
			shapeDrawer.end();
			Gdx.gl.glDisable(GL20.GL_BLEND);
			tempCombo-=delta;
		}
		batch.begin();
	}
	
	public void dispose() {
		shapeDrawer.dispose();
		this.actual.dispose();
	}
	
	private void ryu(boolean position) {
		this.standby=new Gifs("p1.png", 5, 0, 74, 120, 70, 100, position);
		this.medio=new Gifs("p1.png", 3, 4, 93, 355, 88, 100, position);
		this.alto=new Gifs("p1.png", 3, 8, 74, 779, 75, 130, position);
	}
	
	private void ken(boolean position) {
		this.standby=new Gifs("p2.png", 5, 0, 70, 863, 67, 93, position);
		this.medio=new Gifs("p2.png", 2, 0, 70, 1145, 72, 100, position);
		this.alto=new Gifs("p2.png", 3, 6, 74, 2920, 75, 130, position);
	}
	
	private void cam(boolean position) {
		this.standby=new Gifs("p3.png", 8, 0, 78, 113, 75, 98, position);
		this.medio=new Gifs("p3.png", 2, 77, 86, 613, 90, 100, position);
		this.alto=new Gifs("p3.png", 2, 192, 68, 1327, 60, 100, position);
	}
	
	public Rectangle getCaja() {
		return this.caja;
	}
	
	public void setEnemigo(Rectangle enemigo) {
		this.enemigo=enemigo;
	}
	
	public boolean muerto() {
		return this.vida<0;
	}
}
