package enemis;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.jln.ld.GameAnimation;
import com.jln.ld.GameEntity;
import com.jln.ld.Shared;

import MapItem.Powerup;
import menus.GameMenu;
import shoot.BasicShoot;
import shoot.DestroyerShoot;

public class Destroyer implements GameEntity{

	protected float x,y,targetX,targetY, speed, maxSpeed;
	protected boolean destroy;
	protected int type;
	protected Rectangle collision;
	protected int life = 5;
	protected int speedIndex = 0;
	protected int cptPause = 0, cptShoot=60, cptHit=0;
	protected int size;
	public int cptHitByShoot = 0;
	protected TextureRegion[][] regions;

	public static final int TYPE_01 = 1;
	public static final int TYPE_02 = 2;
	public static final int TYPE_03 = 3;
	public static final int TYPE_04 = 4;

	public Destroyer(){
		this.initShip();
	}

	protected void initShip(){
		this.x = (float)(Math.random()*(1500)+Shared.GAME_WIDTH);
		this.y = (float)(Math.random()*320)+25;

		this.type = TYPE_01;
		initRandomTarget();
		destroy = false;
		this.collision = new Rectangle(x-10,y-30,20,60);
		maxSpeed = (float) ((Math.random()) + 1f);
		this.speed = maxSpeed;
		this.cptPause = 0;
		regions = Shared.SHIP_DESTROYER;
		size = 64;
		life = 5;
	}

	@Override
	public void update(GameMenu game) {
		if(cptHitByShoot>0){
			cptHitByShoot-=3;
		}
		if(destroy){
			this.initShip();
		}else{
			if(x>(Shared.GAME_WIDTH - 20)){
				x--;
				collision.setX(collision.getX()-1);
			}else{
				if(cptHit>0){
					cptHit --;
					x--;
					collision.setX(collision.getX()-1);
				}else if(cptPause<=0){
					move(game);
				}else{
					cptPause--;
				}
				if(cptShoot<=0){
					shoot(game);
					Double r = Math.random();
					if(r<0.25){
						cptShoot = 20;
					}else if(r<0.5){
						cptShoot = 40;
					}else{
						cptShoot = 80;
					}
				}else{
					cptShoot--;
				}

			}
		}

	}

	protected void move(GameMenu game){
		float dx = 0;
		float dy = 0;
		if(Math.abs(x-targetX)>25){
			if(x>targetX){
				dx -= speed;
			}else{
				dx += speed;
			}
		}
		if(Math.abs(y-targetY)>25){
			if(y>targetY){
				dy -= speed;
			}else{
				dy += speed;
			}
		}

		if(dx != 0 || dy !=0){
			if(dx != 0 && dy != 0){
				dx *= 0.7f;
				dy *= 0.7f;
			}
			x+=dx;
			y+=dy;
			collision.setX(collision.getX()+dx);
			collision.setY(collision.getY()+dy);
		}else{

			Double r = Math.random();
			if(r<0.33){
				speed = 0.7f*maxSpeed;
				speedIndex = 1;
				initRandomTarget();
			}else if(r<0.8){
				speed = maxSpeed;
				speedIndex = 2;

			}else{
				cptPause = (int)(Math.random()*60);
				speed = 0.7f*maxSpeed;
				speedIndex = 1;
				initRandomTarget();
			}
		}
	}

	@Override
	public void render(GameMenu game) {
		if(cptHitByShoot>0){
			Shared.GAME_BATCH.setColor(1f, 1f-(cptHitByShoot/100f), 1f-(cptHitByShoot/100f), 1f);
		}
		if(!destroy && x<660){
			Shared.GAME_BATCH.draw(regions[0][(cptPause>0)?0:speedIndex], x-(size/2)+Shared.DECAL_X,y-(size/2)+Shared.DECAL_Y);
		}
		Shared.GAME_BATCH.setColor(1f, 1f, 1f, 1f);

	}
	
	protected void shoot(GameMenu game){
		game.addEnemyShoot(new DestroyerShoot(x-12, y+14, -3.5f, 0.2f));
		game.addEnemyShoot(new DestroyerShoot(x-12, y-14, -3.5f, -0.2f));
		playShootSound();
	}

	protected void initRandomTarget(){
		this.targetX = (float) ((Shared.GAME_WIDTH/2)+((Math.random()*(Shared.GAME_WIDTH/2.5f))));
		this.targetY = (float)(Math.random()*320)+25;
	}

	public boolean onShootContact(GameMenu game, BasicShoot s, boolean damage){
		if(!destroy && !s.isDestroy() && collision.contains(s.getX(), s.getY())){
			if(damage && cptHitByShoot<50){
				life --;
				cptHitByShoot = 100;
				cptHit = 8;
				game.getAnimations().add(new GameAnimation(GameAnimation.ANIM_DESTROYER_EXPLOSION, 
						(float)(x-24+(Math.random()*48))+Shared.DECAL_X, (float)(y-24+(Math.random()*48))+Shared.DECAL_Y));
				game.getAnimations().add(new GameAnimation(GameAnimation.ANIM_DESTROYER_EXPLOSION, 
						(float)(x-24+(Math.random()*48))+Shared.DECAL_X, (float)(y-24+(Math.random()*48))+Shared.DECAL_Y));
				if(life < 0){
					destroy = true;
					giveBonus(game);
					game.updateEnemis = true;
					game.getAnimations().add(new GameAnimation(GameAnimation.ANIM_DESTROYER_EXPLOSION, 
							(float)(x-24+(Math.random()*48))+Shared.DECAL_X, (float)(y-24+(Math.random()*48))+Shared.DECAL_Y));
					game.getAnimations().add(new GameAnimation(GameAnimation.ANIM_DESTROYER_EXPLOSION, 
							(float)(x-24+(Math.random()*48))+Shared.DECAL_X, (float)(y-24+(Math.random()*48))+Shared.DECAL_Y));
					game.getAnimations().add(new GameAnimation(GameAnimation.ANIM_DESTROYER_EXPLOSION, 
							(float)(x-24+(Math.random()*48))+Shared.DECAL_X, (float)(y-24+(Math.random()*48))+Shared.DECAL_Y));
					game.getAnimations().add(new GameAnimation(GameAnimation.ANIM_DESTROYER_EXPLOSION, 
							(float)(x-24+(Math.random()*48))+Shared.DECAL_X, (float)(y-24+(Math.random()*48))+Shared.DECAL_Y));
				}
			}
			return true;
		}
		return false;
	}
	
	protected void giveBonus(GameMenu game){
		game.incPointsWithMessage(Shared.POINTS_DESTROYER, x, y);
		double r = Math.random();
		if(r < 0.1){
			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));
		}else if(r < 0.35){
			game.getPowerups().add(new Powerup(x, y, Powerup.POWERUP_100,game));
		}else if(r < 0.4){
			game.getPowerups().add(new Powerup(x, y, Powerup.POWERUP_INCREASESHOOT,game));
		}
	}
	
	public boolean isDestroy(){
		return destroy;
	}
	
	protected void playShootSound(){
		Shared.SOUND_LASER_4.play(0.8f);
	}
}
