/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.impressremote.communication;

import java.util.concurrent.TimeUnit;

import android.os.Handler;

public class Timer implements Runnable {
    public interface TimerListener {
        public void onTimerUpdated();
    }

    private static final long UPDATE_PERIOD_IN_MINUTES = 1;

    private final Handler mTimerHandler;
    private final TimerListener mTimerListener;

    private int mTotalMinutes;
    private int mPassedMinutes;

    public Timer(TimerListener aTimerListener) {
        mTimerHandler = new Handler();
        mTimerListener = aTimerListener;

        mTotalMinutes = 0;
        mPassedMinutes = 0;
    }

    public void setMinutesLength(int aLengthInMinutes) {
        mTotalMinutes = aLengthInMinutes;
    }

    public int getMinutesLength() {
        return mTotalMinutes;
    }

    public boolean isSet() {
        return mTotalMinutes != 0;
    }

    public void start() {
        if (!isSet()) {
            return;
        }

        mTimerHandler.postDelayed(this, TimeUnit.MINUTES.toMillis(UPDATE_PERIOD_IN_MINUTES));
    }

    @Override
    public void run() {
        updatePassedMinutes();

        mTimerListener.onTimerUpdated();

        start();
    }

    private void updatePassedMinutes() {
        mPassedMinutes++;
    }

    public void stop() {
        pause();
        reset();
    }

    public void pause() {
        mTimerHandler.removeCallbacks(this);
    }

    public void reset() {
        mTotalMinutes = 0;
        mPassedMinutes = 0;
    }

    public void resume() {
        start();
    }

    public boolean isTimeUp() {
        return getMinutesLeft() <= 0;
    }

    public int getMinutesLeft() {
        return mTotalMinutes - mPassedMinutes;
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

@@ -1251,77 +1251,443 @@
 lic 
-void start() %7B%0A        if (!isSet()) %7B%0A            return;%0A        %7D%0A
+int getMinutesLeft() %7B%0A        return mTotalMinutes - mPassedMinutes;%0A    %7D%0A%0A    public boolean isTimeUp() %7B%0A        return getMinutesLeft() %3C= 0;%0A    %7D%0A%0A    public void start() %7B%0A        if (!isSet()) %7B%0A            return;%0A        %7D%0A%0A        tearDownTimerHandler();%0A        setUpTimerHandler();%0A    %7D%0A%0A    private void tearDownTimerHandler() %7B%0A        mTimerHandler.removeCallbacks(this);%0A    %7D%0A%0A    private void setUpTimerHandler() %7B
 %0A   
@@ -1830,21 +1830,23 @@
         
-updat
+increas
 ePassedM
@@ -1943,13 +1943,15 @@
 oid 
-updat
+increas
 ePas
@@ -2090,33 +2090,40 @@
 use() %7B%0A        
-m
+tearDown
 TimerHandler.rem
@@ -2118,37 +2118,17 @@
 rHandler
-.removeCallbacks(this
+(
 );%0A    %7D
@@ -2270,173 +2270,8 @@
    %7D
-%0A%0A    public boolean isTimeUp() %7B%0A        return getMinutesLeft() %3C= 0;%0A    %7D%0A%0A    public int getMinutesLeft() %7B%0A        return mTotalMinutes - mPassedMinutes;%0A    %7D
 %0A%7D%0A%0A

Fix possible timer duplicates.