package menus;

import java.util.ArrayList;

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

public class MenuSeleccion extends Menu{
	
	ArrayList<Caratula> caratulas;
	ShapeRenderer shapeDrawer;
	
	public MenuSeleccion() {
		super();
		this.shapeDrawer=new ShapeRenderer();
		this.n_opciones=5;
		this.local=Menu.SELEC;
		this.caratulas=new ArrayList<Caratula>();
		this.caratulas.add(new Caratula("p1.png", "ryu", 984, 88, 128, 110));
		this.caratulas.add(new Caratula("p2.png", "ken", 228, 565, 128, 110));
		this.caratulas.add(new Caratula("p3.png", "cam", 243,0,128, 110));
		this.caratulas.add(new Caratula("p4.png", "sagat", 227,312,128, 110));
		this.caratulas.add(new Caratula("p5.png", "blanka", 99,2963,100, 110));
	}
	
	@Override
	public int draw(SpriteBatch batch, float delta) {
		for(int i=0; i<this.caratulas.size();i++) {
			if(i<3) {
				if(i==this.position) {
					batch.end();
					shapeDrawer.begin(ShapeType.Filled);
					shapeDrawer.setColor(Color.GRAY);
					shapeDrawer.rect(10+i*280, 430, 190, 180);
					shapeDrawer.end();
					batch.begin();
				}
				this.caratulas.get(i).draw(batch, 10+i*200, 300);
			}else {
				if(i==this.position) {
					batch.end();
					shapeDrawer.begin(ShapeType.Filled);
					shapeDrawer.setColor(Color.GRAY);
					shapeDrawer.rect(10+(i-3)*280, 210, 190, 180);
					shapeDrawer.end();
					batch.begin();
				}
				this.caratulas.get(i).draw(batch, 10+(i-3)*200, 150);
			}
			
		}
		return this.darSeleccionado();
	}

	@Override
	int darSeleccionado() {
		if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
			if(!this.pres) {
				this.position++;
				this.pres=true;
			}
		}else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
			if(!this.pres) {
				this.position--;
				this.pres=true;
			}
		}else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
			if(!this.pres) {
				this.position=3;
				this.pres=true;
			}
		}else if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
			if(!this.pres) {
				this.position=0;
				this.pres=true;
			}
		}else if (Gdx.input.isKeyPressed(Input.Keys.ENTER)){
			if(!this.pres) {
				this.pres=true;
				String malo = this.caratulas.get((int) Math.round(Math.random()*(this.caratulas.size()-1))).nombre;
				Menu.menus.add(new Battle(this.caratulas.get(this.position).nombre, malo));
				return Menu.BATTLE;
			}
		}else if(this.pres) {
			this.pres=false;
		}
		this.position%=this.n_opciones;
		if(this.position<0)this.position=this.n_opciones-1;
		return this.local;
	}

}
