package mlxy.tumplar.view.fragment;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;

import de.psdev.licensesdialog.LicensesDialog;
import mlxy.tumplar.BuildConfig;
import mlxy.tumplar.R;
import mlxy.tumplar.view.dialog.ChangelogDialogFragment;

public class HelpFragment extends PreferenceFragment {
    private Preference prefChangelog;
    private Preference prefLicense;

    private ChangelogDialogFragment changelogDialog;
    private LicensesDialog licensesDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs_help);

        prefChangelog = findPreference(getString(R.string.pref_key_changelog));
        prefLicense = findPreference(getString(R.string.pref_key_license));

        Preference prefVersion = findPreference(getString(R.string.pref_key_version));
        prefVersion.setSummary(BuildConfig.VERSION_NAME);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (preference == prefChangelog) {
            showChangeLog();
            return true;
        }
        if (preference == prefLicense) {
            showLicense();
            return true;
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }

    private void showChangeLog() {
        if (changelogDialog == null) {
            changelogDialog = new ChangelogDialogFragment();
        }

        if (!changelogDialog.isAdded()) {
            changelogDialog.show(getFragmentManager(), ChangelogDialogFragment.TAG);
        }
    }

    private void showLicense() {
        if (licensesDialog == null) {
            licensesDialog = new LicensesDialog.Builder(getActivity())
                    .setNotices(R.raw.licenses)
                    .setTitle(R.string.license)
                    .setThemeResourceId(R.style.AppTheme)
                    .build();
        }
        licensesDialog.show();
    }
}

@@ -344,16 +344,37 @@
 ragment;
+%0Aimport mlxy.utils.T;
 %0A%0Apublic
@@ -495,16 +495,52 @@
 License;
+%0A    private Preference prefVersion;
 %0A%0A    pr
@@ -952,16 +952,92 @@
 cense));
+%0A        prefVersion = findPreference(getString(R.string.pref_key_version));
 %0A%0A      
@@ -1499,32 +1499,113 @@
 true;%0A        %7D%0A
+        if (preference == prefVersion) %7B%0A            versionClicked();%0A        %7D%0A
         return s
@@ -2287,32 +2287,32 @@
 ld();%0A        %7D%0A
-
         licenses
@@ -2331,11 +2331,231 @@
 );%0A    %7D
+%0A%0A    private int versionClicked;%0A    private void versionClicked() %7B%0A        if (versionClicked == 8) %7B%0A            T.showShort(getActivity(), %22Easter egg coming soon (%EF%BD%80%EF%BD%A5%CF%89%EF%BD%A5%C2%B4)%22);%0A        %7D%0A        versionClicked++;%0A    %7D
 %0A%7D%0A

Add easter egg