import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.font.*;

public class DirectionPanel extends Panel
{
  int current = 0;
  int total = 0;
  Vector directions = new Vector();
  Vector titles = new Vector();
  Label title = new Label();
  Label direction1 = new Label();
  Label direction2 = new Label();
  //Label direction3 = new Label();
  Label back = new Label("[Back]");
  Label next = new Label("[Next]");

  public DirectionPanel(int width, int height)
  {
    int count = 0;
    Panel p1 = new Panel(new BorderLayout(0, 0));
    Panel p2 = new Panel(new FlowLayout());
    Panel p3 = new Panel(new BorderLayout(180, 0));

    Font headingFont = new Font("TimesRoman", Font.BOLD, 20);
    Font navigationFont = new Font("TimesRoman", Font.PLAIN, 16);
    Font textFont = new Font("TimesRomam", Font.PLAIN, 16);
    setSize(width, height);
    setLayout(new BorderLayout(0, 0));
    setBackground(Color.white);

    add(title, BorderLayout.NORTH);

    direction1.setSize(500, 10);
    direction1.setFont(textFont);
    p3.add(direction1, BorderLayout.NORTH);
    direction2.setSize(500, 10);
    direction2.setFont(textFont);
    p3.add(direction2, BorderLayout.CENTER);
    //direction3.setSize(500, 10);
    //direction3.setFont(textFont);
    //p3.add(direction3, BorderLayout.SOUTH);

    p1.add(p3, BorderLayout.NORTH);
    p2.add(back);
    p2.add(next);
    p1.add(p2, BorderLayout.SOUTH);

    add(p1, BorderLayout.CENTER);

    back.addMouseListener(new BackListener());
    next.addMouseListener(new NextListener());

    back.setForeground(Color.blue);
    next.setForeground(Color.blue);
    back.setFont(navigationFont);
    next.setFont(navigationFont);
    title.setFont(headingFont);
    next.setAlignment(next.CENTER);
    back.setAlignment(back.CENTER);
  }

  public void addDirection(String Title, String Direction)
  {
    total++;

    titles.addElement(Title);
    directions.addElement(Direction);
    title.setText((String)titles.elementAt(0));
    write((String)directions.elementAt(0));
  }

  public void write(String text)
  {
    int length = text.length();
    int lg1 = 85, lg2 = 170, charnum = 110;

    if(length < charnum)
    {
      direction1.setText(text.substring(0, length));
      direction2.setText("");
      //direction3.setText("");
    }
    else
    {
      lg1 = wraplength(text.substring(0, charnum));
      direction1.setText(text.substring(0, lg1));

      if(length - lg1 < charnum)
      {
        direction2.setText(text.substring(lg1 + 1, length));
        //direction3.setText("");
      }
      else
      {
        lg2 = wraplength(text.substring(0, lg1 + charnum));
        direction2.setText(text.substring(lg1 + 1, lg2));
        //direction3.setText(text.substring(lg2 + 1));
      }
    }
  }

  public int wraplength(String text)
  {
    int length = text.length();

    for(; length > 0; length--)
    {
      if(text.charAt(length - 1) == ' ')
      {
        return length - 1;
      }
    }

    return 85;
  }

  public void nextDirection()
  {
    if(current < total - 1)
    {
      current++;
      title.setText((String)titles.elementAt(current));
      write((String)directions.elementAt(current));
    }
  }

  public void previousDirection()
  {
    if(current > 0)
    {
      current--;
      title.setText((String)titles.elementAt(current));
      write((String)directions.elementAt(current));
    }
  }

  class BackListener extends MouseAdapter
  {
    public void mousePressed(MouseEvent e)
    {
      previousDirection();
    }
  }

  class NextListener extends MouseAdapter
  {
    public void mousePressed(MouseEvent e)
    {
      nextDirection();
    }
  }
}

