import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Clock
{
    // Clock Model
    private long startTime;

    public long getTime() {      
      // TODO get time from start to now in milliseconds
    }

    public void reset() {
      // TODO set start time to now
    }
}

// ############################################################################
class UpdateRequest implements Runnable
{
    private Clock     clock;
    private ClockView view;

    public UpdateRequest(Clock clock, ClockView view) {
        this.clock = clock;
        this.view  = view;
    }

    public void run() {
      // TODO display the left time in the ClockView, this task runs every UPDATE_INTERVAL
    }
}

class Ticker extends Thread
{
    private final static long UPDATE_INTERVAL = 10; // Milliseconds
    private UpdateRequest updateReq;

    public Ticker(Clock clock, ClockView view) {
        // TODO initialize UpdateRequest and start thread
    }

    public void run() {
        try {
            while(!isInterrupted()) {
              // TODO Request Update of GUI and wait intervall
            }
        }
        catch(InterruptedException e) { }
    }



}

class ClockPresenter implements ActionListener
{
    protected Clock     clock;
    protected ClockView view;
    private Ticker ticker;

    public void setModelAndView(Clock clock, ClockView view)  {
        this.clock = clock;
        this.view  = view;
    }

    // this runs in the GUI task
    public void actionPerformed(ActionEvent e)  {
        String s = e.getActionCommand();
        if(s.equals("Start"))  {
          // TODO reset clock and start the ticker thread if not started
        }        
        else if(s.equals("Stopp"))  {
          // TODO stop the clock (ticker) if running
        }
        else if(s.equals("Null")) {
          // TODO reset clock and update
        }
        else if(s.equals("Ende")) {
          // TODO shut down process
        }
    }
}

// ############################################################################
class ClockView
{
    private ClockPresenter presenter;
    private JLabel  label;

    public ClockView( ClockPresenter presenter) {
        this.presenter = presenter;
        initView();
    }

    private void initView() {
        JFrame f = new JFrame("Uhr");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(0, 1));
        label = new JLabel("", SwingConstants.RIGHT);
        f.add(label);
        JButton b1 = new JButton("Start");
        f.add(b1);
        JButton b2 = new JButton("Stopp");
        f.add(b2);
        JButton b3 = new JButton("Null");
        f.add(b3);
        JButton b4 = new JButton("Ende");
        f.add(b4);
        b1.addActionListener(presenter);
        b2.addActionListener(presenter);
        b3.addActionListener(presenter);
        b4.addActionListener(presenter);
        f.setLocation(300, 50);
        f.setSize(150, 200);
        f.setVisible(true);
    }

    public void showTime(long elapsedTime) {
      // TODO formats time and set to label, Format  (seconds : milliseconds)
    }
}

// ############################################################################
public class ClockManager
{
    public static void main(String[] args) {
        ClockPresenter h  = new ClockPresenter();
        ClockView    view = new ClockView( h );
        Clock clock       = new Clock();
        h.setModelAndView(clock, view);
        //h.reset();
    }
}
