package com.example.timecounter03;


import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

//http://www.androidhello.com/android-Examples-Sources.php?android-example-code=1042  a voir 

// http://abhiandroid.com/ui/chronometer

// http://sampleprogramz.com/android/chronometer.php

// https://stackoverflow.com/questions/5594877/android-chronometer-pause

// http://adilwaqar.blogspot.fr/2014/01/using-android-chronometerstartpause-stop.html


// ********************** https://stackoverflow.com/questions/5740516/chronometer-doesnt-stop-in-android

// ********************** http://abhiandroid.com/ui/chronometer

// http://blog.xuite.net/a88370.a88370/cjoushua/78316038-Android+%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-Chronometer

// http://developer.alexanderklimov.ru/android/views/chronometer.php

///////////////////////////////  acceleration 
// https://stackoverflow.com/questions/13264718/how-to-calculate-running-speed-using-accelerometer-sensor-in-android

public class MainActivity extends Activity {

    Chronometer simpleChronometer;
    Button start, pause, resume, clear;
    long timeWhenStopped;

    private long lastPause;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer);
        start = (Button) findViewById(R.id.startButton);
        pause = (Button) findViewById(R.id.pauseButton);
        resume = (Button) findViewById(R.id.resumeButton);
        clear = (Button) findViewById(R.id.clearButton);
        
        start.setEnabled(true);
        pause.setEnabled(false);
        resume.setEnabled(false);
        clear.setEnabled(false);
        
        //simpleChronometer.setFormat("HH:MM:SS");
        
        //formatChronometerText(simpleChronometer);
       //simpleChronometer.setText("00:00:00");
        
        ///android:format = "Timer: %s "
        
        simpleChronometer.setFormat("0%s");
        simpleChronometer.setFormat("00:%s");
        
        start.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                simpleChronometer.setBase(SystemClock.elapsedRealtime());
                simpleChronometer.start();
                start.setEnabled(false);
                pause.setEnabled(true);
                resume.setEnabled(false);
                clear.setEnabled(true);
            }
        });
        
        pause.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
            	lastPause = SystemClock.elapsedRealtime();
           	    simpleChronometer.stop();
           	    start.setEnabled(false);
           	    pause.setEnabled(false);
           	    resume.setEnabled(true);
           	    clear.setEnabled(true);
            }
        });
        
        resume.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
            	simpleChronometer.setBase(simpleChronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
            	simpleChronometer.start();
            	start.setEnabled(false);
            	pause.setEnabled(true);
           	    resume.setEnabled(false);
            	clear.setEnabled(true);
            }
        });
        
        clear.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
            	simpleChronometer.setBase(SystemClock.elapsedRealtime());
            	simpleChronometer.stop();
            	start.setEnabled(true);
                pause.setEnabled(false);
                resume.setEnabled(false);
                clear.setEnabled(false);
            }
        });

        
    }

    
}

/*
chrono.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(Chronometer chrono) {
        time = SystemClock.elapsedRealtime() - chrono.getBase();
        int h = (int) (time / 3600000);
        int m = (int) (time - h * 3600000) / 60000;
        int s = (int) (time - h * 3600000 - m * 60000) / 1000;
        String hh = h < 10 ? "0" + h : h + "";
        String mm = m < 10 ? "0" + m : m + "";
        String ss = s < 10 ? "0" + s : s + "";
        chrono.setText(hh + ":" + mm + ":" + ss);
        Log.i("SpeedB", "averageSpeed=" + averageSpeed + "; time ="
                + time + "; lol=" + (distanceM / (time / 1000))
                * 3.6);
    }
});
*/