// $Id: RollingMessages.java,v 1.3 2004/06/14 20:49:53 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package com.meyling.principia.gui.puzzle;


import javax.swing.*;
import java.awt.*;


/**
 * Rollt eine Nachricht.
 *
 * @version     $Revision: 1.3 $
 * @author      Michael Meyling
 */
class RollingMessages extends JComponent {

    private int width;
    private int height;
    private Image im = null;
    private Graphics offscreen = null;
    private int position = 0;
    private int dc = 0;
    private int xoff = 5;
    private int yoff = 17;
    private Mover mover;

    final String[] messages;
    final boolean down;
    final int delta;
    final int screenWidth;
    int messageLength = 0;


    RollingMessages(final String[] messages) {
        super();
        this.messages = messages;
        this.down = true;
        this.delta = 2;
        this.screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    }


    RollingMessages(final String message) {
        super();
        this.messages = new String[] {message};
        this.down = false;
        this.delta = 2;
        this.screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    }


    void start() {
        try {
            width = getSize().width;
            height = getSize().height;
            if (down) {
                im = createImage(width, (this.messages.length + 2) * height);
                if (im == null) {
                    throw new IllegalArgumentException("call \"start()\" after adding instance"
                        + " to contentPane");
                };
                offscreen = im.getGraphics();
                offscreen.setColor( Color.gray );
                offscreen.fillRect( 0, 0, width, (this.messages.length + 3) * height);
                offscreen.setColor( Color.yellow );
                for (int i = 0; i < messages.length; i++) {
                    offscreen.drawString(this.messages[i], xoff, yoff + (i + 1) * height);
                }
                offscreen.drawString(this.messages[0], xoff, yoff +
                    (this.messages.length + 1) * height);
            } else {
                final String message = messages[0];
                messageLength = getGraphics().getFontMetrics().stringWidth(message);;
                im = createImage(2 * screenWidth + messageLength, height);
                if (im == null) {
                    throw new IllegalArgumentException("call \"start()\" after adding instance"
                        + " to contentPane");
                };
                System.out.println("height="+height);
                System.out.println("screenWidth="+screenWidth);
                System.out.println("messageLength="+messageLength);
                System.out.println("width="+(2 * screenWidth + messageLength));
                offscreen = im.getGraphics();
                offscreen.setColor( Color.gray );
                offscreen.fillRect( 0, 0, 2 * screenWidth + messageLength, height);
                offscreen.setColor( Color.yellow );
//                offscreen.drawString(message, screenWidth, yoff);
// TODO
//                offscreen.drawString(message, 10, 20);

// TODO ok:
                offscreen.drawString(message, 0, yoff);

                position = screenWidth - width;
// TODO
                position = 0;
            }
            this.mover = new Mover();
            mover.start();
        } catch(Exception e) {
            e.printStackTrace();
            offscreen = null;
        }

    }

    public void reshape(int x, int y, int width, int height) {
        this.width = width;
        this.height = height;
        super.reshape(x, y, width, height);
    }


    public void paint(Graphics g) {
        super.paint(g);
        if (offscreen != null) {
            if (down) {
                g.drawImage( im, 0, -position, this);
            } else {
                System.out.println("drawing at " + (-position));
                g.drawImage( im, -position, 0, this);
            }
        } else {
            g.setColor(this.getBackground());
            g.fillRect(0, 0, width, height);
        }
    }


    public void update(Graphics g) {
        paint(g);
    }


    private class Mover extends Thread {

        public void run() {
            while (true) {
                position += delta;
                if (down) {
                    if (position % height == 0){
                        try {
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e) {
                        }
                        if (im.getHeight(null) <= position + height) {
                            position = height;
                        }
                    } else {
                        try {
                            Thread.sleep(60);
                        } catch (InterruptedException e) {
                        }
                        RollingMessages.this.repaint();
                    }
                } else {
                    if (position > screenWidth + messageLength) {
                        System.out.println("stopping");
                        offscreen = null;
                        RollingMessages.this.repaint();
                        break;  // stop if message was scrolled once
//                        position = screenWidth - width;
                    } else {
                        try {
    //                        Thread.sleep(20);
                            Thread.sleep(60);
                        } catch (InterruptedException e) {
                        }
                        RollingMessages.this.repaint();
                    }
                }
            }
        }
    }


}
