package com.yuji.ec.utility;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.AsyncTask;
import android.view.KeyEvent;

public class AsyncTaskCommand extends AsyncTask<String, Integer, String> {
	private WeakReference<Activity> activityRef;
	private AsyncTaskIF command;
	private ProgressDialog dialog;
	private boolean isCancel = false;
	private boolean isDialog = true;
	private int status = -1;
	
	private String title = "";
	private String buttonTitle = "";
	private boolean cancelable = true;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public AsyncTaskCommand(Activity activity, AsyncTaskIF command) {
		this.activityRef = new WeakReference(activity);
		this.command = command;
	}

	@Override
	protected void onPreExecute() {
		Debug.d(this, "onPreExecute start");

		Activity activity = activityRef.get();
		if (activity == null) {
			return;
		}
		if (isDialog) {
			dialog = new ProgressDialog(activity);
			dialog.setIndeterminate(true);
			dialog.setCancelable(cancelable);
			dialog.setTitle(title);
			if (cancelable){
				dialog.setButton(buttonTitle, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
						AsyncTaskCommand.this.cancel(true);
					}
				});
				dialog.setOnKeyListener(new OnKeyListener() {
					public boolean onKey(DialogInterface dialog, int keyCode,
							KeyEvent event) {
						switch (event.getKeyCode()) {
						case KeyEvent.KEYCODE_SEARCH:
							return true;
						}
						return false;
					}
				});
			}
			dialog.show();
		}
		Debug.d(this, "onPreExecute end");
	}

	@Override
	protected String doInBackground(String... arg0) {
		try {
			Debug.d(this, "doInBackground start");
			command.doExecute();
			status = command.getStatus();
			Debug.d(this, "doInBackground end");
		} catch (RuntimeException e) {
			Debug.d(this, null, e);
			throw e;
		}
		return null;
	}

	@Override
	protected void onCancelled() {
		Debug.d(this, "onCancelled start");

		isCancel = true;

		if (command != null) {
			command.cancel();
		}
		onPostExecute(null);
		Debug.d(this, "onCancelled end");
	}

	@Override
	protected void onPostExecute(String result) {
		Debug.d(this, "onPostExecute start");

		try {
			if (command != null) {
				command.done(isCancel);
				command = null;
			}
			if (dialog != null) {
				try {
					dialog.dismiss();
				}
				catch (IllegalArgumentException e){
					;
				}
				dialog = null;
			}
			activityRef = null;
		} catch (RuntimeException e) {
			Debug.d(this, null, e);
			throw e;
		}
		Debug.d(this, "onPostExecute end");
	}

	public void stop() {
		Debug.d(this, "stop start");

		if (command != null) {
			isCancel = true;
			command.cancel();
		}
		// onPostExecute()のdialog.dismiss();でエラーとなるため
		// 本質的な解決策ではない
		// dialog = null;
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}

		Debug.d(this, "stop end");
	}

	public void setDialog(boolean b) {
		isDialog = b;
	}
	
	public int getResultStatus(){
		return status;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setButtonTitle(String buttonTitle) {
		this.buttonTitle = buttonTitle;
	}
	
	public void setCancelable(boolean cancelable){
		this.cancelable  = cancelable;
	}
}
