package ch.spherIC.recurvebowsight.components;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

import java.text.DecimalFormat;

import ch.spherIC.recurvebowsight.utilities.UnitConverter;

@SuppressLint("AppCompatCustomView")
public class NumberAutoCompleteTextView extends AutoCompleteTextView {


    private DecimalFormat formatter;

    public NumberAutoCompleteTextView(Context context) {
        this(context, null);
    }

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

    public NumberAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public NumberAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public NumberAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, Resources.Theme popupTheme) {
        super(context, attrs, defStyleAttr, defStyleRes, popupTheme);
        int paddingH = UnitConverter.dpToPxH(16);
        int paddingV = UnitConverter.dpToPxV(16);
        setPadding(paddingH, paddingV, paddingH, paddingV);
    }

    public DecimalFormat getFormatter() {
        return formatter;
    }

    public void setFormatter(DecimalFormat formatter) {
        this.formatter = formatter;
    }

    @Override
    protected CharSequence convertSelectionToString(Object selectedItem) {
        if (this.formatter != null && selectedItem instanceof Double) {
            return this.formatter.format(selectedItem);
        }
        return super.convertSelectionToString(selectedItem);
    }
}
