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

import android.content.Context;

import android.util.AttributeSet;

import ch.spherIC.recurvebowsight.model.IRiser;


/**
 * @author  FC Smilari
 */
public class RiserSpinner extends XSpinner {

    /**
     * @param  context
     */
    public RiserSpinner(final Context context) {
        super(context);
    }

    /**
     * @param  context
     * @param  attrs
     */
    public RiserSpinner(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * @param  context
     * @param  attrs
     * @param  defStyle
     */
    public RiserSpinner(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }


    /**
     * Retrieves selected riser item.
     *
     * @return  Retrieves selected {@link IRiser} item
     */
    public IRiser getSelectedRiser() {
        return (IRiser) getSelectedItem();
    }

    /**
     * Set the passed in {@link IRiser} as the selected riser item.
     */
    public void setSelectedRiser(final IRiser riser) {

        for (int i = 0; i < getCount(); i++) {
            if (((IRiser) getItemAtPosition(i)).getId() == riser.getId()) {
                setSelection(i);
                break;
            }
        }
    }

    public void fireSelectionChangedEvent() {
        OnItemSelectedListener listener = getOnItemSelectedListener();
        if (listener != null) {
            int id = getSelectedView() != null ? getSelectedView().getId() : 0;
            listener.onItemSelected(this, getSelectedView(), getSelectedItemPosition(), id);
        }
    }

}
