/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pyamsoft.powermanager.uicore;

import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.preference.DialogPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceViewHolder;
import android.support.v7.preference.TwoStatePreference;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import timber.log.Timber;

abstract class FormatterPreferenceFragment extends PreferenceFragmentCompat {

  final void applyFormattedStrings(@NonNull Preference preference, @NonNull String name) {
    final String replaceString = "REPLACE_ME";
    String title = preference.getTitle().toString();
    title = title.replace(replaceString, name);
    preference.setTitle(title);

    if (preference instanceof DialogPreference) {
      final DialogPreference dialogPreference = (DialogPreference) preference;
      dialogPreference.setDialogTitle(title);
    }

    if (preference instanceof TwoStatePreference) {
      final TwoStatePreference twoStatePreference = (TwoStatePreference) preference;
      String summaryOn = twoStatePreference.getSummaryOn().toString();
      summaryOn = summaryOn.replace(replaceString, name);
      twoStatePreference.setSummaryOn(summaryOn);

      String summaryOff = twoStatePreference.getSummaryOff().toString();
      summaryOff = summaryOff.replace(replaceString, name);
      twoStatePreference.setSummaryOff(summaryOff);
    } else {
      String summary = preference.getSummary().toString();
      summary = summary.replace(replaceString, name);
      preference.setSummary(summary);
    }
  }

  @CheckResult @Nullable
  final PreferenceViewHolder findViewForPreference(@Nullable String preferenceKey) {
    if (preferenceKey == null) {
      Timber.w("NULL Preference Key");
      return null;
    }

    final PreferenceGroup.PreferencePositionCallback callback =
        (PreferenceGroup.PreferencePositionCallback) getListView().getAdapter();
    final int position = callback.getPreferenceAdapterPosition(preferenceKey);
    if (position == RecyclerView.NO_POSITION) {
      Timber.w("No position for key: %s", preferenceKey);
      return null;
    }

    return (PreferenceViewHolder) getListView().findViewHolderForAdapterPosition(position);
  }

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    resolvePreferences();
    applyFormattedStrings(getModuleName());
  }

  @Override public void onStop() {
    super.onStop();
    if (isRemoving()) {
      dismissOnboarding();
    }
  }

  abstract void dismissOnboarding();

  abstract void resolvePreferences();

  abstract void applyFormattedStrings(@NonNull String name);

  @CheckResult @NonNull protected abstract String getModuleName();
}

@@ -1009,238 +1009,52 @@
 nce.
-PreferenceGroup;%0Aimport android.support.v7.preference.PreferenceViewHolder;%0Aimport android.support.v7.preference.TwoStatePreference;%0Aimport android.support.v7.widget.RecyclerView;%0Aimport android.view.View;%0Aimport timber.log.Timber
+TwoStatePreference;%0Aimport android.view.View
 ;%0A%0Aa
@@ -2266,669 +2266,8 @@
  %7D%0A%0A
-  @CheckResult @Nullable%0A  final PreferenceViewHolder findViewForPreference(@Nullable String preferenceKey) %7B%0A    if (preferenceKey == null) %7B%0A      Timber.w(%22NULL Preference Key%22);%0A      return null;%0A    %7D%0A%0A    final PreferenceGroup.PreferencePositionCallback callback =%0A        (PreferenceGroup.PreferencePositionCallback) getListView().getAdapter();%0A    final int position = callback.getPreferenceAdapterPosition(preferenceKey);%0A    if (position == RecyclerView.NO_POSITION) %7B%0A      Timber.w(%22No position for key: %25s%22, preferenceKey);%0A      return null;%0A    %7D%0A%0A    return (PreferenceViewHolder) getListView().findViewHolderForAdapterPosition(position);%0A  %7D%0A%0A
   @O

Remove unused