package net.cs76.lectures.Threads01;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

/*
 * Code1
 * Dan Armendariz
 * Computer Science E-76
 * 
 * Demonstrating Android's single-threaded model,
 * Handler/Runnable adherence to that model, and the
 * dreaded Application Not Responding (ANR) dialog.
 */

public class Code1 extends Activity implements OnClickListener {

	// UI objects
	private Button sleep, runnable;
	private CheckBox moreSleep;
	private ImageView img;
	
	// animation object
	private AnimationDrawable anim;
	
	// the amount of time (in seconds) we should sleep when asked
	private static final int S = 10;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// connect to UI objects
		sleep    = (Button)    findViewById(R.id.button_sleep);
		runnable = (Button)    findViewById(R.id.button_runnable);
		img      = (ImageView) findViewById(R.id.anim);
		moreSleep= (CheckBox)  findViewById(R.id.check_sleep);
		
		// set click listeners
		sleep.setOnClickListener(this);
		runnable.setOnClickListener(this);
		img.setOnClickListener(this);
	}

	/**
	 * Display a message to a user via a toast
	 */
	public void showMessage(CharSequence txt) {
		(Toast.makeText(this, txt, Toast.LENGTH_SHORT)).show();
	}
	
	
	/**
	 * Handle onClick events to UI elements.
	 */
	@Override
	public void onClick(View v) {
		switch(v.getId()) {

		case R.id.anim:
			// start animating when the image is clicked

			// set the image resource to use our animation drawable
			img.setImageResource(R.drawable.smiley);
			
			// cast the drawable to an animation and start it
			anim = (AnimationDrawable) img.getDrawable();
			anim.start();

			break;

		case R.id.button_sleep:
			// sleep for S seconds
			SystemClock.sleep(S * 1000);

			// Bonus! Try tapping on the screen as soon as the thread starts sleeping.

			// more info on time keeping:
			// http://developer.android.com/reference/android/os/SystemClock.html

			showMessage("Waited " + S + " seconds.");
			break;
		
		case R.id.button_runnable:
			// create a new handler
			h = new Handler();
			
			// have the handler post the runnable to the message queue in S seconds
			h.postDelayed(waitForS, S*1000);

			// notify the user that the runnable was posted
			showMessage("Runnable posted");
			break;
		}
	}


	// define a handler that will post our runnable to the queue
	private Handler h;
	
	// the class that contains code to be run
	private Runnable waitForS = new Runnable() {
		
		/**
		 * The method to be run when the queue gets to this runnable
		 */
		public void run() {
			// if requested, we'll sleep some additional time
			if(moreSleep.isChecked()) SystemClock.sleep(S * 1000);

			showMessage("Waited approx. " + S + " seconds.");
		}
	};


}