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

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

/**
 * This class models all aspects of Characters, both the cardboard "unit"
 * and the card used in missions and combat.  The reason it is not named
 * Character is to avoid a name conflict with java.lang.
 * You can read more about class
 * Charactr in the <A href="doc-files/index.html">
 * design documentation</A>.
 */

public class Charactr extends Unit { 
    // Instance Variables 
    private String homePlanet, title, race, specialDraws, iconURL, loyalty, name; 
    private int endurance, intelligence, leadership, spaceLeadership, diplomacy, navigation, life; 
    private boolean isDetected, inPlay, isPilot, isLeader, isHealing, aboardShip, isPrisoner, isActive; 
    private Vector possessions, mUnitsLed;
    private ImageIcon icon; 

    protected char groupType, side;
    protected Spaceship ship;

    protected Vector MiliUnit=new Vector();
	 private static Charactr[] charArr;
	 
   public Charactr(char s, String nm, int com, int spaclead, Environ env) {
      side = s;
      name = nm;
      combat = com;
      spaceLeadership = spaclead;
      environ = env;
    }

	 /**
	  * This constructor initializes the attributes of a FITG character by inputting
	  * those values explicitly
	  */
     public Charactr(char s, String nm, int com, int spaclead, Environ env, String hp, String t, String r, String sd, String iu, int e, int i, int le, int d, int nv, int li, boolean pil) {
		  side = s;
		  name = nm;
		  combat = com;
	          spaceLeadership = spaclead;
		  environ = env;
		  homePlanet = hp;
		  unitType = 'c';
		  mUnitsLed = new Vector();

    } // end first Charactr constructor
    
    /**
     * This constructor initializes the attributes of a FITG character
     * by inputing and parsing a String, implicitly from the charactr data file.
     */
    public Charactr(String input) {
        int i = 0, curField = 1;
        StringBuffer inBuffer = new StringBuffer();
        input = input + "#";
        while(i < input.length()) {
            if(input.charAt(i) != ',' && input.charAt(i) != '#') {
                inBuffer.append(input.charAt(i));

            } // end if stmt
            else {
                processField(inBuffer, curField);
                curField++;
                inBuffer = new StringBuffer();
            } // end else stmt
            i++;
        } // end while loop

        
        isDetected = inPlay = isPilot = isLeader = isHealing = false;


	unitType='c';
        isDetected = inPlay = isPilot = isLeader = isHealing = false; 

        aboardShip = isPrisoner = false;
        isChar = true;
	ship = null;
        life = endurance; 
	//environ.init();         
	possessions = new Vector(); 
	mUnitsLed = new Vector();
	groupType = 'c';
	orbit = null;

    } // end second Charactr constructor
    
    /**
     * This constructor takes the string from the varu-or-whatever.dat file and
     * looks up the rest of the info according to that
     * Note that the dummyvariable just gives this a different function header
     * from the above but actually serves no purpose!
     */
    public Charactr(String inputString, int dummyvariable){
        int i = inputString.indexOf(",");
        if( i == -1 ){
            System.out.println("Error in .dat source file, character initialization");
            System.exit(0);
        }
        if( inputString.charAt(0) == 'R' ) loyalty = "Rebel";
        else loyalty = "Imperial";
        name = inputString.substring(i+1, inputString.length() - 1);
        
        
    } // end third Charactr constructor
    
    /**
     * This method processes a String to initialize the values of a character.
     */
    private void processField(StringBuffer inBuffer, int curField) {
        switch (curField) {
            case 1:
                name = inBuffer.toString();
                break;
            case 2:
                iconURL = inBuffer.toString();
                //icon = new ImageIcon(ClassLoader.getSystemResource(inBuffer.toString()));
					 icon = new ImageIcon("images/" + iconURL);
                break;
            case 3:
                if (inBuffer.length() != 0)
                    title = inBuffer.toString();
                else
                    title = "None";
                break;
            case 4:
                race = inBuffer.toString();
                break;
            case 5:
                side = inBuffer.toString().charAt(0);
                break;
            case 6:
                combat = (int)inBuffer.charAt(0) - 48;
                break;
            case 7:
                endurance = (int)inBuffer.charAt(0) - 48;
                break;
            case 8:
                intelligence = (int)inBuffer.charAt(0) - 48;
                break;
            case 9:
                leadership = (int)inBuffer.charAt(0) - 48;
                break;
            case 10:
                diplomacy = (int)inBuffer.charAt(0) - 48;
                break;
            case 11:
                navigation = (int)inBuffer.charAt(0) - 48;
                break;
            case 12:
                homePlanet = inBuffer.toString();
                break;
            case 13:
                specialDraws = inBuffer.toString();
                break;
            default:
                System.out.println("Surpassed accessible fields.  Input file may not be character dat file");
                break;
        } // end switch stmt
        return;
    } // end processField method
    
    // Accessor methods
    public String getCharName() {
        return name;
    }
    public String getHomePlanet() {
        return homePlanet;
    }
    public String getTitle() {
        return title;
    }
    public String getRace() {
        return race;
    }
    public boolean getIsActive(){
	return isActive;
    }
    public String getLocation() {
        return "unknown location";
        //    return location;
    }
    public String getLoyalty() {
        //return "unknown loyalty";
        return loyalty;
    }
    //public int getCombat() {
    /* add in any possessions' combat values */
    //return combat;
    //}
    public int getEndurance() {
        return endurance;
    }
    public int getIntelligence() {
        return intelligence;
    }
    public int getLeadership() {
        return leadership;
    }
    public int getSpaceLeadership() {
        return spaceLeadership;
    }
    public int getDiplomacy() {
        return diplomacy;
    }
    public int getNavigation() {
        return navigation;
    }
    public int getLife(){
        return life;
    }
    public boolean getIsDetected() {
        return isDetected;
    }
    public boolean getInPlay() {
        return inPlay;
    }
    public boolean getIsPilot() {
        return isPilot;
    }
    public boolean getIsLeader() {
        return isLeader;
    }
    public boolean getIsHealing() {
        return isHealing;
    }
    public boolean getAboardShip(){
        return aboardShip;
    }
    public boolean getIsPrisoner() {
        return isPrisoner;
    }

	 public char getSide() {
		  return side;
	 }

    public Vector getPossessions() { 
        return possessions; 
    } 
    public Vector getMUnitsLed() { 
        return mUnitsLed; 
    } 
    public ImageIcon getIcon() { 
        return icon; 
    } 
    public String getIconURL() { 

        return iconURL;
    }
    public Vector getActive(Vector chara){

       	Vector activeIndex = new Vector(1,2);
	Charactr tmp;
	int i = 0;

	while(i < chara.size()) {
		tmp = (Charactr)chara.elementAt(i);

		if(tmp.isActive) {
			activeIndex.add(tmp);
		}
		i++;
	}

	return activeIndex;

    }
    
    // Modifier methods
    //public void changeLocation(String l) {
    //location = l;
    //return;
    //}
    public void changeLife(int li) {
        life += li;
        if (life > endurance)
            life = endurance;
        if (life <= 0)
            setInPlay(false);
        return;
    }

    public String getSpecialDraws() { 
        return specialDraws; 
    } 
 
	 public static Charactr[] getCharInitArray() {
		  return charArr;
	 }


  public Spaceship getShip() { 
        return ship; 
    }
 
    public void setIsDetected(boolean d) { 
        isDetected = d; 
        return; 
    } 
    public void setInPlay(boolean p) { 
        inPlay = p; 
        return; 
    } 
    public void setIsLeader(boolean le) { 
        isLeader = le; 
        return; 
    } 
    public void setisHealing(boolean h) { 
        isHealing = h; 
        return; 
    } 
    public void addPossession(Object p) { 
        possessions.add(p); 
    } 
    public void setLeader(MilitaryUnit M) { 
        isLeader = true; 
        MiliUnit.add(M);
    } 
    public void removeLeadMilUnit(MilitaryUnit M)
    {
	if(MiliUnit.size()==1) isLeader=false;
	MiliUnit.remove(M);
    }
    public void addMUnitsLed(Charactr c) { 
        mUnitsLed.add(c); 
    } 
    public void removeMUnitsLed(Charactr c) { 
        mUnitsLed.remove(c); 
    } 
    public void setIsPilot(boolean b, Spaceship s) { 
	isPilot = b;
	ship = s;
        return; 

    }
    public void setIsPilot(boolean b) { 
	isPilot = b;
        return; 
    }
    public void setShip(Spaceship s) {
	ship = s;
    }
    public void setAboardShip(boolean b) {
        aboardShip = b;
    }

    /** Switch isActive between true and false */
    public void toggleActive() {
	if (isActive)
	    isActive = false;
	else
	    isActive = true;
	return;
    }
    
    
  

	 public void setIsPrisoner(boolean b) {
		  isPrisoner = b;
	 }

	 /**
	  * This method initializes all the characters in FITG (rebel and
	  * imperial) and places them into a static array.
	  */
	 public static void init() {
		  RandomAccessFile inputChar = null;
		  charArr = new Charactr[32];
		  String charText = null;
		  Charactr fitgChar; 
		  int i = 0;
		  try { 
            inputChar = new RandomAccessFile("datfiles/charactr2.dat", "r"); 

        } // end first try stmt
        catch(FileNotFoundException fne) {
            System.err.println("Can not open file datfiles/charactr2.dat");
            System.err.println("Error " + fne);
            System.exit(100);
        } // end first catch stmt
        try {
            charText = inputChar.readLine();
            while(charText != null) {
                //System.out.println("input: " + charText);
                fitgChar = new Charactr(charText);
                charArr[i] = fitgChar;
                charText = inputChar.readLine();
                i++;
            } // end while loop
        } // end second try stmt
        catch (IOException ioe) {
            System.err.println("End of file");
            System.err.println("Error " + ioe);
            System.exit(100);
        } // end second catch stmt
    } // end init method
} // end of class definition
