package net.phatcode.rel;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import net.phatcode.rel.Entity.RenderMode;
import net.phatcode.rel.math.Vector2D;
import net.phatcode.rel.utils.ImageAtlas;
import net.phatcode.rel.utils.LinkedArray;
import net.phatcode.rel.utils.Sonics;

public class EnemyEntityFactory
{
	
	public static final int OUT_OF_BOUNDS = -1;
	
	protected LinkedArray<EnemyEntity> enemies;
	
	
	public EnemyEntityFactory()
	{
				
	}
	
	public EnemyEntityFactory( int size )
	{
				
	}
	
	public EnemyEntity spawn( TurretFactory turrets,
							  float startX,
					     	  float startY,
					     	  float endX,
					     	  float endY )
	{
	
		return null;
	}
	
	public int getSize()
	{
		return enemies.getSize();
	}
	
	public int getActiveEnemies()
	{
		return enemies.getActiveElements();
	}
	
	public EnemyEntity getEnemy( int index )
	{
		return enemies.get( index );
	}
	
	public int getAvailableIndex()
	{
		int size = enemies.getSize();
		for( int i = 0; i < size; i++ )
		{
			EnemyEntity t = enemies.get(i);
			if( !t.isActive() ) return i;
		}
		return OUT_OF_BOUNDS;
	}
	
	public int addEnemy( EnemyEntityFang e )
	{
		return enemies.add( e );
	}
	
	public void removeEnemy( int index )
	{
		enemies.remove( index );
	}
	
	public void destroyAll()
	{
		int size = enemies.getSize();
		for( int i = 0; i < size; i++ )
		{
			EnemyEntity e = enemies.get(i);
			
			if( e.isActive() )
			{
				e.kill() ;
				enemies.remove(i);
			}
		}
		
	}
	
	public void killAll()
	{
		int size = enemies.getSize();
		for( int i = 0; i < size; i++ )
		{
			EnemyEntity e = enemies.get(i);
			
			if( e.isActive() )
			{
				e.kill() ;
				enemies.remove(i);
			}
		}

	}
	
	public void updateAll( BulletFactory bulletFactory, Vector2D playerShipPosition )
	{
		int size = enemies.getSize();
		for( int i = 0; i < size; i++ )
		{
			EnemyEntity e = enemies.get(i);
			
			if( e.isActive() )
			{
				e.update( bulletFactory, playerShipPosition );
				if( e.isKillMe() )
				{
					e.kill();
					enemies.remove(i);
				}

			}
		}
		
	}
	
	public void renderAll( Screen screen, Graphics2D g, ImageAtlas imageAtlas )
	{
        
		int size = enemies.getSize();
		for( int i = 0; i < size; i++ )
		{
			EnemyEntity e = enemies.get(i);
			
			if( e.isActive() )
			{
				BufferedImage sprite = imageAtlas.getSprite(e.getFrame());
				g.drawImage( sprite, 
							 (int)e.position.x - sprite.getWidth()/2,
							 (int)e.position.y - sprite.getHeight()/2,
							 screen );
			}
		}
		
	}
	
	public void renderAabbs( Screen screen, Graphics2D g, ImageAtlas imageAtlas )
	{
		g.setStroke(new BasicStroke(1));
        g.setColor( Color.WHITE );
        
		int size = enemies.getSize();
		for( int i = 0; i < size; i++ )
		{
			EnemyEntity e = enemies.get(i);
			
			if( e.isActive() )
			{
				g.drawRect( (int)e.hitBox.x1, (int)e.hitBox.y1, (int)e.hitBox.width, (int)e.hitBox.height );
			}
		}
		
	}
	
	public void collideWithPlayer( ShipEntity viper )
	{
		boolean hit = false;
		int size = enemies.getSize();
		for( int i = 0; i < size; i++ )
		{
			EnemyEntity e = enemies.get( i );
			
			if( e.isActive() )
			{
				
				//if( e.collidesWith( viper ) )
				//{
				//	viper.kill();
				//	enemies.remove(i);
				//}
				
				// shots
				int smax = viper.shots.getSize();
				for( int j = 0; j < smax; j++ )
				{
					ShotEntity se = viper.shots.get( j );
					if( se.isActive() )
					{
						if( e.collidesWith(se) )
						{
							e.setRenderMode( RenderMode.WHITE );
							e.decHp( ShotEntity.SHOT_ENERGY );
							if( e.getHp() <= 0 )
							{
								e.explode();
								enemies.remove(i);
								break;
							}
							se.explode();
							viper.shots.remove(j);
							hit = true;
						}
					}
				}
				
				if( hit ) Sonics.getInstance().playEffect("media/enemyhit.wav");
			}
		}
		
	}
	

}
