import java.awt.* ;
import java.awt.event.* ;

/*
---------------------------------------------------------------------
 NeilW's jillDisplay Class
 (c) Neil Whitehouse 2015
---------------------------------------------------------------------
*/
public class jillDisplay extends Canvas implements ActionListener
{
    private Image  mainImage  ;
    private Image  workImage  ;
    private Thread workThread ;
    private String workString ;
    private String debugText  ;

    private int	workMode   ;
    private int	workIndex  ;
    private int	workHeight ;
    private int	workWidth  ;

    private final int SPAN     = 1 ;
    private final int J_NULL   = 0 ;
    private final int J_NORMAL = 1 ;
    private final int J_POUR   = 2 ;
    private final int J_SCROLL = 3 ;
    private final int J_DEBUG  = 9 ;

    /*
    -----------------------------------------------------------------
    Picture play canvas
    -----------------------------------------------------------------
    */
    public jillDisplay ()
    {
      this (null) ;
    }
    public jillDisplay (Image im)
    {
      super () ;

      if (im != null)
      { mainImage = im ;

        workMode  = J_NORMAL ;
        workIndex = 1 ;
      }
      else
      { mainImage = null;

        workMode  = J_NULL ;
        workIndex = 1 ;
      }
      debugText = new String ("Nothing to report.") ;
    }
    /*
    -----------------------------------------------------------------
    Update - This is a show this build next type update.
    -----------------------------------------------------------------
    */
    public void update (Graphics g)
    {
      switch (workMode)
      { case J_POUR:
          showPlay (g) ;
          makePour ()  ;
          workIndex++  ;
          break ;
        case J_SCROLL:
          showPlay (g)  ;
          makeScroll () ;
          workIndex++   ;
          break ;
        default:
          paint (g) ;
          break ;
      }
    }
    /*
    -----------------------------------------------------------------
    Paint
    -----------------------------------------------------------------
    */
    public void paint (Graphics g)
    {
      super.paint (g) ;

      switch (workMode)
      { case J_NULL:
          g.setColor   (Color.blue) ;
          g.drawString ("NULL", 50, 50) ;
          break ;
        case J_NORMAL:
          g.drawImage  (mainImage, 0, 0, this) ;
          break ;
/*      case J_POUR:
          showPlay (g) ;
          makePour ()  ;
          workIndex++  ;
          break ;
        case J_SCROLL:
          showPlay (g)  ;
          makeScroll () ;
          workIndex++   ;
          break ;
*/      case J_DEBUG:
          g.setColor   (Color.red) ;
          g.drawString ("Josie: " + debugText, 50, 50) ;
          break ;
        default:
          g.setColor   (Color.blue) ;
          g.drawString ("Josie: Mode "  + workMode + " not plugged in yet!", 50, 50) ;
          break ;
       }
    }
    /*
    -----------------------------------------------------------------
    Display Iteration
    -----------------------------------------------------------------
    */
    private void showPlay (Graphics g)
    {
      g.drawImage (workImage, 0, 0, null) ;
    }
    /*
    -----------------------------------------------------------------
    Make Pour Iteration.
    -----------------------------------------------------------------
    */
    private boolean makePour ()
    {
      Graphics workGC = workImage.getGraphics () ;

      /* Blat in Main Image */
      workGC.drawImage (mainImage, 0, 0, null) ;

      int wx = 0 ;
      int wy = workHeight - (workIndex * SPAN) -1 ;
      int vx = wx ;
      int vy = wy ;

      if (wy < 0) return false ;

      while (vy > 0)
      { vy -= SPAN ;

        workGC.copyArea (wx, wy, workWidth, SPAN, 0, vy - wy) ;
      }
      return true ;
    }
    /*
    -----------------------------------------------------------------
    Make Scroll Iteration.
    -----------------------------------------------------------------
    */
    private boolean makeScroll ()
    {
      Graphics workGC = workImage.getGraphics () ;

      /* Blat in Main Image */
      workGC.drawImage (mainImage, 0, 0, null) ;

      int tx = 0 ;
      int ty = workIndex * SPAN ;
      int wx = 0 ;
      int wy = workHeight - ty -1 ;
      int dx = 0 ;
      int dy = - wy ;

      workGC.copyArea  (wx, wy, workWidth, workIndex * SPAN, dx, dy) ;
      workGC.drawImage (mainImage, tx, ty, null) ;

      return true ;
    }
    /*
    -----------------------------------------------------------------
    Action
    -----------------------------------------------------------------
    */
    public void actionPerformed (ActionEvent evt)
    {
      workString = evt.getActionCommand () ;

      if (workString.compareTo ("debug") == 0)
      { workMode = J_DEBUG ;
        repaint () ;
        return ;
      }

      debugText = new String (workString
                + " length: " + workString.length    ()
                + " key: "    + workString.substring (0, 4)
                + " value: "  + workString.substring (4, workString.length())) ;


      if (workString.length () > 4)
      { /** My Event, I made it, it belongs to me !
         *  Arguement is POURnnnn - delta timing n
         */
        workMode = J_NULL ;

        if (workString.substring (0, 4).compareTo ("POUR") == 0)
          workMode = J_POUR ;
        if (workString.substring (0, 4).compareTo ("SCRL") == 0)
          workMode = J_SCROLL ;

        if (workMode > J_NULL)
        { int timer ;

          try
          {
            timer = Integer.parseInt(workString.substring (4, workString.length()));
          }
          catch(NumberFormatException nfe)
          {
            timer = 0 ;
          }

          if ((mainImage != null) && (timer > 0))
          { workWidth  = mainImage.getWidth  (null) ;
            workHeight = mainImage.getHeight (null) ;
            workImage  = createImage (workWidth, workHeight) ;
            workIndex  = 1 ;

            Graphics workGC = workImage.getGraphics () ;

            workGC.drawImage (mainImage, 0, 0, null) ;

            workThread = new jillThread (this, workHeight, timer) ;
            workThread.setPriority (Thread.MAX_PRIORITY) ;
            workThread.start () ;
          }
        }
      }
    }
}
