package cgeo.geocaching.settings;

import cgeo.geocaching.R;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.utils.LogTemplateProvider;
import cgeo.geocaching.utils.LogTemplateProvider.LogTemplate;

import org.apache.commons.lang3.StringUtils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class TemplateTextPreference extends DialogPreference {

    private SettingsActivity settingsActivity;
    private EditText editText;

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

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

    private void init() {
        setDialogLayoutResource(R.layout.template_preference_dialog);
    }

    @Override
    protected void onBindDialogView(View view) {
        settingsActivity = (SettingsActivity) this.getContext();

        editText = (EditText) view.findViewById(R.id.signature_dialog_text);
        editText.setText(getPersistedString(StringUtils.EMPTY));

        Button button = (Button) view.findViewById(R.id.signature_templates);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View button) {
                AlertDialog.Builder alert = new AlertDialog.Builder(TemplateTextPreference.this.getContext());
                alert.setTitle(R.string.init_signature_template_button);
                final ArrayList<LogTemplate> templates = LogTemplateProvider.getTemplates();
                String[] items = new String[templates.size()];
                for (int i = 0; i < templates.size(); i++) {
                    items[i] = settingsActivity.getResources().getString(templates.get(i).getResourceId());
                }
                alert.setItems(items, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int position) {
                        dialog.dismiss();
                        final LogTemplate template = templates.get(position);
                        insertSignatureTemplate(template);
                    }
                });
                alert.create().show();
            }
        });

        super.onBindDialogView(view);
    }

    private void insertSignatureTemplate(final LogTemplate template) {
        String insertText = "[" + template.getTemplateString() + "]";
        ActivityMixin.insertAtPosition(editText, insertText, true);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            String text = editText.getText().toString();
            persistString(text);
            callChangeListener(text);
        }
        super.onDialogClosed(positiveResult);
    }
}

@@ -360,24 +360,63 @@
 gInterface;%0A
+import android.content.res.TypedArray;%0A
 import andro
@@ -659,24 +659,169 @@
 eference %7B%0A%0A
+    /**%0A     * default value, if none is given in the preference XML.%0A     */%0A    private static final String DEFAULT_VALUE = StringUtils.EMPTY;%0A
     private 
@@ -885,16 +885,49 @@
 ditText;
+%0A    private String initialValue;
 %0A%0A    pu
@@ -1551,16 +1551,65 @@
 dString(
+initialValue != null ? initialValue.toString() : 
 StringUt
@@ -3389,11 +3389,571 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) %7B%0A        if (restorePersistedValue) %7B%0A            // Restore existing state%0A            initialValue = this.getPersistedString(DEFAULT_VALUE);%0A        %7D else %7B%0A            // Set default state from the XML attribute%0A            initialValue = defaultValue.toString();%0A            persistString(initialValue);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected Object onGetDefaultValue(TypedArray array, int index) %7B%0A        return array.getString(index);%0A    %7D
 %0A%7D%0A

fix #3337: use default values in template text preferences