package com.rehivetech.beeeon.gui.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.avast.android.dialogs.core.BaseDialogFragment;
import com.rehivetech.beeeon.R;

/**
 * Created by vico on 30.6.2015.
 */
public class ConfirmDialog extends BaseDialogFragment {
	public static String TAG = "confirmDialog";

	public static int TYPE_DELETE_GATE = 10;
	public static int TYPE_DELETE_USER = 11;
	public static int TYPE_DELETE_DEVICE = 13;
	public static int TYPE_CHANGE_OWNERSHIP = 20;

	private ConfirmDialogListener mCallback;

	private static final String EXTRA_TITLE = "extra_title";
	private static final String EXTRA_MESSAGE = "extra_message";
	private static final String EXTRA_BUTTON_TEXT_RES = "extra_button_text_res";

	private static final String EXTRA_CONFIRM_TYPE = "extra_confirm_type";
	private static final String EXTRA_DATA_ID = "extra_data_id";

	public static <T extends FragmentActivity & ConfirmDialogListener> void confirm(T activity, String title, String message, @StringRes int buttonTextRes, int confirmType, String dataId) {
		ConfirmDialog confirmDialog = new ConfirmDialog();

		Bundle args = new Bundle();
		args.putString(EXTRA_TITLE, title);
		args.putString(EXTRA_MESSAGE, message);
		args.putInt(EXTRA_BUTTON_TEXT_RES, buttonTextRes);
		args.putInt(EXTRA_CONFIRM_TYPE, confirmType);
		args.putString(EXTRA_DATA_ID, dataId);
		confirmDialog.setArguments(args);

		confirmDialog.show(activity.getSupportFragmentManager(), TAG);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mCallback = (ConfirmDialogListener) getActivity();
		} catch (ClassCastException e) {
			throw new ClassCastException(String.format("%s must implement ConfirmDialogListener", activity.toString()));
		}
	}

	@Override
	public BaseDialogFragment.Builder build(final BaseDialogFragment.Builder builder) {
		final Bundle args = getArguments();

		builder.setTitle(args.getString(EXTRA_TITLE));
		builder.setMessage(args.getString(EXTRA_MESSAGE));
		builder.setPositiveButton(args.getInt(EXTRA_BUTTON_TEXT_RES), new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mCallback.onConfirm(args.getInt(EXTRA_CONFIRM_TYPE), args.getString(EXTRA_DATA_ID));
				dismiss();
			}
		});
		builder.setNegativeButton(R.string.activity_fragment_btn_cancel, new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		return builder;
	}

	public interface ConfirmDialogListener {
		void onConfirm(int confirmType, String dataId);
	}
}
@@ -40,37 +40,8 @@
 g;%0A%0A
-import android.app.Activity;%0A
 impo
@@ -62,16 +62,16 @@
 Bundle;%0A
+
 import a
@@ -107,16 +107,56 @@
 ingRes;%0A
+import android.support.v4.app.Fragment;%0A
 import a
@@ -540,16 +540,69 @@
 R = 11;%0A
+%09public static int TYPE_DELETE_DASHBOARD_VIEW  = 12;%0A
 %09public 
@@ -689,51 +689,8 @@
 0;%0A%0A
-%09private ConfirmDialogListener mCallback;%0A%0A
 %09pri
@@ -1623,301 +1623,644 @@
 %7D%0A%0A%09
-@Override%0A%09public
+public static %3CT extends Fragment & ConfirmDialogListener%3E
  void 
+c
 on
-Attach(Activity activity) %7B%0A%09%09super.onAttach(activity);%0A%09%09try %7B%0A%09%09%09mCallback = (ConfirmDialogListener) getActivity();%0A%09%09%7D catch (ClassCastException e) %7B%0A%09%09%09throw new ClassCastException(String.format(%22%25s must implement ConfirmDialogListener%22, activity.toString()));%0A%09%09%7D
+firm(T fragment, String title, String message, @StringRes int buttonTextRes, int confirmType, String dataId) %7B%0A%09%09ConfirmDialog confirmDialog = new ConfirmDialog();%0A%0A%09%09Bundle args = new Bundle();%0A%09%09args.putString(EXTRA_TITLE, title);%0A%09%09args.putString(EXTRA_MESSAGE, message);%0A%09%09args.putInt(EXTRA_BUTTON_TEXT_RES, buttonTextRes);%0A%09%09args.putInt(EXTRA_CONFIRM_TYPE, confirmType);%0A%09%09args.putString(EXTRA_DATA_ID, dataId);%0A%09%09confirmDialog.setArguments(args);%0A%09%09confirmDialog.setTargetFragment(fragment, confirmType);%0A%0A%09%09confirmDialog.show(fragment.getFragmentManager(), TAG);
 %0A%09%7D%0A
@@ -2640,21 +2640,134 @@
 ) %7B%0A
+%0A
 %09%09%09%09
-mCallback
+for (ConfirmDialogListener listener : ConfirmDialog.this.getDialogListeners(ConfirmDialogListener.class)) %7B%0A%09%09%09%09%09listener
 .onC
@@ -2838,16 +2838,23 @@
 A_ID));%0A
+%09%09%09%09%7D%0A%0A
 %09%09%09%09dism

Improve ConfirmDialog to use with fragments