/*
 * Copyright (c) 2006-2008 MiniMe.
 * Code released under The MIT / X Window System License.   Full licence text can be found in license.txt
 */
package minime.ui;

import javax.microedition.lcdui.Graphics;

import minime.Image;
import minime.core.Component;
import minime.core.Event;
import minime.core.ResourceManager;

/**
 * The Scroller class is used to hold vertical scrolling bar.
 * 
 * @author yishu
 * 
 */
public class Scroller extends Component
{
    private Image scrollbarImg, sliderImg;
    private int row = -1;
    private int selectedRowIndex = 0;

    /**
     * create a scroller with the images
     * 
     * @param scrollbarImg
     *            the slot image for scroller
     * @param sliderImg
     *            the image for scroller
     * @param row
     *            amout of menu rows
     */
    public Scroller(Image scrollbarImg, Image sliderImg, int row)
    {
        this.scrollbarImg = scrollbarImg;
        this.sliderImg = sliderImg;
        this.row = row;
    }

    /**
     * create a scroller with the images, which are gotten from resource
     * file(*.bin) based on resource id.if want to set image to null, imRscId
     * should be ResourceManager.NONE_RESOURCE.
     * 
     * @param scrollbarImRscId
     *            the slot image resource id for scroller,if want to set image
     *            to null, imRscId should be ResourceManager.NONE_RESOURCE.
     * @param sliderImRscId
     *            the image resource id for scroller,if want to set image to
     *            null, imRscId should be ResourceManager.NONE_RESOURCE.
     * @param row
     *            amout of menu rows
     */
    public Scroller(int scrollbarImRscId, int sliderImRscId, int row)
    {
        this(new Image(ResourceManager.getImage(scrollbarImRscId)), new Image(ResourceManager.getImage(sliderImRscId)),
                row);
    }

    public void layoutImp()
    {
        if (sliderImg != null)
        {
            setWidth(sliderImg.getWidth());
        }
        if (scrollbarImg != null)
        {
            setHeight(scrollbarImg.getHeight());
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see minime.Drawable#renderImpl(javax.microedition.lcdui.Graphics)
     */
    public void renderImpl(Graphics gc)
    {
        if (row > 0)
        { // draw scroller if items exist
            if (scrollbarImg != null && sliderImg != null)
            {
                scrollbarImg.draw(gc, (getWidth() - scrollbarImg.getWidth()) / 2, 0);
                int slitter = getHeight() / row;
                int sliderY;
                sliderY = selectedRowIndex * slitter;
                if (sliderImg.getHeight() > slitter && selectedRowIndex == row - 1)
                    sliderY = sliderY - (sliderImg.getHeight() - slitter) + 3;

                sliderImg.draw(gc, 0, sliderY);
            }
        }
    }

    public boolean onEvent(Event evt)
    {
        if (evt.type != Event.MENU_EVENT)
        {
            return false;
        }
        // get the selected row
        setSelectedRowIndex(((Integer) evt.payload).intValue());
        minime.core.Runtime.getInstance().repaint();
        return true;
    }

    /**
     * set the selected row index
     * 
     * @param index
     *            the selected row index
     */
    public void setSelectedRowIndex(int index)
    {
        selectedRowIndex = index;
    }

    /**
     * set the row count
     * 
     * @param row
     *            amount of rows
     */
    public void setRowCnt(int row)
    {
        this.row = row;
    }

    public int getPriority()
    {
        return LOW_PRIORITY;
    }
}
