//import AttachmentInfo;

/**
 * A simple representation of mail messages.  Note that the message identifier
 * is separate from the header; it is a value that the server might use
 * when returning messages.
 *
 * I commented out the attachment info, since we have not talked of dealing
 * with them
 * @author Samuel A. Rebelsky
 * @author Joshua Vickery
 * @version 1.2 of October 1999
 */
public class MailMessage {
  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+
  
  /** 
   * The header of the message.  This specifies sender, recipient,
   * and so on and so forth.
   */
  protected MessageHeader header;
  
  /**
   * A unique identifier for the message.  This is typically generated
   * by the mail server so that clients can refer to messages by
   * identifier.
   */
  protected String identifier;
  
  /**
   * The body of the message.
   */
  protected String body;
  
  /**
   * Information on any attachments the message may have.  This is
   * always non-null.
   */
    //protected AttachmentInfo[] attachments;
  
  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+
  
  /**
   * Build a complete mail message from one of the textual messages
   * typically sent across the Internet.  Also requires a local
   * identifier for the message.
   */
  public MailMessage(String message, int identifier) {
    // STUB.  To be implemented.
      //this(identifier);
  } // MailMessage(String,String)
  
  /**
   * Build a new, empty, mail message.  Requires a local identifier
   * that uniquely identifies the message.
   */
    // public MailMessage(String identifier) {
    //this.identifier = identifier;
    //this.body = "";
    //this.header = new MessageHeader();
    //this.attachments = new AttachmentInfo[0];
    //} // MailMessage(String)
  
  // +------------+----------------------------------------------
  // | Extractors |
  // +------------+
  
  /**
   * Get information on the nth attachment.  It is the responsibility of the 
   * client to then use that information to request the attachment from the
   * server.
   * @exception MessageException
   *   If there is no such attachment.
   */
    /*  public AttachmentInfo getAttachmentInfo(int n) 
	throws MessageException
	{
	if ((n < 0) || (n >= attachments.length)) {
	throw new MessageException("Request for attachment " + n + ", only 0 .." +
	(attachments.length-1));
	} // if the message number is invalid.
	return attachments[n];
	} // getAttachment(int)
    */
    
  /**
   * Get the body of the message.
   */
  public String getBody() {
    return this.body;
  } // getBody()
  
  /**
   * Get one field of the header.  Returns the empty string if the field
   * is not defined.
   */
  public String getField(String fieldName) {
    try {
      return this.header.getField(fieldName);
    }
    catch (Exception fe) {
      return "";
    }
  } // getField(String)
  
  /**
   * Get the string used to uniquely identify this message.
   */
  public String getId() {
    return this.identifier;
  } // getId()
  
  /**
   * Convert the message to a string, for ease of printing.
   */
  public String toString() {
    return this.toString(this.header.toString());
  } // toString()
  
  /**
   * Convert the message to a string, using only selected fields in the header.
   */
  public String toString(String[] headerFields) {
    return this.toString(this.header.toString(headerFields));
  } // toString(String[])
  
  /**
   * Convert the message to a string, using the selected string as the header.
   * This is primarily intended as a utility class because the other two
   * versions of toString() are otherwise quite similar, and it seems silly
   * to repeat the code.
   */
  protected String toString(String headerString) {
    String message = headerString + "\n" + this.body;
    /* if (this.attachments != null) {
	message = message + "\nAttachments:\n";
      for (int i = 0; i < this.attachments.length; ++i) {
      message = message + "  " + this.attachments[i] + "\n";
      } // for
      } // if (this.attachments != null) */
    return message;
  } // toString()
    
} // class MailMessage

