package hh.dejsem.fm;

import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

import hh.lib.D;

public class DialogFragmentDelete extends DialogFragment implements DialogInterface.OnClickListener {
	/**----------------------------------------------------------
	 * dialog for "delete selected" approve
	 * ----------------------------------------------------------*/
	static final String TAG = "approve delete dialog";

	D d;
	String title, message;
	boolean localFS;
	AlertDialog dialog;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		d.l(3, String.format("onCreateDialog"));
		setRetainInstance(true);
		if(dialog == null) {
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder
					.setCancelable(true)
					.setTitle(title)
					.setMessage(message)
					.setPositiveButton("OK", this)
					.setNegativeButton("Cancel", null);
			dialog = builder.create();
		}
		return dialog;
	}

	@Override
	public void onDestroyView() {
		d.l(3, "onDestroyView");
		Dialog dialog = getDialog();
		// Work around bug: http://code.google.com/p/android/issues/detail?id=17423
		if ((dialog != null) && getRetainInstance()) dialog.setDismissMessage(null);
		super.onDestroyView();
	}

	@Override
	public void onClick(DialogInterface dialog, int id) {
		final FM fm = localFS ? FragFM.localDirPanel.fm : FragFM.remoteDirPanel.fm;
		fm.iterateDelete();
	}

	void approveDelete(PanelDirView parentPanel, String title, String message) {
		this.d = parentPanel.d.klon(this);
		localFS = PanelLocalDir.class.isInstance(parentPanel);
		this.title = title;
		this.message = message;
		show(parentPanel.d.getFmgr(), TAG);
	}
}
