import java.io.*;
import java.util.*;
import java.lang.reflect.*;

class PageSlide {

  PictureParameters parameters;
  int numberOfPanels=1;
  String originalFileName;
  String baseFileName;
  File baseDir;
  String pageFileName;
  String pageImageName;
  String descriptiveText;
  String pageContents;

  public void makePage(Template t) throws IOException {
    PrintStream output = new PrintStream(new BufferedOutputStream(new FileOutputStream(pageFileName)));
    t.prefix(output, 1);
    output.println(pageContents);
    t.suffix(output, 1);
    output.close();
  }

  String pageFileReferenceNoDir() {
    return URLEncoder.encode(baseFileName) + ".html";
  }

  PageSlide (PageSlides p, PictureParameters pp) {
    parameters = pp;
    String s = p.lastLine;
    baseFileName = s.substring(0, s.lastIndexOf('.'));
    baseDir  = p.dir;

    originalFileName  = baseDir + "/" + s;

    pageFileName      = p.pageDir + "/" + baseFileName + ".html";

    /* Inhale contents of the slide page. */
    StringBuffer sb = new StringBuffer();
    BufferedReader in = null;
    try {
      in = new BufferedReader(new FileReader(originalFileName));
      s = in.readLine();
      while (s != null) {
	sb.append(s);
	sb.append('\n');
	s = in.readLine();
      } 
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    pageContents = sb.toString();

    /* Inhale any trailing descriptive text from the index. */
    sb = new StringBuffer();
    try {
      p.read();
      while (p.lastLine != null && !p.empty()) {
	sb.append(p.lastLine);
	sb.append('\n');
	p.read();
      } 
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    descriptiveText = sb.toString();
    System.err.println("Picture text is \n" + descriptiveText);
  }
}
