package com.javacodegeeks.android.androidtimerexample;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button startButton;
	private Button pauseButton;
	
	private TextView timerValue;
	
	private long startTime = 0L;
	
	private Handler customHandler = new Handler();
	
	long timeInMilliseconds = 0L;
	long timeSwapBuff = 0L;
	long updatedTime = 0L;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		timerValue = (TextView) findViewById(R.id.timerValue);

		startButton = (Button) findViewById(R.id.startButton);
		
		startButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View view) {
				startTime = SystemClock.uptimeMillis();
				customHandler.postDelayed(updateTimerThread, 0);

			}
		});

		pauseButton = (Button) findViewById(R.id.pauseButton);
		
		pauseButton.setOnClickListener(new View.OnClickListener() {
		
			public void onClick(View view) {
			
				timeSwapBuff += timeInMilliseconds;
				customHandler.removeCallbacks(updateTimerThread);

			}
		});

	}

	private Runnable updateTimerThread = new Runnable() 
	{

		public void run()
		{
				timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
				
				updatedTime = timeSwapBuff + timeInMilliseconds;
	
				int secs = (int) (updatedTime / 1000);
				int mins = secs / 60;
				secs = secs % 60;
				int milliseconds = (int) (updatedTime % 1000);
				timerValue.setText("" + mins + ":"
						+ String.format("%02d", secs) + ":"
						+ String.format("%03d", milliseconds));
				customHandler.postDelayed(this, 0);
		}

	};
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
	      getMenuInflater().inflate(R.menu.main, menu);
	      return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
	    switch (item.getItemId())
	    {
		   
		    case R.id.test1:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
	    		Intent intent_id1 = new Intent(this, Picker.class);
	            startActivity(intent_id1);     
	            return true;
		    case R.id.test2:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
	    		Intent intent_id2 = new Intent(this, Alarm.class);
	            startActivity(intent_id2);     
	            return true;    
	        
		    case R.id.test3:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "click", Toast.LENGTH_SHORT).show();
	    		Intent intent_id3 = new Intent(this, Clock.class);
	            startActivity(intent_id3);     
	            return true;        
	            
	            
		    case R.id.test4:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "insertion", Toast.LENGTH_SHORT).show();
	    		Intent intent_id4 = new Intent(this, Periodic.class);
	            startActivity(intent_id4);    
	            return true;
	            
		    case R.id.test5:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "insertion", Toast.LENGTH_SHORT).show();
	    		Intent intent_id5 = new Intent(this, CountDown.class);
	            startActivity(intent_id5);    
	            return true;   
	         
		    case R.id.test6:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "insertion", Toast.LENGTH_SHORT).show();
	    		Intent intent_id6 = new Intent(this, CountDown02.class);
	            startActivity(intent_id6);    
	            return true;   
	               
		    case R.id.repeat01:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "insertion", Toast.LENGTH_SHORT).show();
	    		Intent intent_id7 = new Intent(this, Repeat01.class);
	            startActivity(intent_id7);    
	            return true;       
	        
		    case R.id.Alarm02:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "Alarme02", Toast.LENGTH_SHORT).show();
	    		Intent intent_id8 = new Intent(this, Alarm02.class);
	            startActivity(intent_id8);    
	            return true;        
	            
		    case R.id.Alarm03:
		        // Comportement du bouton "Paramètres"
	    		Toast.makeText(getBaseContext(), "Alarme03", Toast.LENGTH_SHORT).show();
	    		Intent intent_id9 = new Intent(this, Alarm03.class);
	            startActivity(intent_id9);    
	            return true;            
	            
		    default:
		        return super.onOptionsItemSelected(item);
	    }
	}

}