package net.phatcode.rel;

import java.util.ArrayList;

import net.phatcode.rel.math.Utils;
import net.phatcode.rel.math.Vector2D;
import net.phatcode.rel.utils.Explosion;


public class EnemyEntityFang extends EnemyEntity
{
	
	private ArrayList<BulletPattern> bulletPatterns = new ArrayList<BulletPattern>();
	private ArrayList<TurretBehavior> turretRotateBehaviors = new ArrayList<TurretBehavior>();
	private ArrayList<TurretBehavior> turretFireBehaviors = new ArrayList<TurretBehavior>();
	
	private ArrayList<TurretBehaviorInfo> turretBehaviorInfos = new ArrayList<TurretBehaviorInfo>();
	
	private EnemyBehavior myBehavior = new EnemyBehaviorSpline();
	private Turret cannon;
	private int patternIndex = 0;
	
	public EnemyEntityFang()
	{
		active = false;
		
		spline.addPoint( new Vector2D( (float)Math.random() * Constants.SCREEN_WIDTH,
				   					   -50 ) );
		spline.addPoint( new Vector2D( (float)Math.random() * Constants.SCREEN_WIDTH,
				   					   -50 ) );

		for( int i = 0; i < 10; i++ )
		{
			spline.addPoint( new Vector2D( (float)Math.random() * Constants.SCREEN_WIDTH,
					    				   (float)Math.random() * Constants.SCREEN_HEIGHT/2 ) );
		}
		
		spline.addPoint( new Vector2D( (float)Math.random() * Constants.SCREEN_WIDTH,
				   		               (float) Constants.SCREEN_HEIGHT + 100 ) );
		spline.addPoint( new Vector2D( (float)Math.random() * Constants.SCREEN_WIDTH,
	               					   (float) Constants.SCREEN_HEIGHT + 100 ) );

		renderMode = RenderMode.NORMAL;
		
		animator = new Animator( 2, 1, 24, Animator.Mode.ORIENTED );
		
		initHitBox( position.x, position.y, 40, 40 );
	
		bulletPatterns.add( BulletPatternFactory.fullSpread( new Animator(19,3) ) );
		bulletPatterns.add( BulletPatternFactory.wasak( new Animator( 26, 1, 24, Animator.Mode.ORIENTED) ) );
		bulletPatterns.add( BulletPatternFactory.shinto( new Animator(76,4) ) );
		bulletPatterns.add( BulletPatternFactory.fullSpreadDecel( new Animator(53,3) ) );
		
		turretRotateBehaviors.add( Behaviors.rotateBehaviorOscillate );
		turretRotateBehaviors.add( Behaviors.rotateBehaviorOscillate );
		turretRotateBehaviors.add( Behaviors.rotateBehaviorOscillate );
		turretRotateBehaviors.add( Behaviors.rotateBehaviorDefault );
		
		turretFireBehaviors.add( Behaviors.fireBehaviorSpread );
		turretFireBehaviors.add( Behaviors.fireBehaviorSpread );
		turretFireBehaviors.add( Behaviors.fireBehaviorSpread );
		turretFireBehaviors.add( Behaviors.fireBehaviorSpread );
		
		turretBehaviorInfos.add( new TurretBehaviorInfo( Utils.PI / 2, Utils.PI / 2 - 0.2f, Utils.PI / 2 + 0.2f, 0.1f, 1, false ) );
		turretBehaviorInfos.add( new TurretBehaviorInfo( Utils.PI / 2, 0, Utils.PI * 2, 0.0f, 1, true ) );
		turretBehaviorInfos.add( new TurretBehaviorInfo( Utils.PI / 2, 0, Utils.PI * 2, 0.1f, 1, true ) );
		turretBehaviorInfos.add( new TurretBehaviorInfo() );

		
	}

	@Override
	public void update( BulletFactory bulletFactory, Vector2D playerShipPosition )
	{
		counter++;
		myBehavior.update( this );
		position.x = spline.getX();
		position.y = spline.getY();
		
		if( position.y > Constants.SCREEN_HEIGHT + 50 )
		{
			killMe = true;
			return;
		}
		
		angle = spline.getAngle();
		
		animator.setAngle( Utils.wrap((int)Math.toDegrees((double)angle), 0, 359) );
		
		animator.update();
		
		if( renderMode == RenderMode.NORMAL )
		{
			animator.baseFrame = 2;
		}
		else
		{
			animator.baseFrame = 26;
			renderMode = RenderMode.NORMAL;
		}
		
		if( cannon != null )
		{
			cannon.setPosition( position.x, position.y );
		}
		
		updateHitBox();
		
		if( (counter & 255) == 0 )
		{
			
			patternIndex = (patternIndex + 1) % bulletPatterns.size();
			
			// #1
			cannon.setPattern( bulletPatterns.get(patternIndex) );
			
			TurretBehaviorInfo info = turretBehaviorInfos.get(patternIndex);
			cannon.setBehaviorInfo( info.angle,
						   	 		info.minAngle,
						   	 		info.maxAngle,
						   	 		info.angleDelta,
						   	 		info.angleDirection,
						   	 		info.seekPlayer,
						   	 		turretRotateBehaviors.get(patternIndex),
						   	 		turretFireBehaviors.get(patternIndex) );
		}
		
	}
	
	public void spawn( TurretFactory turrets )
	{
		counter = 0;
		active = true;
		killMe = false;
		Hp = 30;
		patternIndex = 0;
		spline.reset();
		TurretBehaviorInfo info = turretBehaviorInfos.get(patternIndex);
		cannon = turrets.spawn( bulletPatterns.get(patternIndex), 
								position.x, 
								position.y, 
								info.angle, 
								info.minAngle, 
								info.maxAngle, 
								info.angleDelta, 
								info.angleDirection, 
								info.seekPlayer, 
								turretRotateBehaviors.get(patternIndex),
					   	 		turretFireBehaviors.get(patternIndex) );
	
	}
	
	@Override
	public void kill()
	{
		
		active = false;
		cannon.kill();
		
	}
	
	@Override
	public void explode()
	{
		Explosion.getInstance().spawn( position.x, 
				   					   position.y, 
				   					   0,
				   					   0, 
				   					   42, 5 );	
		int max = 4 + (int)(Math.random() * 5);
		for( int i = 0; i < max; i++ )
		{
			Explosion.getInstance().spawn( position.x, 
					   					   position.y, 
										   counter + (2 * Utils.PI * i) / (float)max,
										   5, 
										   52, 4 );	
			
		}
		
		this.kill();
		
	}
	
	
}
