package course.examples.ThreadingHandlerRunnableReuseTask;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class HandlerRunnableActivity extends Activity implements LoadIconTask.IconLoader{
	
	private ImageView mImageView;
	private ProgressBar mProgressBar;
	private Bitmap mBitmap;
	private int mDelay = 500;
	//private Handler handler = new Handler();
	public LoadIconTask loadIconTask;

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

		mImageView = (ImageView) findViewById(R.id.otherImageView);
		mProgressBar = (ProgressBar) findViewById(R.id.otherProgressBar);

		final Button button = (Button) findViewById(R.id.otherLoadButton);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Thread theThread;
				loadIconTask = new LoadIconTask(HandlerRunnableActivity.this);
				loadIconTask.setResId(R.drawable.painter);
				theThread = new Thread(loadIconTask);
				theThread.start();
			}
		});

		final Button otherButton = (Button) findViewById(R.id.otherOtherButton);
		otherButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(HandlerRunnableActivity.this, "I'm Working",
						Toast.LENGTH_SHORT).show();
			}
		});

		final Button btnActivity = (Button) findViewById(R.id.btnActivity);
		btnActivity.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(HandlerRunnableActivity.this, OtherActivity.class);
				startActivity(intent);
			}
		});

	}

	@Override
	public ProgressBar getProgressBar() {
		return mProgressBar;
	}

	@Override
	public ImageView getImageView() {
		return mImageView;
	}

/*	private class LoadIconTask implements Runnable {
		int resId;
		
		LoadIconTask(int resId) {

			this.resId = resId;
		}

		public void run() {

			handler.post(new Runnable() {
				@Override
				public void run() {
					mProgressBar.setVisibility(ProgressBar.VISIBLE);
				}
			});

			mBitmap = BitmapFactory.decodeResource(getResources(), resId);
			
			// Simulating long-running operation
			
			for (int i = 1; i < 11; i++) {
				sleep();
				final int step = i;
				handler.post(new Runnable() {
					@Override
					public void run() {
						mProgressBar.setProgress(step * 10);
					}
				});
			}

			handler.post(new Runnable() {
				@Override
				public void run() {
					mImageView.setImageBitmap(mBitmap);
				}
			});
			
			handler.post(new Runnable() {
				@Override
				public void run() {
					mProgressBar.setVisibility(ProgressBar.INVISIBLE);
				}
			});
		}
	}

	private void sleep() {
		try {
			Thread.sleep(mDelay);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}*/

}
