/*
Tiamat - a role-playing game framework
Party.java - a collection of Characters
 
 Copyright  (C) 2004 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat;

import java.util.ArrayList;
import com.huguesjohnson.tiamat.Character;
import com.huguesjohnson.tiamat.graphics.Sprite;

/** Party - a collection of Characters
 * @author Hugues Johnson
 */
public class Party{
/* begin public properties */
    //the members of this Party
    private ArrayList<Character> members;
    /** 
     * Returns the members (Characters) in the Party.
     * @return The members (Characters) in the Party.
     */
    public Character[] getMembers(){ 
        return(this.members.toArray(new Character[0])); 
    }
    /**
     * Sets the members in the Party.
     * @param members The new members in the Party.
     */
    public void setMembers(Character[] members){
        int length=members.length;
        this.members=new ArrayList<Character>(length);
        for(int index=0;index<length;index++){
            this.members.add(members[index]);
        }
        //set sprite followers
        length--;
        for(int index=0;index<length;index++){
            Sprite sprite=(this.members.get(index)).getSprite();
            Sprite follower=(this.members.get(index+1)).getSprite();
            sprite.setFollower(follower);
        }
    }
/* end public properties */

/* begin public methods */
    /** 
     * Returns the number of characters in this Party.
     * @return The number of characters in this Party. 
     */
    public int getMemberCount(){
        return(this.members.size());
    }
    /** 
     * Returns the character at index, null if index<0 || index>=getMemberCount().
     * @return The character at index, null if index<0 || index>=getMemberCount(). 
     */
    public PlayerCharacter getMember(int index){
        PlayerCharacter character=null;
        if((index>=0)&&(index<this.members.size())){
            character=(PlayerCharacter)this.members.get(index);
        }
        return(character);
    }
    /** 
     * Adds a character to the end of the Party.
     * @param character The Character to add.
     */
    public void add(PlayerCharacter character){
        this.addAt(character,this.members.size());
    }
    /** 
     * Adds a Character at the specified index.
     * Shifts all Character past index down a position.
     * Adds to the end of the list if index>=count.
     * Adds to the start of the list if index<=0 
     * @param character The Character to add.
     * @index The index to add the Character at.
     */
    void addAt(PlayerCharacter character,int index){
        if(index<0){
            index=0;
        }
        if(index>=this.members.size()){
            this.members.add(character);
        } else{
            this.members.add(index,character);
        }
        //set sprite followers
        int length=this.members.size()-1;
        if(index>0){ index--; }
        for(int index1=index;index1<length;index1++){
            Sprite sprite=((PlayerCharacter)this.members.get(index)).getSprite();
            Sprite follower=((PlayerCharacter)this.members.get(index+1)).getSprite();
            sprite.setFollower(follower);
        }        
    }
    /** 
     * Removes and returns the character at the specified index.
     * Moves members down as needed.
     * Returns null if index<0 || index>=getMemberCount(). 
     * @param index The index of the Character to remove.
     * @return The Character that was removed, null if index<0 || index>=getMemberCount().
     */
    public Character remove(int index){
        Character character=null;
        if((index>=0)&&(index<this.members.size())){
            character=this.members.get(index);
            this.members.remove(index); 
            //set sprite followers
            int length=this.members.size()-1;
            if(index>0){ index--; }
            for(int index1=index;index1<length;index1++){
                Sprite sprite=((PlayerCharacter)this.members.get(index)).getSprite();
                Sprite follower=((PlayerCharacter)this.members.get(index+1)).getSprite();
                sprite.setFollower(follower);
            }  
        }
        return(character);
    } 
    /** 
     * Swaps the players at the two indexes. 
     * @param index1 The index of the Character to swap.
     * @param index2 The index of the other Character to swap.
     */
    void swapMembers(int index1,int index2){
        int size=this.members.size();
        if((index1>=0)&&(index1<this.members.size())&&(index2>=0)&&(index2<this.members.size())){
            PlayerCharacter tempCharacter=new PlayerCharacter();
            tempCharacter=(PlayerCharacter)this.members.get(index1);
            this.members.set(index1,this.members.get(index2));
            this.members.set(index2,tempCharacter);
        }
        //set sprite followers (probably more sophisticated way to do this than brute force)
        int length=this.members.size()-1;
        for(int index=0;index<length;index++){
            Sprite sprite=((PlayerCharacter)this.members.get(index)).getSprite();
            Sprite follower=((PlayerCharacter)this.members.get(index+1)).getSprite();
            sprite.setFollower(follower);
        }        
    }
/* end public methods */

/* begin constructors */
    /** 
     * Creates an empty Party. 
     */
    public Party(){
        this.members=new ArrayList<Character>(0);
    }
    /** 
     * Creates a Party with a single member.
     * @param character The single member of the Party.
     */
    public Party(PlayerCharacter character){
        PlayerCharacter[] members=new PlayerCharacter[1];
        members[0]=character;
        this.setMembers(members);
    }
    /** 
     * Creates a Party with the specified members. 
     * @param members The members of the Party.
     */
     Party(PlayerCharacter[] members){
        this.setMembers(members);
     }
/* end constructors */
}