package com.github.timnew.rubiktimer;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.widget.TextView;

import com.github.timnew.rubiktimer.typefaces.KozGoProTypeface;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
import com.googlecode.androidannotations.annotations.SystemService;
import com.googlecode.androidannotations.annotations.ViewById;

@EBean
public class GameTimer {
    private static final String TIMER_TAG = "Game Timer";
    private static final int UPDATE_INTERVAL = 10;
    public static final int REFRESH_TIMER = 101;

    @RootContext
    protected Activity activity;

    @Bean
    protected KozGoProTypeface typeface;

    @SystemService
    protected PowerManager powerManager;

    @ViewById(R.id.timer)
    protected TextView timerView;

    private PowerManager.WakeLock wakeLock;
    private long totalTime = 0;
    private long startTime = 0;
    private boolean isTiming = false;

    private Handler uiCallback;

    @AfterViews
    protected void afterViews() {
        typeface.applyTo(timerView);

        wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TIMER_TAG);

        uiCallback = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what != REFRESH_TIMER)
                    return false;

                if (isTiming) {
                    uiCallback.sendEmptyMessageDelayed(REFRESH_TIMER, UPDATE_INTERVAL);
                    updateTimer();
                }

                return true;
            }
        });

        refresh();
    }

    public void toggle() {
        if (isTiming)
            pause();
        else
            restart();
    }

    public void start() {
        if (isTiming)
            return;

        isTiming = true;
        startTime = System.currentTimeMillis();
//        wakeLock.acquire();

        uiCallback.sendEmptyMessageDelayed(REFRESH_TIMER, UPDATE_INTERVAL);

        updateTimer();
    }

    private void updateTimer() {
        long now = System.currentTimeMillis();
        totalTime += (now - startTime);
        startTime = now;

        refresh();
    }

    public void refresh() {
        long seconds = totalTime / 1000;
        long min = seconds / 60;
        seconds %= 60;
        long milli = totalTime % 1000;

        String timeText = String.format("%02d:%02d.%03d", min, seconds, milli);
        timerView.setText(timeText);
    }

    public void pause() {
        if (!isTiming)
            return;

        isTiming = false;

        updateTimer();

//        wakeLock.release();
    }

    public void restart() {
        totalTime = 0;
        refresh();
        start();
    }

    public static class AlarmReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }
}

@@ -65,113 +65,8 @@
 ty;%0A
-import android.content.BroadcastReceiver;%0Aimport android.content.Context;%0Aimport android.content.Intent;%0A
 impo
@@ -630,97 +630,158 @@
 d;%0A%0A
-@EBean%0Apublic class GameTimer %7B%0A    private static final String TIMER_TAG = %22
+import static android.os.Handler.Callback;%0Aimport static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;%0A%0A@EBean%0Apublic class 
 Game
- 
 Timer
-%22;
+ %7B
 %0A   
@@ -1107,52 +1107,8 @@
 w;%0A%0A
-    private PowerManager.WakeLock wakeLock;%0A
     
@@ -1228,24 +1228,23 @@
 ndler ui
-Callback
+Updater
 ;%0A%0A    @
@@ -1338,113 +1338,17 @@
     
-wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TIMER_TAG);%0A%0A        uiCallback
+uiUpdater
  = n
@@ -1362,24 +1362,16 @@
 ler(new 
-Handler.
 Callback
@@ -1585,32 +1585,31 @@
           ui
-Callback
+Updater
 .sendEmptyMe
@@ -2048,38 +2048,8 @@
 s();
-%0A//        wakeLock.acquire();
 %0A%0A  
@@ -2056,24 +2056,23 @@
       ui
-Callback
+Updater
 .sendEmp
@@ -2140,24 +2140,85 @@
 dateTimer();
+%0A%0A        activity.getWindow().addFlags(FLAG_KEEP_SCREEN_ON);
 %0A    %7D%0A%0A    
@@ -2799,18 +2799,16 @@
 );%0A%0A
-//
         
 wake
@@ -2807,25 +2807,59 @@
     
-wakeLock.release(
+activity.getWindow().clearFlags(FLAG_KEEP_SCREEN_ON
 );%0A 
@@ -2960,174 +2960,7 @@
    %7D
-%0A%0A    public static class AlarmReceiver extends BroadcastReceiver %7B%0A%0A        @Override%0A        public void onReceive(Context context, Intent intent) %7B%0A%0A        %7D%0A    %7D
 %0A%7D%0A

Clean Code and keep screen on when timer is running