import clock.io.ClockOutput;

public class ClockThread implements Runnable {
    long t;
    long diff;
    private ClockOutput out;
    private Monitor monitor;

    public ClockThread(Monitor monitor, ClockOutput out) {
        this.out = out;
        this.monitor = monitor;
        t = System.currentTimeMillis();
    }

    @Override
    public void run() {
        System.out.println("ClockThread started");

        while (true) {
            t += 1000;
            diff = t - System.currentTimeMillis();

            if (diff > 0) {
                try {
                    Thread.sleep(diff);
                    monitor.tick();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            NaiveTime time = monitor.getTime();

            out.displayTime(time.hour(), time.min(), time.sec());

            if (monitor.getAlarmStatus()) {
                out.alarm();
            }
        }
    }
}