/*
 * Copyright (c) 2016, Apptentive, Inc. All Rights Reserved.
 * Please refer to the LICENSE file for the terms and conditions
 * under which redistribution and use of this file is permitted.
 */

package com.apptentive.android.sdk.module.engagement.interaction.view.survey;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.apptentive.android.sdk.R;
import com.apptentive.android.sdk.module.engagement.interaction.model.survey.RangeQuestion;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.NumberFormat;


public class RangeSurveyQuestionView extends BaseSurveyQuestionView<RangeQuestion> implements RadioButton.OnCheckedChangeListener {

	private static final NumberFormat defaultNumberFormat = NumberFormat.getInstance();
	private static final String KEY_VALUE_WAS_SELECTED = "value_was_selected";
	private static final String KEY_SELECTED_VALUE = "selected_value";

	private int min;
	private int max;
	private String minLabel;
	private String maxLabel;

	private RadioGroup radioGroup;

	private boolean valueWasSelected;
	private int selectedValue;

	public static RangeSurveyQuestionView newInstance(RangeQuestion question) {
		RangeSurveyQuestionView f = new RangeSurveyQuestionView();
		Bundle b = new Bundle();
		b.putString("question", question.toString());
		f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			try {
				question = new RangeQuestion(bundle.getString("question"));
			} catch (JSONException e) {
				// Nothing
			}
		}
		min = question.getMin();
		max = question.getMax();
		minLabel = question.getMinLabel();
		maxLabel = question.getMaxLabel();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = super.onCreateView(inflater, container, savedInstanceState);
		ViewGroup answerContainer = getAnswerContainer(v);
		ViewGroup answer = (ViewGroup) inflater.inflate(R.layout.apptentive_survey_question_range_answer, answerContainer, false);
		answerContainer.addView(answer);

		if (!TextUtils.isEmpty(minLabel)) {
			TextView minLabelTextView = (TextView) answer.findViewById(R.id.min_label);
			minLabelTextView.setText(minLabel);
		}
		if (!TextUtils.isEmpty(maxLabel)) {
			TextView maxLabelTextView = (TextView) answer.findViewById(R.id.max_label);
			maxLabelTextView.setText(maxLabel);
		}

		radioGroup = (RadioGroup) answer.findViewById(R.id.range_container);

		for (int i = min; i <= max; i++) {
			RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.apptentive_survey_question_range_choice, radioGroup, false);
			radioButton.setText(defaultNumberFormat.format(i));
			radioButton.setTag(i);
			radioButton.setOnCheckedChangeListener(this);
			radioGroup.addView(radioButton);
		}
		return v;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putBoolean(KEY_VALUE_WAS_SELECTED, valueWasSelected);
		outState.putInt(KEY_SELECTED_VALUE, selectedValue);
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
		super.onViewStateRestored(savedInstanceState);

		// Restore instance state after the fragment tries to to avoid any UI weirdness.
		if (savedInstanceState != null) {
			valueWasSelected = savedInstanceState.getBoolean(KEY_VALUE_WAS_SELECTED, false);
			selectedValue = savedInstanceState.getInt(KEY_SELECTED_VALUE, 0);
		}

		for (int i = 0; i < radioGroup.getChildCount(); i++) {
			RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
			if (valueWasSelected && (int) radioButton.getTag() == selectedValue) {
				radioButton.setChecked(true);
				return;
			}
		}
	}

	@Override
	public boolean isValid() {
		return !question.isRequired() || valueWasSelected;
	}

	@Override
	public Object getAnswer() {
		if (valueWasSelected) {
			try {
				JSONArray jsonArray = new JSONArray();
				JSONObject jsonObject = new JSONObject();
				jsonArray.put(jsonObject);
				jsonObject.put("value", selectedValue);
				return jsonArray;
			} catch (JSONException e) {
				// Return null;
			}
		}
		return null;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			selectedValue = (int) buttonView.getTag();
			valueWasSelected = true;
			fireListener();
		}
	}
}

@@ -2474,47 +2474,8 @@
 );%0A%0A
-%09%09if (!TextUtils.isEmpty(minLabel)) %7B%0A%09
 %09%09Te
@@ -2544,24 +2544,62 @@
 min_label);%0A
+%09%09if (!TextUtils.isEmpty(minLabel)) %7B%0A
 %09%09%09minLabelT
@@ -2632,48 +2632,66 @@
 %0A%09%09%7D
-%0A%09%09if (!TextUtils.isEmpty(maxLabel)) %7B%0A%09
+ else %7B%0A%09%09%09minLabelTextView.setVisibility(View.GONE);%0A%09%09%7D%0A
 %09%09Te
@@ -2765,18 +2765,56 @@
 abel);%0A%09
+%09if (!TextUtils.isEmpty(maxLabel)) %7B%0A
 %09%09
+%09
 maxLabel
@@ -2840,24 +2840,81 @@
 xLabel);%0A%09%09%7D
+ else %7B%0A%09%09%09maxLabelTextView.setVisibility(View.GONE);%0A%09%09%7D
 %0A%0A%09%09radioGro

If Range question labels are missing, GONE them. ANDROID-694