package com.example.timecounter02;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

// https://github.com/wheam/TimeCounter

// https://github.com/thegenuinegourav/Counter-Clock-Timer-/blob/master/app/src/main/java/thegenuinegourav/timer/MainActivity.java

// https://github.com/draftbk/Android_TimeCounter/blob/master/app/src/main/java/com/example/slf/timecounter/MainActivity.java

// https://android--examples.blogspot.fr/2015/04/android-countdowntimer-start-pause.html

// http://www.androidhub4you.com/2013/04/countdown-demo-in-android-custom-timer.html

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

//https://stackoverflow.com/questions/27690540/how-to-pause-and-resume-chronometer-in-notification-panel

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

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.widget.Chronometer.OnChronometerTickListener;
//import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TextView;
import android.widget.Chronometer;


public class MainActivity extends Activity
{

    TextView tvData;
    Chronometer azerr;

    Button btnStart; 
    Button btnPause; 
    Button btnResume;
    Button btnCancel;
    
    //Declare a variable to hold count down timer's paused status
    private boolean isPaused = false;
    //Declare a variable to hold count down timer's paused status
    private boolean isCanceled = false;
    
    private long lastPause;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvData = (TextView) findViewById(R.id.tv_data);
        azerr = (Chronometer) findViewById(R.id.blabla);
        
        btnStart = (Button) findViewById(R.id.btn_start);
        btnPause = (Button) findViewById(R.id.btn_pause);
        btnResume = (Button) findViewById(R.id.btn_resume);
        btnCancel = (Button) findViewById(R.id.btn_cancel);
          
       
        //Initially disabled the pause, resume and cancel button
        btnPause.setEnabled(false);
        btnResume.setEnabled(false);
        btnCancel.setEnabled(false);
        
     //   chrono = (Chronometer) findViewById(R.id.chronometer);

     //   startChronometer();
        
        
        azerr.setOnChronometerTickListener(new OnChronometerTickListener() 
        {
            public void onChronometerTick(Chronometer cArg) 
            {
            	if(isPaused  == false)
            	{
            		Calendar calendar = Calendar.getInstance();
            		calendar.set(Calendar.HOUR_OF_DAY, 0);
            		calendar.set(Calendar.MINUTE, 0);
            		calendar.set(Calendar.SECOND, 0);
            		calendar.set(Calendar.MILLISECOND, getCurrentMiliSecondsOfChronometer());
            		tvData.setText(DateFormat.format("HH:mm:ss", calendar.getTime()));
            	}	
            }

			
        });
        
        
        btnStart.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //isPaused = false;
                //isCanceled = false;

                //Disable the start and pause button
                btnStart.setEnabled(false);
                btnResume.setEnabled(false);
                //Enabled the pause and cancel button
                btnPause.setEnabled(true);
                btnCancel.setEnabled(true);
                
                startChronometer();

               
            }
        });

      
        btnPause.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //When user request to pause the CountDownTimer
                isPaused = true;

                //Enable the resume and cancel button
                btnResume.setEnabled(true);
                btnCancel.setEnabled(true);
                //Disable the start and pause button
                btnStart.setEnabled(false);
                btnPause.setEnabled(false);
                
                //azerr.stop();
                
                
                
            }
        });
        
        

        //Set a Click Listener for resume button
        btnResume.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //Disable the start and resume button
                btnStart.setEnabled(false);
                btnResume.setEnabled(false);
                //Enable the pause and cancel button
                btnPause.setEnabled(true);
                btnCancel.setEnabled(true);

                //Specify the current state is not paused and canceled.
                isPaused = false;
                isCanceled = false;

                azerr.start();
                //startChronometer();

              
            }
        });
        
        
      //Set a Click Listener for cancel/stop button
        btnCancel.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //When user request to cancel the CountDownTimer
                isCanceled = true;

                //Disable the cancel, pause and resume button
                btnPause.setEnabled(false);
                btnResume.setEnabled(false);
                btnCancel.setEnabled(false);
                //Enable the start button
                btnStart.setEnabled(true);

               
            }
        });

        
        
    }

    private int getCurrentMiliSecondsOfChronometer() 
    {
    	
    	//if(isPaused  == false)
    	
    		
    	
		        int stoppedMilliseconds = 0;
		        String chronoText = azerr.getText().toString();
		        String array[] = chronoText.split(":");
		        if (array.length == 2) 
		        {
		            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000 + Integer.parseInt(array[1]) * 1000;
		        } 
		        else if (array.length == 3) 
		        {
		            stoppedMilliseconds =  Integer.parseInt(array[0]) * 60 * 60 * 1000 + Integer.parseInt(array[1]) * 60 * 1000 + Integer.parseInt(array[2]) * 1000;
		        }
		        
    	     
		        return stoppedMilliseconds;
    }

    private void startChronometer()
    {
        int stoppedMilliseconds = getCurrentMiliSecondsOfChronometer();
        azerr.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
        azerr.start();
    }
}