import java.util.*;
/**
 * Sovereign is the class which handles Sovereigns in Freedom in
 * the Galaxy.
 * A Sovereign is a character which has absolute control over a given
 * Environ.  A Sovereign is also highly respected and obeyed by his
 * subjects.
 * @author	<a href="mailto:jburke@cs.nmsu.edu">James M Burke</a>
 * @version	Fri Nov 21 12:11:19 MST 2003 
 */
public class Sovereign extends Charactr {
   /**
    * Static counter of the number of sovereigns.
    */  
   private static int counter = 0;
   /** 
    * Static array with the list of environs which have obtained a
    * sovereign.  These environs are no longer allowed to summon.
    */
   private static int[] summoned = new int[13];
   /**
    * The name of the sovereign.
    */
   protected String name;
   /**
    * What side the Sovereign supports.  Valid values are "imperial", 
    * "rebel", or "neutral"
    */
   protected String type;
   /**
    * The assigned combat rating for the sovereign.
    */
   protected int combatRating;
   /**
    * The assigned endurance rating for the sovereign.
    */
   protected int enduranceRating;
   /**
    * The assigned leadership rating for the sovereign.
    */
   protected int leadershipRating;
   /**
    * The assigned leadership rating for the sovereign in space.
    */
   protected int spaceLeadershipRating;
   /**
    * The integer value for the Star System in which the sovereign
    * is assigned to.
    */
   protected int starSystem;
   /**
    * The integer value for the Planet and Environ in which the
    * sovereign is assigned to.
    */
   protected int environID;
   /**
    * The current status of the sovereign.  Value is set to 
    * <code>true</code> by default.
    */
   protected boolean isAlive;

   //public static void main (String[] args) {
   //   sovereign a = sovereign.get(4411);
   //   System.out.println("Name: " + a.name + "\n");
   //   System.out.println("Type: " + a.type);
   //}

   /**
    * Create a Sovereign object initialized with the values passed.
    *
    * @param name			The name of the sovereign.
    * @param type			Specifies who the sovereign 
    *					supports.  Valid responses are:
    *					"imperial", "rebel", or "neutral"
    * @param combatRating		The sovereign's combat rating.
    * @param enduranceRating		The sovereign's endurance rating.
    * @param leadershipRating		The sovereign's leadership rating.
    * @param spaceLeadershipRating	The sovereign's leadership rating
    *					while in space.
    * @param starSystem			The integer value given to the 
    *					Star System where the sovereign
    *					is located.
    * @param environID			The integer value given to the
    *					Planet + Environ where the 
    *					sovereign is located.
    */
   public Sovereign(String name, String type, int combatRating, 
      int enduranceRating, int leadershipRating, 
      int spaceLeadershipRating, int starSystem, int environID) 
   {
      super('?', name, combatRating, spaceLeadershipRating, (Environ)null, 
         "", "", "", "", "", enduranceRating, 0, leadershipRating, 0, 0, 
         0, false);
      this.name = name;
      this.type = type;
      this.combatRating = combatRating;
      this.enduranceRating = enduranceRating;
      this.leadershipRating = leadershipRating;
      this.spaceLeadershipRating = spaceLeadershipRating;
      this.starSystem = starSystem;
      this.environID = environID;
      this.isAlive = true;
   }

   /**
    * When given a unique environID, <code>get</code> returns a Sovereign
    * object containing the given data corresponding to the environID.  
    *
    * Note: Code does not currently have environID, currently uses
    * planetID.
    * @param environID		The integer value given to the Planet
    * 				+ Environ where the desired sovereign
    *              		is located.
    * @return			<code>Sovereign</code> if there was a 
    *				match found for the given environID.;
    *                           <code>null</code> if there was no match
    *                           found for the given environID.
    * @throws UnsupportedOperationException	If a Sovereign has already
    *                           been assigned to the specified environID.
    * @throws IllegalArgumentException		If the total number of
    *                           summoned sovereigns exceeds 12. 	
    * @see			Sovereign
    */
   public static Sovereign get(int environID) 
   {
      if (counter<12) {
         if (environID==4411) {
            if (searchArray(summoned,4411))
               alreadySummoned();
	    summoned[counter]=4411;
            counter++;      
            return new Sovereign("Yaldor","rebel",4,4,2,0,44,4411);
            }
         else if (environID==5111) {
            if (searchArray(summoned,5111))
               alreadySummoned();
            summoned[counter]=5111; 
            counter++;
            return new Sovereign("Inzenzia III","rebel",3,3,1,0,51,5111);
            } 
         else if (environID==1411) {
            if (searchArray(summoned,1411))
               alreadySummoned();
            summoned[counter]=1411;
            counter++; 
            return new Sovereign("Nan Nhuk","rebel",4,4,2,2,14,1411);
            }
         else if (environID==3122) { // need environ # 
            if (searchArray(summoned,3122))
               alreadySummoned();
            summoned[counter]=3122;
            counter++;
            return new Sovereign("Shirofune","rebel",5,5,2,0,31,3122);
            }
         else if (environID==2311) {
            if (searchArray(summoned,2311))
               alreadySummoned();
            summoned[counter]=2311;
            counter++;
            return new Sovereign("Ascaill","imperial",3,3,1,1,23,2311);
            }
         else if (environID==2232) {
            if (searchArray(summoned,2232))
               alreadySummoned();
            summoned[counter]=2232;
            counter++;
            return new Sovereign("Megda Sheels","imperial",2,2,1,0,22,2232);
            }
         else if (environID==4122) {
            if (searchArray(summoned,4122))
               alreadySummoned();
            summoned[counter]=4122;
            counter++;
            return new Sovereign("Odel Hobar","imperial",3,3,2,0,41,4122);
            }
         else if (environID==2121) {
            if (searchArray(summoned,2121))
               alreadySummoned();
            summoned[counter]=2121;
            counter++;
            return new Sovereign("Treb Eyro","imperial",3,3,2,0,21,2121);
            }
         else if (environID==3411) { // need environ #
            if (searchArray(summoned,3411))
               alreadySummoned();
            summoned[counter]=3411;
            counter++;
            return new Sovereign("Balgar","neutral",4,4,2,1,31,3411);
            }
         else if (environID==5411) {
            if (searchArray(summoned,5411))
               alreadySummoned();
            summoned[counter]=5411; 
            counter++;
            return new Sovereign("Darb Selesh","neutral",5,4,2,0,54,5411);
            }
         else if (environID==5211) {
            if (searchArray(summoned,5211))
               alreadySummoned();
            summoned[counter]=5211;
            counter++; 
            return new Sovereign("Tensok Phi","neutral",3,3,1,0,52,5211);
            }
         else if (environID==3221) {
            if (searchArray(summoned,3221))
               alreadySummoned();
            summoned[counter]=3221;
            counter++;
            return new Sovereign("Xela Grebb","neutral",3,3,2,0,32,3221);
            }
         else {
            return null;
            }
         }
      else
         throw new IndexOutOfBoundsException("Maximum number of 12 " +
            "Sovereigns has been reached.");
   }
   
   /**
    * Determines if an element exists in the given array.
    *
    * @param theArray		Contains the array of what to search
    * @param element		Contains the element wishing to be found
    * @return			<code>true</code> If the specified element
    *				exists in the specified array.;
    *				<code>false</code> If the specified 
    *                           element does not exist in the array.	
    */
   private static boolean searchArray(int[] theArray, int element) {
      for (int i=0; i<theArray.length; i++)
         if (theArray[i]==element) return true;
      return false;
   }

   /**
    * Throws an exception if a Sovereign has already been summoned.
    * The purpose of this is to cut down on the clutter in the get
    * method.
    * @throws UnsupportedOperationException Always
    */
   private static void alreadySummoned() {
      throw new UnsupportedOperationException("The given environ has " + 
         "already Summoned a Sovereign.");
   }
}
