package com.rehivetech.beeeon.gui.dialog;

import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.avast.android.dialogs.core.BaseDialogFragment;
import com.avast.android.dialogs.fragment.SimpleDialogFragment;
import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.controller.Controller;
import com.rehivetech.beeeon.gui.fragment.DeviceDetailFragment;
import com.rehivetech.beeeon.gui.fragment.DeviceDetailGroupModuleFragment;
import com.rehivetech.beeeon.household.device.Module;
import com.rehivetech.beeeon.util.Log;
import com.rehivetech.beeeon.util.UnitsHelper;

import net.simonvt.numberpicker.NumberPicker;

import java.util.ArrayList;

/**
 * Created by leo on 24.4.15.
 */
public class NumberPickerDialogFragment extends SimpleDialogFragment {

	public static String TAG = "jayne";

	private static Module sMModule;
	private static FragmentActivity mActivity;
	private static Fragment mFragment;

	public static void show(FragmentActivity activity, Module module, Fragment frg) {
		sMModule = module;
		mActivity = activity;
		mFragment = frg;
		new NumberPickerDialogFragment().show(activity.getSupportFragmentManager(), TAG);
	}

	@Override
	public BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) {
		builder.setTitle(getString(R.string.number_picker_dialog_dialog_title_actuator_set_value));
		LayoutInflater li = LayoutInflater.from(getActivity());
		View view = li.inflate(R.layout.fragment_dialog_number_picker, null);

		final NumberPicker numberPicker = (NumberPicker) view.findViewById(R.id.dialog_number_picker_numberpicker);
		TextView unitView = (TextView) view.findViewById(R.id.dialog_number_picker_unit);
		double value = sMModule.getValue().getDoubleValue();
		final ArrayList<String> tmp = new ArrayList<>();
		for (double i = value - 40.0; i < value + 40.0; i += 0.5) {
			tmp.add(String.valueOf(i));
		}

		numberPicker.setDisplayedValues(tmp.toArray(new String[tmp.size()]));
		numberPicker.setMinValue(0);
		numberPicker.setMaxValue(159);
		numberPicker.setValue(80);

		// UserSettings can be null when user is not logged in!
		SharedPreferences prefs = Controller.getInstance(mActivity).getUserSettings();

		UnitsHelper mUnitsHelper = (prefs == null) ? null : new UnitsHelper(prefs, mActivity);
		unitView.setText(mUnitsHelper.getStringUnit(sMModule.getValue()));

		builder.setView(view);
		builder.setPositiveButton(getString(R.string.activity_fragment_btn_set), new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//
				Log.d(TAG, "NUMBER PICKER selected index" + numberPicker.getValue() + " value " + numberPicker.getDisplayedValues()[numberPicker.getValue()]);
				if (mFragment instanceof DeviceDetailFragment) {
					((DeviceDetailFragment) mFragment).onSetTemperatureClick(
							Double.parseDouble(numberPicker.getDisplayedValues()[numberPicker.getValue()]),
							sMModule.getId());
				} else {
					((DeviceDetailGroupModuleFragment) mFragment).onSetTemperatureClick(
							Double.parseDouble(numberPicker.getDisplayedValues()[numberPicker.getValue()]),
							sMModule.getId());
				}
				dismiss();
			}
		});
		builder.setNegativeButton(getString(R.string.activity_fragment_btn_cancel), new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		return builder;
	}
}

@@ -996,24 +996,23 @@
 odule sM
-M
 odule;%0A
+
 %09private
@@ -1167,25 +1167,24 @@
  frg) %7B%0A%09%09sM
-M
 odule = modu
@@ -1846,17 +1846,16 @@
 lue = sM
-M
 odule.ge
@@ -2339,45 +2339,20 @@
 ();%0A
-%0A
 %09%09
-UnitsHelper mUnitsHelper =
+if
  (prefs 
 == n
@@ -2351,17 +2351,17 @@
 efs 
-=
+!
 = null) 
 ? nu
@@ -2360,17 +2360,30 @@
 ll) 
-? null : 
+%7B%0A%09%09%09unitView.setText(
 new 
@@ -2415,41 +2415,8 @@
 ity)
-;%0A%09%09unitView.setText(mUnitsHelper
 .get
@@ -2428,17 +2428,16 @@
 gUnit(sM
-M
 odule.ge
@@ -2447,16 +2447,20 @@
 lue()));
+%0A%09%09%7D
 %0A%0A%09%09buil
@@ -2986,33 +2986,32 @@
 e()%5D),%0A%09%09%09%09%09%09%09sM
-M
 odule.getId());%0A
@@ -3176,32 +3176,32 @@
 r.getValue()%5D),%0A
+
 %09%09%09%09%09%09%09sMModule.
@@ -3193,17 +3193,16 @@
 %09%09%09%09%09%09sM
-M
 odule.ge

Fix potential crash in NumberPickerDialogFragment