//Spaceship.java
//Movement & Search team
//Jon Havstad, Jesse Eyerman, Sam Cordova, Qutaiba Mahmoud
//CS371 Lab 7


import javax.swing.*;
import java.util.*;

public class Spaceship extends Unit {

    static Spaceship[] AllShips;
    static Hashtable SpaceshipHash;
    //instance variables
    private String iconURL, shipName;
    private int cannons, shields, shieldMax, maneuver, maxPassengers, numPassengers;
    private boolean inPlay, hasPilot;
    private ImageIcon icon;
    private Charactr pilot, owner;
    private Charactr[] passengers;
    private Vector shipPassengers;

	 
    //constructor method
    public Spaceship(String name, int cann, int shiel, int maneuv, int maxPass, Charactr own, char loyalty){
	shipName = name;
	cannons = cann;
	shields = shiel;
	maneuver = maneuv;
	maxPassengers = maxPass;
	owner = own;
	inPlay = hasPilot = false;
	pilot = null;
	passengers = new Charactr[maxPassengers];
	numPassengers = 0;
	unitType = 's';
	groupType = 'c';
	shipPassengers = new Vector();
	side = loyalty;
    }
// Accessor methods
/**
 * get(String)
 * Returns Spaceship matching Name
 */
    public static Spaceship get(String s)
    {
	return (Spaceship)SpaceshipHash.get(s);
    }
/**
 * getSpaceshipInitArray
 * Returns Spaceship initialization array
 */
    public static Spaceship[] getSpaceshipInitArray()
    {
	return AllShips;
    }
	 public String getName() {
		  return shipName;
	 }
	 public int getCannons() {
		  return cannons;
	 }
	 public int getShieldValue() {
		  return shields;
	 }
	 public int getManeuverRating() {
		  return maneuver;
	 }
	 public int getMaxPassengers() {
		  return maxPassengers;
	 }
    public Vector getShipPassengers(){
	return shipPassengers;
    }
	 public Charactr getPilot() {
		  return pilot;
	 }
	 public Charactr getOwner() {
		  return owner;   

	 }
	 public boolean getHasPilot() {
		  return hasPilot;
	 }

	 public Charactr[] getPassengers() {
		  return passengers;
	 }

	 public int getNumPassengers() {
		  return numPassengers;
	 }

//Modifier methods
	 public void setPilot(Charactr newPilot) {
		  pilot = newPilot;
		  if (newPilot != null) {
		      hasPilot = true;
				passengers[numPassengers] = newPilot;
				numPassengers++;
		  }
		  else 
		      hasPilot = false;
		  return;
	 }
	 public void changeShields(int shieldValMod) {
		  if ((shields += shieldValMod) <= shieldMax)
				shields += shieldValMod;
		  else
				shields = shieldMax;
		  
		  if (shields <= 0)
				setInPlay(false);
		  return;
	 }
	 public void setInPlay(boolean b) {
		  inPlay = b;
	 }

	 public boolean addPassenger(Charactr newPassenger) {
		  if (numPassengers == maxPassengers || newPassenger == null) 
				return false;
		  passengers[numPassengers] = newPassenger;
		  numPassengers++;
		  return true;
	 }

    /*
     * init()
     * initializes all instances of Spaceships
     */
    public static void init() {

	Charactr.init();
	SpaceshipHash = new Hashtable();

	Charactr[] characterArray = Charactr.getCharInitArray();
	AllShips = new Spaceship[10];
	AllShips[0] = new Spaceship("Explorer",2,2,4,8,characterArray[5],'r'); 
	AllShips[1] = new Spaceship("Galactic Freighter",0,1,0,16,null,'r'); 
	AllShips[2] = new Spaceship("Interstellar Sloop",2,1,2,4,null,'r');  
	AllShips[3] = new Spaceship("Planetary Privateer",3,2,3,6,characterArray[13],'r'); 
	AllShips[4] = new Spaceship("Solar Merchant",0,1,1,14,characterArray[8],'r');  
	AllShips[5] = new Spaceship("Star Cruiser",1,2,2,10,null,'r');  
	AllShips[6] = new Spaceship("Stellar Courier",2,3,4,4,null,'r'); 
	AllShips[7] = new Spaceship("S-XIII",0,4,6,5,null,'r');  
	AllShips[8] = new Spaceship("Redjac's Spaceship",2,3,4,4,characterArray[22],'i');  
	AllShips[9] = new Spaceship("Spaceship",1,1,2,4,null,'i'); 

	SpaceshipHash.put("Explorer",AllShips[0]);
	SpaceshipHash.put("Galactic Freighter",AllShips[1]);
	SpaceshipHash.put("Interstellar Sloop",AllShips[2]);
	SpaceshipHash.put("Planetary Privateer",AllShips[3]);
	SpaceshipHash.put("Solar Merchant",AllShips[4]);
	SpaceshipHash.put("Star Cruiser",AllShips[5]);
	SpaceshipHash.put("Stellar Courier",AllShips[6]);
	SpaceshipHash.put("S-XIII",AllShips[7]);
	SpaceshipHash.put("Redjac's Spaceship",AllShips[8]);
	SpaceshipHash.put("Spaceship",AllShips[9]);
    }
    boolean flag = false;
    public boolean boardShip(Spaceship ship, Charactr[] c){
	for (int i=0;i<c.length;i++){
	    System.out.println("shipPassengers.size() = "+shipPassengers.size());
	    System.out.println("ship.maxPassengers = "+ship.maxPassengers);
	    if (ship.maxPassengers > shipPassengers.size()){
		ship.shipPassengers.add(c[i]);
		c[i].setShip(ship);
		c[i].setAboardShip(true);
		System.out.println("Passenger "+(i+1)+" is aboard");
		flag = true;
	    }
	    if (flag == false)
		return(false);
	}
	return(true);
    }
    public void leaveShip(Spaceship ship, Charactr[] c){
	for (int i=0;i<c.length;i++){
	    ship.shipPassengers.remove(c[i]);
	    c[i].setShip(null);
	    c[i].setAboardShip(false);
	    if (c[i].getIsPilot() == true){
		c[i].setIsPilot(false);
		ship.pilot = null;
		ship.hasPilot = false;
	    }
	}
    }
    public boolean setShipPilot(Spaceship ship, Charactr c){
	if (c.getShip() == ship){
	    ship.pilot = c;
	    ship.hasPilot = true;
	    return(true);
	}
	else
	    return(false);
    }
}

