import greenfoot.*;
import java.util.*;

/**
 * Return random spaceships that can be allies (in 2-player mode) or enemies.
 * 
 * @author Jannis Andrija Schnitzer, Martin Schend
 * @version 2011-01-21
 */
public class SpaceshipFactory  
{
    public static final int N_AI_CLASSES = 2;
    
    // list of possible classes, the variables are just for the sake of nicer code!
    public static final int ENEMY1 = 0;
    public static final int ENEMY2 = 1;
    
    /**
     * Random deviation in the level of created spaceships.
     */
    public static final int LEVEL_DEVIATION = 1;
    
    /**
     * Side the created spaceships will be on.
     */
    protected int side;
    /**
     * Level the created spaceships will (approximately) have.
     * 
     * @see #LEVEL_DEVIATION
     */
    protected int level;
    /**
     * Direction the created spaceships will fly in.
     */
    protected double direction;

    public SpaceshipFactory(int the_side, int starting_level)
    {
        side = the_side;
        level = starting_level;
    }
    
    public void setLevel(int new_level)
    {
        level = new_level;
    }
    public int getLevel()
    {
        return level;
    }
    public void increaseLevel()
    {
        level++;
    }
    
    public void setDirection(double the_direction)
    {
        direction = the_direction;
    }
    public double getDirection()
    {
        return direction;
    }

    /**
     * Return a random AIControlledSpaceship object that can be placed into the world.
     * 
     * @return     the ship, randomly selected.
     */
    public AIControlledSpaceship ship()
    {
        int number = Greenfoot.getRandomNumber(N_AI_CLASSES);
        // add some randomness in the level...
        int actual_level = level + (Greenfoot.getRandomNumber(2*LEVEL_DEVIATION+1) - LEVEL_DEVIATION);
        if (actual_level < 1)
            actual_level = 1;
        
        /* When we're at level 1, deviation is somewhat unfair since there's no chance to get an
         * easier spaceship. So we want less hard spaceships, too. Thus, we want to cut the chance
         * to get a harder spaceship a bit more. In 50% of cases, we just reset the level to 1.
         * That way, the chance to get a level 2 spaceship drops to about 17% (if I didn't mis-
         * calculate).
         */
        if (level == 1)
            if (Greenfoot.getRandomNumber(2) != 0)
                actual_level = 1;
        
        AIControlledSpaceship result_ship;
        
        switch(number) {
            case ENEMY1:
                result_ship = new Enemy1(actual_level);
                break;
           case ENEMY2:
                result_ship = new Enemy2(actual_level);
                break;
            default:
                return null;
        }
        
        /* Speed.
         * Ship_deviation contains the ship's deviation property, actual_deviation is set to a random
         * value that is within [-ship_deviation..ship_deviation].
         * Ships only have y speeds as for now. Diversion may be added later. Ship speed setting
         * must be delegated to the ship itself then, though.
         */
        Random random = new Random();
        double initial_speed, ship_deviation, actual_deviation;
        
        initial_speed = result_ship.getInitialSpeed();
        ship_deviation = result_ship.getDeviation();
        actual_deviation = 2.0*ship_deviation*random.nextDouble() - ship_deviation;
        
        result_ship.setSpeed(new Vector(0.0, direction * (initial_speed + actual_deviation)));
        result_ship.setRotation(90-(int)direction*90);

        result_ship.setSide(side);
        return result_ship;
    }
}
