/**
 * Copyright (C) 2005-2012 XELOG AG
 */
package ch.spherIC.recurvebowsight.components;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import ch.spherIC.recurvebowsight.R;

import java.util.List;


/**
 * @author  FC Smilari
 */
public class FontArrayAdapter<O extends Object> extends ArrayAdapter<O> {

    private XSpinner parentSpinner;

    /**
     * @param  context
     * @param  textViewResourceId
     * @param  objects
     */
    public FontArrayAdapter(final Context context, final int textViewResourceId, final O[] objects,
                            final XSpinner parent) {
        super(context, textViewResourceId, objects);
        this.parentSpinner = parent;
    }

    /**
     * @param  context
     * @param  textViewResourceId
     * @param  objects
     */
    public FontArrayAdapter(final Context context, final int textViewResourceId, final List<O> objects,
                            final XSpinner parent) {
        super(context, textViewResourceId, objects);
        this.parentSpinner = parent;
    }

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View spinnerTxtView = inflater.inflate(R.layout.rbs_spinner, parent, false);
        TextView textView = (TextView) spinnerTxtView.findViewById(R.id.spinner_view);
        textView.setText(getItem(position).toString());

        return spinnerTxtView;
    }


    @Override
    public View getDropDownView(final int position, final View convertView, final ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View spinnerItem = inflater.inflate(R.layout.rbs_spinner_item, parent, false);
        FontTextView textView = (FontTextView) spinnerItem.findViewById(R.id.spinner_item_view);
        final CheckBox checkBoxView = (CheckBox) spinnerItem.findViewById(R.id.spinner_item_RadioBtn);

        textView.setText(getItem(position).toString());
//        spinnerItem.setBackgroundResource(R.drawable.list_item_background);

        if (this.parentSpinner.getSelectedItemPosition() == position) {
            textView.setFont(getContext(), "fonts/TitilliumWeb-Bold.ttf");
            textView.setSelected(true);
            checkBoxView.setChecked(true);
        } else {
            checkBoxView.setChecked(false);
        }

        return spinnerItem;
    }

    protected LayoutInflater getLayoutInflater() {
        return (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}
