package enemis;

import com.badlogic.gdx.math.Rectangle;
import com.jln.ld.GameEntity;
import com.jln.ld.Shared;

import MapItem.Powerup;
import Ship.SupplySpaceship;
import menus.GameMenu;
import shoot.BasicShoot;

public class Asteroid implements GameEntity{
	private float x;
	private float y;
	private int size;
	private boolean destroy = false;
	private Rectangle collision;
	private int life = 3;
	private int cptHit;
	private float speed = 2;
	private boolean recreate = false;
	private int colorType = 0;	
	
	public static final int MEDIUM1 = 0;
	public static final int MEDIUM2 = 1;
	public static final int MEDIUM3 = 2;
	public static final int SMALL = 3;
	public static final int BIG = 4;
	
	public Asteroid(int size, boolean recreate){
		this.size = size;
		this.recreate = recreate;
		this.initAsteroid();
	}
	
	private void initAsteroid(){
		this.x = (float)(Math.random()*(recreate?1000:3000)+Shared.GAME_WIDTH);
		this.y = (float)(Math.random()*360);
		destroy = false;
		
		double r = Math.random();
		this.size = r<0.33?MEDIUM1:r<0.66?MEDIUM2:MEDIUM3;
		this.life = 3;
		this.speed = (float) (0.5f+(Math.random()*2));
		this.initCollision();
		colorType = (int)(Math.random()*4);
	}
	
	private void initCollision(){
		if(size == MEDIUM1 || size == MEDIUM3){
			if(life == 3){
				collision = new Rectangle(x-19,y-17,38,34);
			}else if(life == 2){
				collision = new Rectangle(x-17,y-17,34,34);
			}else if(life == 1){
				collision = new Rectangle(x-16,y-16,32,32);
			}else if(life == 0){
				collision = new Rectangle(x-12,y-9,24,18);
			}
		}else if(size == MEDIUM2){
			if(life == 3){
				collision = new Rectangle(x-16,y-12,32,24);
			}else if(life == 2){
				collision = new Rectangle(x-16,y-12,32,24);
			}else if(life == 1){
				collision = new Rectangle(x-8,y-8,16,16);
			}else if(life == 0){
				collision = new Rectangle(x-5,y-5,10,10);
			}
		}
	}
	
	@Override
	public void update(GameMenu game) {
		if(destroy){
			if(recreate){
				this.initAsteroid();
			}
		}else{
			if(cptHit>0){
				cptHit --;
			}else{
				x -=speed;
				if(x<-100){
					destroy = true;
				}
				collision.setX(collision.getX()-speed);
			}
			if(game.getHunter01().onAsteroidContact(game,this)){
				destroy = true;
			}else if(game.getHunter02().onAsteroidContact(game,this)){
				destroy = true;
			}else if(game.getSupplyShip().onAsteroidContact(game,this)){
				destroy = true;
			}
			
			if(destroy && !recreate){
				game.updateAsteroid = true;
			}
		}
		
	}

	@Override
	public void render(GameMenu game) {
		if(!destroy && x<680){
			switch(colorType){
			case 0:
				Shared.GAME_BATCH.draw(Shared.ASTEROIDS[size][3-life], (int)(x-24), (int)(y-24));
				break;
			case 1:
				Shared.GAME_BATCH.draw(Shared.ASTEROIDS2[size][3-life], (int)(x-24), (int)(y-24));
				break;
			case 2:
				Shared.GAME_BATCH.draw(Shared.ASTEROIDS3[size][3-life], (int)(x-24), (int)(y-24));
				break;
			case 3:
				Shared.GAME_BATCH.draw(Shared.ASTEROIDS4[size][3-life], (int)(x-24), (int)(y-24));
				break;
			}
			
		}
	}
	
	public boolean onShootContact(GameMenu game, BasicShoot s, boolean damage){
		if(!destroy && !s.isDestroy() && collision.contains(s.getX(), s.getY())){
			if(damage){
				life --;
				if(speed>1){
					speed -= 0.2f;
				}
				cptHit = 5;
				if(life < 0){
					destroy = true;
					giveBonus(game);
					game.incPointsWithMessage(Shared.POINTS_ASTEROID_MEDIUM, x, y);
				}
			}
			return true;
		}
		return false;
	}
	
	private void giveBonus(GameMenu game){
		double r = Math.random();
		if(r < 0.1){
			game.getPowerups().add(new Powerup(x, y, Powerup.POWERUP_INCREASESHOOT,game));
		}else if(r < 0.15){
			game.getPowerups().add(new Powerup(x, y, Powerup.POWERUP_SHIELD,game));
		}else if(r < 0.25){
			game.getPowerups().add(new Powerup(x, y, Powerup.POWERUP_50,game));
		}
	}

	public Rectangle getCollision() {
		return collision;
	}

	public boolean isDestroy(){
		return destroy;
	}
	
	public boolean isVisible(){
		return x<600;
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}
	
	public void setRecreate(boolean b){
		this.recreate = b;
	}
	
}
