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

import android.content.Context;

import android.util.AttributeSet;

import ch.spherIC.recurvebowsight.model.ISight;


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

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

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

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


    /**
     * Retrieves selected sight item.
     *
     * @return  Retrieves selected {@link ISight} item
     */
    public ISight getSelectedSight() {
        return (ISight) getSelectedItem();
    }

    /**
     * Set the passed in {@link ISight} as the selected sight item.
     */
    public void setSelectedSight(final ISight sight) {

        for (int i = 0; i < getCount(); i++) {
            if (((ISight) getItemAtPosition(i)).getId() == sight.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);
        }
    }

}
