package cgeo.geocaching.settings;

import static cgeo.geocaching.utils.LocalizationUtils.getString;

import cgeo.geocaching.R;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.log.LogTemplateProvider;
import cgeo.geocaching.log.LogTemplateProvider.LogTemplate;
import cgeo.geocaching.ui.dialog.Dialogs;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;

public class TemplateTextPreference extends Preference {

    /**
     * default value, if none is given in the preference XML.
     */
    private static final String DEFAULT_VALUE = StringUtils.EMPTY;
    private SettingsActivity settingsActivity;
    private EditText editText;
    private EditText editTitle;
    private String initialValue;

    public TemplateTextPreference(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public TemplateTextPreference(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onBindViewHolder(final PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        final Preference pref = findPreferenceInHierarchy(getKey());
        if (pref != null) {
            pref.setOnPreferenceClickListener(preference -> {
                editTemplate();
                return false;
            });
        }
    }

    private void editTemplate() {
        settingsActivity = (SettingsActivity) this.getContext();

        final boolean isSignature = this.getKey().equals(getString(R.string.pref_signature));

        final ImmutablePair<String, String> template = Settings.getLogTemplate(this.getKey());

        final View v = LayoutInflater.from(getContext()).inflate(R.layout.template_preference_dialog, null);
        editTitle = v.findViewById(R.id.title);
        if (isSignature) {
            editTitle.setText(R.string.init_signature);
            editTitle.setEnabled(false);
        } else {
            editTitle.setText(template.getKey());
        }
        editText = v.findViewById(R.id.edit);
        editText.setText(template.getValue());
        Dialogs.moveCursorToEnd(editText);

        final AlertDialog dialog = Dialogs.newBuilder(getContext())
            .setView(v)
            .setPositiveButton(android.R.string.ok, null)
            .setNeutralButton(R.string.init_signature_template_button, null)
            .show();

        // override onClick listener to prevent closing dialog on pressing the "default" button
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(v2 -> {
            final String newTitle = editTitle.getText().toString();
            final String newText = editText.getText().toString();
            // check that for log templates both title and text are filled
            if (!isSignature && StringUtils.isEmpty(newTitle) && !StringUtils.isEmpty(newText)) {
                editTitle.setError(getString(R.string.init_log_template_missing_error));
            } else if (!isSignature && !StringUtils.isEmpty(newTitle) && StringUtils.isEmpty(newText)) {
                editText.setError(getString(R.string.init_log_template_missing_error));
            } else {
                if (isSignature) {
                    persistString(newText);
                } else {
                    Settings.putLogTemplate(this.getKey(), newTitle, newText);
                }
                callChangeListener(newText);
                dialog.dismiss();
            }
        });
        dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener(v2 -> {
            final AlertDialog.Builder templateBuilder = Dialogs.newBuilder(TemplateTextPreference.this.getContext());
            templateBuilder.setTitle(R.string.init_signature_template_button);
            final List<LogTemplate> templates = LogTemplateProvider.getTemplatesWithoutSignature();
            final String[] items = new String[templates.size()];
            for (int i = 0; i < templates.size(); i++) {
                items[i] = settingsActivity.getString(templates.get(i).getResourceId());
            }
            templateBuilder.setItems(items, (dialog3, position) -> {
                dialog3.dismiss();
                final String insertText = "[" + templates.get(position).getTemplateString() + "]";
                ActivityMixin.insertAtPosition(editText, insertText, true);
            });
            templateBuilder.create().show();
        });

    }

    @Override
    protected void onSetInitialValue(final Object defaultValue) {
        if (defaultValue == null) {
            // Restore existing state
            initialValue = this.getPersistedString(DEFAULT_VALUE);
        } else {
            // Set default state from the XML attribute
            initialValue = defaultValue.toString();
            persistString(initialValue);
        }
    }

    @Override
    protected Object onGetDefaultValue(final TypedArray array, final int index) {
        return array.getString(index);
    }
}

@@ -32,74 +32,8 @@
 s;%0A%0A
-import static cgeo.geocaching.utils.LocalizationUtils.getString;%0A%0A
 impo
@@ -1905,16 +1905,33 @@
 .equals(
+settingsActivity.
 getStrin
@@ -3315,32 +3315,49 @@
 tTitle.setError(
+settingsActivity.
 getString(R.stri
@@ -3533,16 +3533,33 @@
 etError(
+settingsActivity.
 getStrin

fix Checkstyle issue