import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
import java.awt.image.ImageObserver;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import javax.swing.JFrame;
/* 21 November 2001, 9pm -  */
/* 22 November 2001, 10pm - 11:30pm */
/* 23 November 2001, 10pm - 11:30pm */
/* 24 November 2001, 9pm - 1:30am */
/* 25 November 2001, on and off during the day. */

class Generate extends JFrame  {


  // This part is all about showing the thumbnails as they are created.
  static Generate frame = new Generate();
  Image theImage;
  Canvas canvas;
  int w = 200;
  int h = 200;
  public Generate() {
    super("Thumbnail");
    Container container = getContentPane();
    container.setBackground(Color.white);
    container.setForeground(Color.black);
    
    canvas = new Canvas() {
	public synchronized void paint(Graphics g) {
	  if (theImage != null) 
	    g.drawImage(theImage,
			(int)(w-theImage.getWidth(null))/2,
			(int)(h-theImage.getHeight(null))/2, this);
	  else
	    super.paint(g);
	}
      };
    canvas.setSize(new Dimension(w,h));
    container.add(canvas);
    pack();
    show();

  }

  // Thumbnails, after creation, call this to 
  // show themselves.
  static void putImage(Image img) {
    frame.theImage = img;
    frame.canvas.repaint();
  }

  public static void main(String[] args) {

  // Read in index.txt, and for each line of the form
  // TYPE base
  // process file base.txt,
  // in combination with subdirectory base
  //

    // Will receive all the pictures, what have you.
    Vector v = new Vector();

    try {
      BufferedReader in = new BufferedReader(new FileReader("index.txt"));
      int lineno = 0;
      String s = in.readLine();
      while (s != null) {
	
	PictureParameters pictureParameters = new PictureParameters();
	// process lines of the form "TYPE pagename"
         lineno++;
         StringTokenizer stok = new StringTokenizer(s);
         String type = null;
         String base = null;
         try {
           type = stok.nextToken();
         } catch (NoSuchElementException ex) {
	   s = in.readLine();
           continue; // ignore blank lines
         }
         try {
           base = stok.nextToken();
         } catch (NoSuchElementException ex) {
           System.err.println("Line " + lineno +
           " contained only one word instead of type and pagename");
	   s = in.readLine();
           continue;
         }

         while (stok.hasMoreTokens()) {
	   String tok = stok.nextToken();
	   int eq = tok.indexOf('=');
	   if (eq > 0) {
	     String sym = tok.substring(0,eq);
	     String val = tok.substring(eq+1);
	     Utility.set(pictureParameters, sym, val);
	   }
         }

	 System.err.println(pictureParameters.toString());

         if (type.equalsIgnoreCase("pictures")) {
	   Pictures p = new Pictures(base, pictureParameters);
	   if (p.error != null) {
	     System.err.println(p.error);
	   } else {
	     v.add(p);
	   }
         } else if (type.equalsIgnoreCase("wordypictures")) {
	   Pictures p = new WordyPictures(base, pictureParameters);
	   if (p.error != null) {
	     System.err.println(p.error);
	   } else {
	     v.add(p);
	   }
         } else if (type.equalsIgnoreCase("pageslides")) {
	   PageSlides p = new PageSlides(base, pictureParameters);
	   if (p.error != null) {
	     System.err.println(p.error);
	   } else {
	     v.add(p);
	   }
         } else if (type.equalsIgnoreCase("text")) {
	   TextPage p = new TextPage(base, pictureParameters);

	   if (p.error != null) {
	     System.err.println(p.error);
	   } else {
	     v.add(p);
	   }
	   
         } else {
           System.err.println("Line " + lineno +
                              " began with unknown type " + type);
         }

	 s = in.readLine();
      }
    } catch (Throwable ex) {
       ex.printStackTrace();
    }

    try {
      // Eventually, this will depend on what pages get made.
      
      for (int i = 0; i < v.size() ; i++) {
	AbstractPage p = (AbstractPage)(v.elementAt(i));
	Template t;
	if (p instanceof TextPage || p instanceof PageSlides) {
	  t = new NavTemplate((Template) null, v, p.title, /* rhs padding */ true);
	} else {
	  t = new NavTemplate((Template) null, v, p.title);
	}
	if (p.pictureParameters.style != null) t.bodyStyle = p.pictureParameters.style;
	p.makePage(t);
      }
    } catch (Throwable ex) {
      ex.printStackTrace();
    }

    System.exit(1);
  }
}
