package com.growthbeat.message.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import com.growthbeat.message.GrowthMessage;
import com.growthbeat.message.model.PlainButton;
import com.growthbeat.message.model.PlainMessage;

public class PlainMessageFragment extends DialogFragment {

	public PlainMessageFragment() {
		super();
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		Object message = getArguments().get("message");
		if (message == null)
			return null;
		if (!(message instanceof PlainMessage))
			return null;

		final PlainMessage plainMessage = (PlainMessage) message;

		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());

		dialogBuilder.setTitle(plainMessage.getCaption());
		dialogBuilder.setMessage(plainMessage.getText());

		if (plainMessage.getButtons() != null && plainMessage.getButtons().size() >= 2) {

			final PlainButton positiveButton = (PlainButton) plainMessage.getButtons().get(0);
			dialogBuilder.setPositiveButton(positiveButton.getLabel(), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					GrowthMessage.getInstance().selectButton(positiveButton, plainMessage);
					if (!getActivity().isFinishing())
						getActivity().finish();
				}
			});

			final PlainButton negativeButton = (PlainButton) plainMessage.getButtons().get(1);
			dialogBuilder.setNegativeButton(negativeButton.getLabel(), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					GrowthMessage.getInstance().selectButton(negativeButton, plainMessage);
					if (!getActivity().isFinishing())
						getActivity().finish();
				}
			});

		}

		AlertDialog dialog = dialogBuilder.create();
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		dialog.setCanceledOnTouchOutside(false);

		return dialog;

	}
}

@@ -1036,70 +1036,539 @@
 s() 
-!
+=
 = null
- && plainMessage.getButtons().size() %3E= 2) %7B%0A%0A%09%09%09final 
+)%0A%09%09%09return null;%0A%0A%09%09final PlainButton positiveButton;%0A%09%09final PlainButton neutralButton;%0A%09%09final PlainButton negativeButton;%0A%0A%09%09switch (plainMessage.getButtons().size()) %7B%0A%09%09case 1:%0A%09%09%09positiveButton = (PlainButton) plainMessage.getButtons().get(0);%0A%09%09%09neutralButton = null;%0A%09%09%09negativeButton = null;%0A%09%09%09break;%0A%09%09case 2:%0A%09%09%09positiveButton = (PlainButton) plainMessage.getButtons().get(0);%0A%09%09%09neutralButton = null;%0A%09%09%09negativeButton = (PlainButton) plainMessage.getButtons().get(1);%0A%09%09%09break;%0A%09%09case 3:%0A%09%09%09positiveButton = (
 Plai
@@ -1566,37 +1566,142 @@
 n = (PlainButton
+)
  p
-osi
+lainMessage.getButtons().get(0);%0A%09%09%09neutralButton = (PlainButton) plainMessage.getButtons().get(1);%0A%09%09%09nega
 tiveButton = (Pl
@@ -1741,20 +1741,94 @@
 s().get(
-0
+2
 );%0A
+%09%09%09break;%0A%09%09default:%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09if (positiveButton != null) %7B%0A
 %09%09%09dialo
@@ -2159,60 +2159,279 @@
 %7D);%0A
-%0A%09%09%09final PlainButton negativeButton = (Plain
+%09%09%7D%0A%0A%09%09if (neutralButton != null) %7B%0A%09%09%09dialogBuilder.setNeutralButton(neutralButton.getLabel(), new DialogInterface.OnClickListener() %7B%0A%09%09%09%09@Override%0A%09%09%09%09public void onClick(DialogInterface dialog, int which) %7B%0A%09%09%09%09%09GrowthMessage.getInstance().selectButton(neutral
 Button
-)
+,
  pla
@@ -2439,37 +2439,137 @@
 nMessage
-.getButtons().get(1);
+);%0A%09%09%09%09%09if (!getActivity().isFinishing())%0A%09%09%09%09%09%09getActivity().finish();%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%09%09%7D%0A%0A%09%09if (negativeButton != null) %7B
 %0A%09%09%09dial
@@ -2897,17 +2897,16 @@
 %0A%09%09%09%7D);%0A
-%0A
 %09%09%7D%0A%0A%09%09A

Support one button and three buttons