package hh.dejsem.hack;

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.View;

import hh.dejsem.K;
import hh.lib.D;

public class HackViewDialog extends DialogFragment {

	public static interface ViewDialogView { View getView(); }

	static final String DIALOG_TAG_KEY = "TAG";
	static final String DIALOG_TYPE_KEY = "TYPE";
	static final int DIALOG_TYPE_VIEW = 0;
	static final int DIALOG_TYPE_TEXT = 1;
	static final String DIALOG_TITLE_KEY = "TITLE";
	static final String DIALOG_TEXT_KEY = "TEXT";
	static final String TAG = "notice dialog";

	D d;
	int dialogType = DIALOG_TYPE_TEXT;
	public ViewDialogView getView = null;
	String title = null;
	String msg = null;
	DialogInterface.OnClickListener lstnr;
	String tag;
	boolean restart = false;
	int counter = 0;
	AlertDialog dialog;

	@Override
	public void onSaveInstanceState(Bundle b) {
		super.onSaveInstanceState(b);
		restart = true;
		b.putString(DIALOG_TAG_KEY, tag);
		K.base.put(tag, this);
			/*if(dialogType == DIALOG_TYPE_TEXT) {
				b.putString(DIALOG_TITLE_KEY, title);
				b.putString(DIALOG_TEXT_KEY, msg);
			}*/
//			d.l(2, "onSaveInstanceState");
	}

	@Override
	public Dialog onCreateDialog(Bundle b) {
		if(b != null) restore(b.getString(DIALOG_TAG_KEY));
		restart = false;
		counter += 1;
		d.l("+++ onCreateDialog, counter=" + counter);

			/*if(b != null) {
				dialogType = b.getInt(DIALOG_TYPE_KEY, DIALOG_TYPE_TEXT);
				if(dialogType == DIALOG_TYPE_TEXT) {
					title = b.getString(DIALOG_TITLE_KEY);
					msg = b.getString(DIALOG_TEXT_KEY);
				}
			}*/

		if(true) {
			final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
			d.l("+++ onCreateDialog, setting view=" + getView.hashCode());
			if(dialogType == DIALOG_TYPE_VIEW && getView != null) builder.setView(getView.getView());
			else if(dialogType == DIALOG_TYPE_TEXT) builder.setTitle(title).setMessage(msg);
			d.l("+++ onCreateDialog, view set");
			builder.setCancelable(true).setPositiveButton("OK", lstnr);
			dialog = builder.create();
			return dialog;
		}
		else {
			final AlertDialog ad = (AlertDialog)getDialog();
			ad.setView(getView.getView());
			ad.setButton(DialogInterface.BUTTON_POSITIVE, "OK", lstnr);
			return ad;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public void display(D d, String tag, ViewDialogView getView, DialogInterface.OnClickListener lstnr) {
		this.getView = getView;
		dialogType = DIALOG_TYPE_VIEW;
		this.display(d, tag, lstnr);
	}

	public void display(D d, String tag, String title, String msg, DialogInterface.OnClickListener lstnr) {
		this.title = title;
		this.msg = msg;
		dialogType = DIALOG_TYPE_TEXT;
		this.display(d, tag, lstnr);
	}

	public void display(D d, String tag, DialogInterface.OnClickListener lstnr) {
		this.d = d.klon((Object)this);
		this.d.setFragment(this);
		this.d.l("+++ display");
		this.tag = tag;
		this.lstnr = lstnr;
		show(d.getFmgr(), tag);
	}

	private void restore(String tag) {
		final HackViewDialog df = (HackViewDialog) K.base.get(tag);
		this.d = df.d;
		this.d.setFragment(this);
		d.l("+++ restore");
		this.dialogType = df.dialogType;
		this.getView = df.getView;
		this.title = df.title;
		this.msg = df.msg;
		this.lstnr = df.lstnr;
		this.tag = df.tag;
		this.restart = df.restart;
		this.counter = df.counter;
	}
}
