package com.example.timecounter01;

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

public class MainActivity extends Activity {

	private Button bn_start;
	private Button bn_stop;
	private Button bn_reset;
	private TextView tx_time;
	private TextView tx_info;
	private Thread timeThread;
	private boolean threadFlag=true;
	private boolean startClickable=true;
	private boolean stopClickable=false;
	private Handler handler;
	private int count=0;

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

        bn_start=(Button)findViewById(R.id.bn_start);
        bn_stop=(Button)findViewById(R.id.bn_stop);
        bn_reset=(Button)findViewById(R.id.bn_reset);
        tx_info=(TextView)findViewById(R.id.tx_info);
        tx_time=(TextView)findViewById(R.id.tx_time);

        handler=new Handler()
        {
        	@Override
        	public void handleMessage(Message msg)
        	{
        		if(msg.what==0)
        		{
        			count++;
        			int min=count/60;
        			int sec=count%60;        		
        			tx_time.setText(String.format("%1$02d:%2$02d",min,sec));
        		}

        		if(msg.what==1)
        		{        			
        			tx_info.setText("aaa");          			
        		}

        		if(msg.what==2)
        		{        			
        			tx_info.setText("bbb");            			
        		}

        		if(msg.what==3)
        		{
        			tx_info.setText("ccc");
        			tx_time.setText("");
        		}
        	}
        	
        };

        bn_start.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(timeThread==null&&startClickable)
				{
					threadFlag=true;
					GetThread();
					timeThread.start();
					Message message=new Message();
					message.what=1;
					handler.sendMessage(message);
					startClickable=false;
					stopClickable=true;
				}
			}
		});
        bn_stop.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(timeThread!=null&&stopClickable)
				{
					threadFlag=false;
					timeThread=null;
					Message message=new Message();
					message.what=2;
					handler.sendMessage(message);
					stopClickable=false;
				}
			}
		});
        bn_reset.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(stopClickable==false)
				{
					count=0;
					Message message=new Message();
					message.what=3;
					handler.sendMessage(message);
					startClickable=true;
				}				
			}
		});
    }
    private void GetThread()
    {
    	   timeThread=new Thread(new Runnable() {
   			
   			public void run() {
   				// TODO Auto-generated method stub
   				while(threadFlag)
   				{
   					try {
   						Thread.sleep(1000);
   					} catch (InterruptedException e) {
   						e.printStackTrace();
   					}
   					Message message=new Message();
   					message.what=0;
   					handler.sendMessage(message);
   				}
   			}
   		});
    }
}