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 android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import hh.lib.D;

/**
 * dialog to choose move destination
 */
public class DialogFragmentMoveDest extends DialogFragment implements DialogInterface.OnClickListener {

	static final String TAG = "find move dest dialog";

	D d;
	String startWd;
	boolean localFS;
	AlertDialog dialog = null;
	PanelDirView moveDestPanel;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		setRetainInstance(true);
		d.l(3, String.format("onCreateDialog, fragment=%s, dialog=%s", this, dialog));

		if(dialog == null) {
			final RelativeLayout dirView = PanelDirView.DirListView.getLayout(d.getContext());
			moveDestPanel = localFS
					? new PanelLocalMoveDest(this.d, dirView, startWd)
					: new PanelServerMoveDest(this.d, dirView, startWd);
			FragFM.dialogDirPanel = moveDestPanel;  // když se refreshuje panel po modifikaci FS, bude se refreshovat i tenhle

			dialog = new AlertDialog(d.getContext()) {
				public boolean onMenuItemSelected(int featureId, MenuItem item) {
					return handleContextItemSelected(item);
				}
			};

			dialog.setCancelable(true);
			dialog.setCustomTitle(null);
			dialog.setView(dirView);
			dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Move here", this);
			dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", (DialogInterface.OnClickListener) null);

			/*AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder
					.setCancelable(true)
					.setCustomTitle(null)
					.setView(dirView)
					.setPositiveButton("Move here", this)
					.setNegativeButton("Cancel", null);
			dialog = builder.create();*/
		}

		return dialog;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
		d.l(4, "dialog, onCreateContextMenu");
		moveDestPanel.buildContextMenu(menu, v, menuInfo);
		/*// musíme potlačit items hlavního menu, které si zadává hlavní aktivita, a které obsahují submenu,
		// protože v context menu v dialogu je potíž (zjištěno v API 22), že context menu se zobrazí normálně v popředí,
		// kdežto submenu se zobrazí POD dialogem a objeví se až po ukončení dialogu
		menu.setGroupVisible(R.id.menu_group_main, false);*/
	}

	boolean handleContextItemSelected(MenuItem item) {
		if(d.ll(4)) d.l("dialog, onMenuItemSelected, item=" + item);
		return moveDestPanel.handleContextItemSelected(item) ? true : super.onContextItemSelected(item);
	}

	@Override
	public void onClick(DialogInterface dialog, int id) {
		FragFM.dialogDirPanel = null;   // už je zbytečné panel refreshovat
		final PanelDirView parentPanel = localFS ? FragFM.localDirPanel : FragFM.remoteDirPanel;
		parentPanel.doMove(moveDestPanel.fm.getCwdName());
	}

	@Override
	public void onDestroyView() {
		d.l(3, "onDestroyView");
		// 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 onDestroy() {
		super.onDestroy();
		FragFM.dialogDirPanel = null;
		this.d.l(2, "onDestroy");
	}

	void moveDest(PanelDirView parentPanel, String startWd) {
		d = new D(this, String.format("%s.%s", parentPanel.d.logPrefix, getClass().getSimpleName()));
		this.startWd = startWd;
		localFS = PanelLocalDir.class.isInstance(parentPanel);
		show(parentPanel.d.getFmgr(), TAG);
	}
}