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;
	int estado;
	int keyR;
	int keyL;
	int x;
	int y;
	float contadorCombo;
	float tempCombo;
	float contadorComboDefensa;
	float tempComboDefensa;
	private float vida;
	Rectangle caja;
	Rectangle enemigo;
	boolean position;
	ShapeRenderer shapeDrawer;
	String combo;
	int caracter;
	float coeficienteDanio;
	float coeficienteDef;
	public Personaje(String caracter, boolean position) {
		this.tempCombo=0;
		this.combo="";
		shapeDrawer=new ShapeRenderer();
		this.vida=100;
		this.position=position;
		if(position) {
			this.x=10;
			this.y=370;
		}else {
			this.x=10;
			this.y=150;
		}
		if(this.position) {
			this.caja=new Rectangle(x, y+120,70,70);
		}else {
			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);
			this.coeficienteDanio=1;
			this.coeficienteDef=1;
			break;
		case "ken":
			this.caracter=2;
			this.ken(position);
			this.coeficienteDanio=0.8f;
			this.coeficienteDef=1.2f;
			break;
		case "cam":
			this.caracter=3;
			this.cam(position);
			this.coeficienteDanio=1.2f;
			this.coeficienteDef=0.8f;
			break;
		case "sagat":
			this.caracter=4;
			this.sagat(position);
			this.coeficienteDanio=0.9f;
			this.coeficienteDef=1.1f;
			break;
		case "blanka":
			this.caracter=5;
			this.blanka(position);
			this.coeficienteDanio=1.5f;
			this.coeficienteDef=1.2f;
			break;
		default:
			break;
		}
		this.actual=this.standby;
	}
	
	public void cambiarEstado(int estado) {
		this.estado=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:
			if(estado==Personaje.BAJO) {
				if(contadorCombo<2) {
					contadorCombo++;
				}
			}else if(estado==Personaje.ALTO) {
				if(contadorCombo>=2) {
					contadorCombo=0;
					this.tempCombo=5;
				}
			}else {
				contadorCombo=0;
			}
			break;
		case 3:
			if(estado==Personaje.MEDIO) {
				if(contadorCombo<2) {
					contadorCombo++;
				}
			}else if(estado==Personaje.BAJO) {
				if(contadorCombo>=2) {
					contadorCombo=0;
					this.tempCombo=5;
				}
			}else {
				contadorCombo=0;
			}
			break;
		case 4:
			if(estado==Personaje.ALTO) {
				if(contadorCombo<2) {
					contadorCombo++;
				}
			}else if(estado==Personaje.BAJO) {
				if(contadorCombo>=2) {
					contadorCombo=0;
					this.tempCombo=5;
				}
			}else {
				contadorCombo=0;
			}
			break;
		default:
			break;
		}		
	}

	void aplicarBoost() {
		this.tempCombo=10;
	}
	
	public void recivir(Ataque ataque) {
		if(this.caja.overlaps(enemigo)) {
			if(this.tempComboDefensa>1) {
				this.vida-=ataque.danio(this.estado)/(this.coeficienteDef*2);
			}else {
				this.vida-=ataque.danio(this.estado)/this.coeficienteDef;
				if(ataque.esquivado)this.tempComboDefensa++;
			}
		}
	}
	
	public Ataque atacar(float delta) {
		if(tempCombo>0) {
			return new Ataque(this.estado, this.coeficienteDanio*delta*20);
		}else {
			return new Ataque(this.estado, this.coeficienteDanio*delta*10);
		}
	}
	
	public void mover() {
		
		if (Gdx.input.isKeyPressed(this.keyR)) {
			if(this.position) {
				if(y<450)this.y++;
			}else {
				if((!this.caja.overlaps(enemigo))&&y<600) {
					this.y++;
				}
			}
		}
		if (Gdx.input.isKeyPressed(this.keyL)) {
			if((!this.position)) {
				if(y>0)this.y--;
			}else {
				if((!this.caja.overlaps(enemigo))) {
					this.y--;
				}
			}
		}
		if(this.position) {
			this.caja.setPosition(x, y+120);
		}else {
			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(7, delta);
		if(this.position) {
			this.actual.draw(batch, this.y, this.x);
		}else {
			this.actual.draw(batch, this.y, this.x);
		}
		this.dibujarBoost(batch, delta);
	}
	
	void dibujarBoost(SpriteBatch batch, float delta) {
		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*1.35f)+190, x+80, 80);
			}else {
				shapeDrawer.circle(70+(1.35f*y), x+80, 80);
			}
			shapeDrawer.end();
			Gdx.gl.glDisable(GL20.GL_BLEND);
			tempCombo-=delta;
		}
		if(tempComboDefensa>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(0f,0f,1f,0.3f);
			if(this.position) {
				shapeDrawer.circle((y*1.35f)+190, x+80, 80);
			}else {
				shapeDrawer.circle(70+(1.35f*y), x+80, 80);
			}
			shapeDrawer.end();
			Gdx.gl.glDisable(GL20.GL_BLEND);
			tempComboDefensa-=delta;
		}
		batch.begin();
	}
	
	public void dispose() {
		shapeDrawer.dispose();
		this.actual.dispose();
	}
	
	private void ryu(boolean position) {
		this.standby=new Gifs("ryu/stand", 6, position);
		this.medio=new Gifs("ryu/frente", 3, position);
		this.alto=new Gifs("ryu/arriba", 3, position);
		this.bajo=new Gifs("ryu/sentado", 3, position);
	}
	
	private void ken(boolean position) {
		this.standby=new Gifs("ken/stand", 6, position);
		this.medio=new Gifs("ken/frente", 3, position);
		this.alto=new Gifs("ken/arriba", 3, position);
		this.bajo=new Gifs("ken/sentado", 3, position);
	}
	
	private void cam(boolean position) {
		this.standby=new Gifs("cam/stand", 8, position);
		this.medio=new Gifs("cam/fente", 6, position);
		this.alto=new Gifs("cam/arriba", 4, position);
		this.bajo=new Gifs("cam/sentado", 3, position);
	}
	
	private void sagat(boolean position) {
		this.standby=new Gifs("sagat/stand", 6, position);
		this.medio=new Gifs("sagat/frente",3, position);
		this.alto=new Gifs("sagat/alto", 2, position);
		this.bajo=new Gifs("sagat/bajo", 2, position);
	}
	
	private void blanka(boolean position){
		this.standby=new Gifs("blanka/stand", 3, position);
		this.medio=new Gifs("blanka/frente",3, position);
		this.alto=new Gifs("blanka/alto", 3, position);
		this.bajo=new Gifs("blanka/bajo", 3, position);
	}
	
	public Rectangle getCaja() {
		return this.caja;
	}
	
	public void setEnemigo(Rectangle enemigo) {
		this.enemigo=enemigo;
	}
	
	public boolean muerto() {
		return this.vida<0;
	}
	
	public String toString() {
		switch (caracter) {
		case 1:return "ryu";
		case 2:return "ken";
		case 3:return "cam";
		}
		return "";
	}
}
