package course.examples.ThreadingHandlerMessages;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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;

import java.lang.ref.WeakReference;

public class HandlerMessagesActivity extends Activity {
	private final  int SET_PROGRESS_BAR_VISIBILITY = 0;
	private final  int PROGRESS_UPDATE = 1;
	private final  int SET_BITMAP = 2;
	//------Otra forma
    private final  int SHOW_PROGRESS_BAR = 3;
    private final  int HIDE_PROGRESS_BAR = 4;

    private ImageView mImageView;
	private ProgressBar mProgressBar;

	private int mDelay=500;

	private class MyHandler extends Handler {
//        private final WeakReference<HandlerMessagesActivity> mActivity;
//
//        public MyHandler(HandlerMessagesActivity activity) {
//            mActivity = new WeakReference<HandlerMessagesActivity>(activity);
//        }

        public void handleMessage(Message msg) {
            //if (mActivity.get() != null) {
                switch (msg.what) {
                    case SET_PROGRESS_BAR_VISIBILITY:
                        mProgressBar.setVisibility((Integer) msg.obj);
                        break;
                    case PROGRESS_UPDATE:
                        mProgressBar.setProgress((Integer) msg.obj);
                        break;
                    case SET_BITMAP:
                        mImageView.setImageBitmap((Bitmap) msg.obj);
                        break;
                    //Otra forma
                    case SHOW_PROGRESS_BAR:
                        mProgressBar.setVisibility(ProgressBar.VISIBLE);
                        break;
                    case HIDE_PROGRESS_BAR:
                        mProgressBar.setVisibility(ProgressBar.INVISIBLE);
                        break;
                }
            //}
        }
    }

    //private final MyHandler myHandler = new MyHandler(this);
    private final MyHandler myHandler = new MyHandler();

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

		mImageView = (ImageView) findViewById(R.id.imageView);
		mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

		final Button button = (Button) findViewById(R.id.loadButton);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
			    //Creamos el runable
			    LoadIconTask loadIconTask = new LoadIconTask(R.drawable.painter);
			    //Lo lanzamos en el worker thread
				new Thread(loadIconTask).start();
			}
		});

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

	}

	//¿Que pasa si esta clase la hubieramos declarado fuera de la MainActivity o incluso en otro Package o fichero?
	private class LoadIconTask implements Runnable {
		private final int resId;
		//private final Handler handler;

		LoadIconTask(int resId){//}, Handler handler) {
			this.resId = resId;
			//this.handler = handler;
		}

		public void run() {

		    Message msg = myHandler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY,ProgressBar.VISIBLE);
		    //Otra forma
            //Message msg = myHandler.obtainMessage(SHOW_PROGRESS_BAR);
			myHandler.sendMessage(msg);

			final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);

			for (int i = 1; i < 11; i++) {
				sleep();
				msg = myHandler.obtainMessage(PROGRESS_UPDATE, i * 10);
                myHandler.sendMessage(msg);
			}

			msg = myHandler.obtainMessage(SET_BITMAP, bitmap);
            myHandler.sendMessage(msg);

			msg = myHandler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY,ProgressBar.INVISIBLE);
            //Otra forma
            //Message msg = myHandler.obtainMessage(HIDE_PROGRESS_BAR);
            myHandler.sendMessage(msg);
		}

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