import java.net.URL;
import java.net.MalformedURLException;
import java.awt.*;
import java.io.File;
import FrameWithImage;
import SimpleOutput;

/**
 * Provides images for the Gmail system.
 *
 * @version 1.0 of 11/7/99
 * @author Gail Bonath
 * @author Joe Pipkins
 * @author Minna Mahlab
 * @author Kate Ducey
 */

public class Photo
    implements PhotoInt
{
    // +--------+--------------------------------------
    // | Fields |
    // +--------+
    

    /**
     *The user's name.
     */

    protected String user;

    /**
     * The username of the person in the picture
     */
    protected String sendername;
    
    /**
     * The null image used when getPhoto needs to return an image
     * if none is found.
     */
    protected Image temp;
    
    /**
     * The pathname of where the jpg file can be found
     */
    protected URL pathname;


    /**
     * The current frame
     */
    protected FrameWithImage frame;

    // +--------------+--------------------------------
    // | Constructors |
    // +--------------+
    
    public Photo(String sender, String user, boolean local) {
        SimpleOutput out = new SimpleOutput();
        out.println( "local = " + local);
        sendername = sender;
        if (local == true) {
            File tmp = new File ("/home/rebelsky/public_html/GMail/Photos/" + sendername + ".jpg");
            if ( tmp.exists()) {
                try{
                    pathname = new URL("http://www.math.grin.edu/~rebelsky/GMail/Photos/" + sendername + ".jpg"); 
                }//try
                catch (MalformedURLException e) {}
            } // if
            else {
                try {
                    pathname = new URL("http://www.math.grin.edu/~rebelsky/GMail/Photos/nophoto.jpg");
                }//try
                catch (MalformedURLException e) {}//catch
            }//else
        }//if   
        else {
            out.println( "made it to local=false");
            File tmp = new File ("/home/" + user + "/public_html/GMail_Photos/" + sendername + ".jpg");
            if (tmp.exists()) {
                try {
                    pathname =  new URL("http://www.math.grin.edu/~" + user + "/GMail_Photos/" + sendername + ".jpg");
                }//try
                catch (MalformedURLException e7) {}
            }//if
            else {
                 try {
                    pathname = new URL("http://www.math.grin.edu/~rebelsky/GMail/Photos/nophoto.jpg");
                }//try
                catch (MalformedURLException e) {}//catch
            } // else
        }//else
    }      // Photo(String)
    
    
    // +---------+---------------------------------------
    // | Methods |
    // +---------+
    
    /**
     * Takes in the username and returns a predetermined image.
     *
     * Photo.java will return an Image from database folder. If there
     * is no associated Image file for the username, an
     * exception will be thrown.
     */
    
    public Image getPhoto()
        throws Exception {
        try {
        return Toolkit.getDefaultToolkit().getImage(pathname);
        }
        catch (Exception e) {
            return temp;
                } //catch
    } // getPhoto
    
 
    /**
     * Creates a window which displays the photo
     */
    
    public void displayPhoto() {
        SimpleOutput out = new SimpleOutput();
        out.println( "pathname = " + this.pathname.toString());
        out.println( "username = " + this.sendername);

        try {
            this.frame  = new FrameWithImage(pathname, sendername);
            this.frame.show();
        } //try
        catch (Exception e) { 
            this.displayNull();
        } // catch
    } // display
 
    
    /**
     * Creates a window which displays a default "No Photo Available" 
     * photo
     */
    
    public void displayNull() {
        try { 
            URL nullpath= new URL ("http://www.math.grin.edu/~rebelsky/GMail/Photos/nophoto.jpg");
            try{
               this.frame = new FrameWithImage(nullpath , "No Photo Available");
               this.frame.show();
            }//try
            catch (Exception e) {}
        }//try
        catch (MalformedURLException e) {}

    } // display

    /** 
     * Closes the current frame
     */
    
    public void closeWindow() {
        this.frame.dispose();
    }//closeWindow

        
} // Photo

