/*
 * 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.padlock.app.settings;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.preference.Preference;
import android.support.v7.preference.SwitchPreferenceCompat;
import com.pyamsoft.padlock.R;
import com.pyamsoft.padlock.Singleton;
import com.pyamsoft.padlock.dagger.main.MainPresenter;
import com.pyamsoft.padlock.dagger.settings.SettingsPreferencePresenter;
import com.pyamsoft.padlock.model.event.RefreshEvent;
import com.pyamsoft.pydroid.base.fragment.ActionBarSettingsPreferenceFragment;
import com.pyamsoft.pydroid.util.AppUtil;
import javax.inject.Inject;
import timber.log.Timber;

public final class SettingsPreferenceFragment extends ActionBarSettingsPreferenceFragment
    implements SettingsPreferencePresenter.SettingsPreferenceView {

  @Inject SettingsPreferencePresenter presenter;

  @Override public void onResume() {
    super.onResume();
    presenter.resume();
  }

  @Override public void onPause() {
    super.onPause();
    presenter.pause();
  }

  @Override public void onDestroyView() {
    super.onDestroyView();
    presenter.unbindView();
  }

  @Override public void onCreatePreferences(@Nullable Bundle bundle, @Nullable String s) {
    addPreferencesFromResource(R.xml.preferences);

    Singleton.Dagger.with(getContext()).plusSettings().inject(this);
    presenter.bindView(this);

    final Preference clearDb = findPreference(getString(R.string.clear_db_key));
    clearDb.setOnPreferenceClickListener(preference -> {
      Timber.d("Clear DB onClick");
      presenter.requestClearDatabase();
      return true;
    });

    final Preference resetAll = findPreference(getString(R.string.clear_all_key));
    resetAll.setOnPreferenceClickListener(preference -> {
      Timber.d("Reset settings onClick");
      presenter.requestClearAll();
      return true;
    });

    final Preference upgradeInfo = findPreference(getString(R.string.upgrade_info_key));
    upgradeInfo.setOnPreferenceClickListener(preference -> showChangelog());

    final SwitchPreferenceCompat showAds =
        (SwitchPreferenceCompat) findPreference(getString(R.string.adview_key));
    showAds.setOnPreferenceChangeListener((preference, newValue) -> toggleAdVisibility(newValue));
  }

  @Override public void showConfirmDialog(int type) {
    AppUtil.guaranteeSingleDialogFragment(getFragmentManager(),
        ConfirmationDialog.newInstance(type), "confirm_dialog");
  }

  @Override public void onClearAll() {
    // TODO stop Service on 24
    android.os.Process.killProcess(android.os.Process.myPid());
  }

  @Override public void onClearDatabase() {
    MainPresenter.Bus.get().post(new RefreshEvent());
  }
}

@@ -822,16 +822,109 @@
 Compat;%0A
+import android.view.LayoutInflater;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;%0A
 import c
@@ -1842,28 +1842,28 @@
 ride public 
-void
+View
  onCreatePre
@@ -1863,218 +1863,409 @@
 eate
-Preferences(@Nullable Bundle bundle, @Nullable String s) %7B%0A    addPreferencesFromResource(R.xml.preferences);%0A%0A    Singleton.Dagger.with(getContext()).plusSettings().inject(this);%0A    presenter.bindView(this);%0A
+View(LayoutInflater inflater, ViewGroup container,%0A      Bundle savedInstanceState) %7B%0A    Singleton.Dagger.with(getContext()).plusSettings().inject(this);%0A    presenter.bindView(this);%0A%0A    return super.onCreateView(inflater, container, savedInstanceState);%0A  %7D%0A%0A  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) %7B%0A    super.onViewCreated(view, savedInstanceState);
 %0A   
@@ -3136,32 +3136,179 @@
 ewValue));%0A  %7D%0A%0A
+  @Override public void onCreatePreferences(@Nullable Bundle bundle, @Nullable String s) %7B%0A    addPreferencesFromResource(R.xml.preferences);%0A  %7D%0A%0A
   @Override publ

correct presenter binding times