/**
  * Shows display channels picker dialog or displays an error toast if active channels have not yet
  * been introduced
  */
 public void onClickedDisplayChannelsButton(View displayChannelsButton) {
   // CHECKS IF CHANNELS TO ACTIVATE ARE ALREADY FILLED
   if (newConfiguration.getActiveSensors() != null
       && newConfiguration.getActiveChannelsNumber() != 0) {
     showDisplayChannels();
   } else {
     displayErrorToast(getString(R.string.nc_error_button_channels_to_display));
   }
 }
  /**
   * Initializes variables and widgets of the activity. The saveInstanceState bundle has always all
   * the configurations. And if the activity is created to modify a configuration it also has the
   * position of the configuration the user wish to update
   */
  @Override
  @SuppressWarnings("unchecked")
  protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ly_new_configuration);

    inflater = this.getLayoutInflater();
    configurations = new ArrayList<DeviceConfiguration>();
    configurations =
        (ArrayList<DeviceConfiguration>)
            getIntent().getExtras().getSerializable(ConfigurationsActivity.KEY_CONFIGURATIONS);

    // CREATES AND SETS DEFAULT VALUES FOR THE NEW CONFIGURATION
    newConfiguration = new DeviceConfiguration(this);
    newConfiguration.setVisualizationFrequency(DEFAULT_RECEPTION_FREQ);
    newConfiguration.setSamplingFrequency(DEFAULT_SAMPLING_FREQ);
    newConfiguration.setNumberOfBits(DEFAULT_NUMBER_OF_BITS);

    // GETS ALL THE VIEWS
    receptionfreqSeekbar = (SeekBar) findViewById(R.id.nc_reception_seekbar);
    samplingfreqSeekbar = (SeekBar) findViewById(R.id.nc_sampling_seekbar);
    receptionFreqEditor = (EditText) findViewById(R.id.nc_reception_freq_view);
    samplingFreqEditor = (EditText) findViewById(R.id.nc_sampling_freq_view);
    configurationName = (EditText) findViewById(R.id.dev_name);
    macAddress = (EditText) findViewById(R.id.nc_mac_address);
    activeChannelsTV = (TextView) findViewById(R.id.nc_txt_active_channels);
    displayChannelsTV = (TextView) findViewById(R.id.nc_txt_channels_to_show);

    // INIT SEEKBARS AND EDITORS TO DEFAULT STATE
    receptionfreqSeekbar.setOnSeekBarChangeListener(
        new OnSeekBarChangeListener() {
          public void onProgressChanged(SeekBar seekBar, int progress, boolean changedByUser) {
            if (changedByUser) {
              int valueToChangeRounded =
                  ALLOW_RECEPTION_FREQ.floor(progress + 1) - RECEPTION_FREQ_MIN;
              Log.i(
                  "TRYANDROID", "samplingprogress " + progress + " floor:" + valueToChangeRounded);

              receptionFreqEditor.setText(
                  String.valueOf(valueToChangeRounded + RECEPTION_FREQ_MIN));
              newConfiguration.setVisualizationFrequency(valueToChangeRounded + RECEPTION_FREQ_MIN);
            }
          }
          // needed for the listener
          public void onStartTrackingTouch(SeekBar seekBar) {}

          public void onStopTrackingTouch(SeekBar seekBar) {}
        });

    receptionFreqEditor.setOnEditorActionListener(
        new OnEditorActionListener() {
          @Override
          public boolean onEditorAction(
              TextView receptionFreqEditorTextView, int actionId, KeyEvent event) {
            boolean handled = false;

            if (actionId == EditorInfo.IME_ACTION_DONE) {
              String frequencyString = receptionFreqEditorTextView.getText().toString();
              if (frequencyString.compareTo("") == 0) frequencyString = "0";
              int newFrequency = Integer.parseInt(frequencyString);

              setReceptionFrequency(newFrequency);
              closeKeyboardAndClearFocus();
              handled = true;
            }
            return handled;
          }

          private void setReceptionFrequency(int newFrequency) {
            // accepted frequency
            if (newFrequency >= RECEPTION_FREQ_MIN && newFrequency <= RECEPTION_FREQ_MAX) {
              receptionfreqSeekbar.setProgress((newFrequency - RECEPTION_FREQ_MIN));
              newConfiguration.setVisualizationFrequency(newFrequency);
              // frequency introduced is too big
            } else if (newFrequency > RECEPTION_FREQ_MAX) {
              receptionfreqSeekbar.setProgress(RECEPTION_FREQ_MAX);
              receptionFreqEditor.setText(String.valueOf(RECEPTION_FREQ_MAX));
              newConfiguration.setVisualizationFrequency(RECEPTION_FREQ_MAX);
              displayErrorToast(
                  getString(R.string.nc_error_max_frequency) + " " + RECEPTION_FREQ_MAX + "Hz");
              // frequency introduced is too small
            } else {
              receptionfreqSeekbar.setProgress(0);
              receptionFreqEditor.setText(String.valueOf(RECEPTION_FREQ_MIN));
              newConfiguration.setVisualizationFrequency(RECEPTION_FREQ_MIN);
              displayErrorToast(
                  getString(R.string.nc_error_min_frequency) + " " + RECEPTION_FREQ_MIN + " Hz");
            }
          }

          private void closeKeyboardAndClearFocus() {
            receptionFreqEditor.clearFocus();
            InputMethodManager inputManager =
                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(
                getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
          }
        });

    samplingfreqSeekbar.setOnSeekBarChangeListener(
        new OnSeekBarChangeListener() {
          public void onProgressChanged(SeekBar seekBar, int progress, boolean changedByUser) {
            if (changedByUser) {
              samplingFreqEditor.setText(String.valueOf(progress + SAMPLING_FREQ_MIN));
              newConfiguration.setSamplingFrequency(progress + SAMPLING_FREQ_MIN);
            }
          }
          // needed for the listener
          public void onStartTrackingTouch(SeekBar seekBar) {}

          public void onStopTrackingTouch(SeekBar seekBar) {}
        });

    samplingFreqEditor.setOnEditorActionListener(
        new OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView currentView, int actionId, KeyEvent event) {
            boolean handled = false;

            if (actionId == EditorInfo.IME_ACTION_DONE) {
              String frequencyString = currentView.getText().toString();
              if (frequencyString.compareTo("") == 0) frequencyString = "0";
              int newFrequency = Integer.parseInt(frequencyString);

              setFrequency(newFrequency);
              closeKeyboardAndClearFocus();
              handled = true;
            }
            return handled;
          }

          private void setFrequency(int newFrequency) {
            // accepted frequency
            if (newFrequency >= SAMPLING_FREQ_MIN && newFrequency <= SAMPLING_FREQ_MAX) {
              samplingfreqSeekbar.setProgress((newFrequency - SAMPLING_FREQ_MIN));
              newConfiguration.setSamplingFrequency(newFrequency);
              // frequency introduced is too big
            } else if (newFrequency > SAMPLING_FREQ_MAX) {
              samplingfreqSeekbar.setProgress(SAMPLING_FREQ_MAX);
              samplingFreqEditor.setText(String.valueOf(SAMPLING_FREQ_MAX));
              newConfiguration.setSamplingFrequency(SAMPLING_FREQ_MAX);
              displayErrorToast(
                  getString(R.string.nc_error_max_frequency) + " " + SAMPLING_FREQ_MAX + "Hz");
              // frequency introduced is too small
            } else {
              samplingfreqSeekbar.setProgress(0);
              samplingFreqEditor.setText(String.valueOf(SAMPLING_FREQ_MIN));
              newConfiguration.setSamplingFrequency(SAMPLING_FREQ_MIN);
              displayErrorToast(
                  getString(R.string.nc_error_min_frequency) + " " + SAMPLING_FREQ_MIN + " Hz");
            }
          }

          private void closeKeyboardAndClearFocus() {
            samplingFreqEditor.clearFocus();
            InputMethodManager inputManager =
                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(
                getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
          }
        });

    /* ********* UPDATE CONFIGURATION CODE ********* */
    if (getIntent().getExtras().containsKey(ConfigurationsActivity.KEY_CONFIGURATION_POSITION))
      isUpdatingConfiguration = true;

    if (isUpdatingConfiguration) {
      oldConfiguration =
          configurations.get(
              getIntent().getExtras().getInt(ConfigurationsActivity.KEY_CONFIGURATION_POSITION));
      // FILL WIDGETS FIELDS WITH CONFIGURATION TO EDIT DETAILS
      configurationName.setText(oldConfiguration.getName());
      macAddress.setText(oldConfiguration.getMacAddress());
      receptionfreqSeekbar.setProgress(
          oldConfiguration.getVisualizationFrequency() - RECEPTION_FREQ_MIN);
      receptionFreqEditor.setText(String.valueOf(oldConfiguration.getVisualizationFrequency()));
      samplingfreqSeekbar.setProgress(oldConfiguration.getSamplingFrequency());
      samplingFreqEditor.setText(String.valueOf(oldConfiguration.getSamplingFrequency()));
      activeChannelsTV.setVisibility(View.VISIBLE);
      activeChannelsTV.setText(
          getString(R.string.nc_channels_to_activate) + " " + oldConfiguration.getActiveChannels());
      displayChannelsTV.setVisibility(View.VISIBLE);
      displayChannelsTV.setText(oldConfiguration.getDisplayChannelsWithSensors());

      // MODIFY VARIABLES FOR VALIDATION PURPOSES
      activeChannels = oldConfiguration.getActiveSensors();
      boolean[] boolArray = {true};
      channelsSelected = boolArray;
      configurations.remove(
          configurations.get(
              getIntent().getExtras().getInt(ConfigurationsActivity.KEY_CONFIGURATION_POSITION)));
      newConfiguration = oldConfiguration;
    }
  }
  /**
   * Creates and shows a dialog for picking which of the active channels the user wants to show on
   * his android device. At least one is required
   */
  private void showDisplayChannels() {

    final ArrayList<String> channels = new ArrayList<String>(); // example {channel 1, channel 5}
    final ArrayList<String> sensors =
        new ArrayList<String>(); // example {blood pressure, temperature}

    // FILL THE CHANNELS AND SENSORS ARRAYS FOR THE LIST ADAPTER
    String[] activeSensors = newConfiguration.getActiveSensors();
    for (int i = 0; i < activeSensors.length; i++) {
      if (activeSensors[i].compareTo("null") != 0) {
        channels.add(getString(R.string.nc_dialog_channel) + " " + (i + 1));
        sensors.add(activeSensors[i]);
      }
    }

    final DisplayChannelsListAdapter displayChannelsListAdapter =
        new DisplayChannelsListAdapter(
            this, channels, sensors, newConfiguration.getDisplayChannels());
    AlertDialog displayChannelsDialog;

    TextView customTitleView = (TextView) inflater.inflate(R.layout.dialog_custom_title, null);
    customTitleView.setText(R.string.nc_dialog_title_channels_to_display);

    // BUILDER
    AlertDialog.Builder displayChannelsBuilder = new AlertDialog.Builder(this);
    displayChannelsBuilder
        .setCustomTitle(customTitleView)
        .setView(getLayoutInflater().inflate(R.layout.dialog_channels_listview, null))
        .setPositiveButton(
            getString(R.string.nc_dialog_positive_button),
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {

                channelsSelected = displayChannelsListAdapter.getChecked();
                String[] displayChannels = new String[8];

                if (numberOfChannelsSelected(channelsSelected) == 0) {
                  displayChannelsTV.setText("");
                  displayChannelsTV.setVisibility(View.GONE);
                } else {
                  displayChannelsTV.setVisibility(View.VISIBLE);
                  displayChannelsTV.setError(null);
                  displayChannelsTV.setTextColor(getResources().getColor(R.color.blue));
                  convertBooleanToString(displayChannels, channelsSelected);
                  newConfiguration.setDisplayChannels(displayChannels);
                  displayChannelsTV.setText(newConfiguration.getDisplayChannelsWithSensors());
                }
              }

              private void convertBooleanToString(
                  String[] channelsToDisplayArray, boolean[] channelsSelected) {
                for (int i = 0; i < channelsSelected.length; i++) {
                  if (channelsSelected[i]) {
                    int in =
                        Character.getNumericValue(
                            (channels
                                    .get(i)
                                    .toString()
                                    .charAt(channels.get(i).toString().length() - 1))
                                - 1);
                    channelsToDisplayArray[in] = sensors.get(i);
                  }
                }
              }
            });
    displayChannelsBuilder.setNegativeButton(
        getString(R.string.nc_dialog_negative_button),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            // dismisses the dialog
          }
        });

    // CREATE DIALOG
    displayChannelsDialog = displayChannelsBuilder.create();
    displayChannelsDialog.show();

    // LIST VIEW CONFIGURATION
    ListView channelsToDisplayListView;
    channelsToDisplayListView =
        (ListView) displayChannelsDialog.findViewById(R.id.lv_channelsSelection);
    channelsToDisplayListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    channelsToDisplayListView.setItemsCanFocus(false);
    channelsToDisplayListView.setAdapter(displayChannelsListAdapter);
  }
  /** Sets up and shows the active channels picker dialog */
  private void showActiveChannelsDialog() {

    // get channel numbers from values, converts them to list and pass as an attribute to the
    // adapter
    String[] rawChannelNumbers = getResources().getStringArray(R.array.channels);
    List<String> channelNumbersAL = Arrays.asList(rawChannelNumbers);
    final ActiveChannelsListAdapter activeChannelsListAdapter =
        new ActiveChannelsListAdapter(this, channelNumbersAL, newConfiguration.getActiveSensors());

    // custom title for dialog
    TextView customTitleView = (TextView) inflater.inflate(R.layout.dialog_custom_title, null);
    customTitleView.setText(R.string.nc_dialog_title_channels_to_activate);

    // ACTIVE CHANNELS BUILDER
    AlertDialog.Builder activeChannelsBuilder;
    activeChannelsBuilder =
        new AlertDialog.Builder(this)
            .setCustomTitle(customTitleView)
            .setView(getLayoutInflater().inflate(R.layout.dialog_channels_listview, null))
            .setPositiveButton(
                getString(R.string.nc_dialog_positive_button),
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    // reset display channels because active channels have changed. Leaves error
                    // message if there was one
                    if (displayChannelsTV.getError() == null) {
                      displayChannelsTV.setText("");
                      displayChannelsTV.setVisibility(View.GONE);
                      channelsSelected = null;
                    }
                    // get active channels from adapter and sets it to the new configuration
                    activeChannels = activeChannelsListAdapter.getChecked();
                    newConfiguration.setActiveChannels(activeChannels);
                    newConfiguration.setDisplayChannels(activeChannels);

                    if (noChannelsActivated(activeChannels)) {
                      if (activeChannelsTV.getError() == null) {
                        activeChannelsTV.setText("");
                        activeChannelsTV.setVisibility(View.GONE);
                      }

                    } else {
                      activeChannelsTV.setVisibility(View.VISIBLE);
                      activeChannelsTV.setError(null);
                      activeChannelsTV.setTextColor(getResources().getColor(R.color.blue));
                      activeChannelsTV.setText(getString(R.string.nc_channels_to_activate));
                      activeChannelsTV.append(
                          "  " + newConfiguration.getActiveChannels().toString());
                    }
                  }
                })
            .setNegativeButton(
                getString(R.string.nc_dialog_negative_button),
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    // dismisses the dialog
                  }
                });

    // creates and shows dialog before setting the list view so that the views are inflated
    AlertDialog activeChannelsDialog = activeChannelsBuilder.create();
    activeChannelsDialog.show();

    ListView activeChannelsListView =
        (ListView) activeChannelsDialog.findViewById(R.id.lv_channelsSelection);
    activeChannelsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    activeChannelsListView.setItemsCanFocus(false);
    activeChannelsListView.setAdapter(activeChannelsListAdapter);
  }