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

import javax.microedition.lcdui.Graphics;

import minime.Drawable;
import minime.Font;
import minime.Image;
import minime.Portability;
import minime.core.ResourceManager;

/**
 * The Gauge class represents a progress bar, The Gauge contains a current value
 * that lies between zero and the maximum value, inclusive. The application can
 * control the current value and maximum value.Either gauge has background image
 * and slider image or it has frame without background images, if it doesn't
 * have background images, the slider part of gauge will be filled with color.It
 * also either has a label or not.
 * 
 * @author yishu
 * 
 */
public class Gauge extends Drawable
{
    private final static int DEFAULT_MAX_VALUE = 100;
    private final static int DEFAULT_GAUGE_COLOR = Portability.BLUE;
    private final static int DEFAULT_LABEL_TEXT_COLOR = Portability.ANTIQUEWHITE;
    public final static int OPTION_DISPLAY_TEXT = 0;
    public final static int OPTION_DISPLAY_COUNTER = 1;
    private Label label = null;
    private int progressColor = DEFAULT_GAUGE_COLOR;
    private int maxValue = DEFAULT_MAX_VALUE;
    private int progressValue = 0;
    private int option = 0;
    
    
    public Gauge( int maxValue)
    {
        this(maxValue, 0, DEFAULT_GAUGE_COLOR,  new Label("", DEFAULT_LABEL_TEXT_COLOR));
    }
    
    protected Gauge( int maxValue, int initValue, int color, Label label)
    {
        setMaxValue(maxValue);
        setValue(initValue);
        setColor(color);
        setLabel(label);
    }

    
    public void increase()
    {
    	increase(1);    
    }
    public void increase(int delta)
    {
    	setValue(progressValue+delta);
    }
    
    // sets the position and size of the guage
    public void layoutImp()
    {
        label.layout();
    }

    /**
	 * 
	 * @see minime.Drawable#renderImpl(javax.microedition.lcdui.Graphics)
	 */
    public void renderImpl(Graphics gc)
    {    	
        gc.setColor(progressColor);      	
    	int w = progressValue*getWidth()/maxValue;
		gc.fillRect(0, 0, w, getHeight());
		int counter = progressValue*100/maxValue;
		if(option == OPTION_DISPLAY_COUNTER)
			label.setString(counter+"%");
		label.render(gc);
	}
    
	public int getMaxValue() {
		return maxValue;
	}

    public void setMaxValue(int maxValue)
    {
        if (maxValue <= 0)
            maxValue = DEFAULT_MAX_VALUE;
        this.maxValue = maxValue;
        if(this.progressValue > this.maxValue)
        	this.progressValue = this.maxValue;
    }
    
	public int getValue() {
		return progressValue;
	}
    public void setValue(int value)
    {
        if (value < 0)
            this.progressValue = 0;
        else if (value > maxValue)
        	this.progressValue = maxValue;
        else
        	this.progressValue = value;
    }
	protected Label getLabel() {
		return label;
	}

	protected void setLabel(Label label) {
		this.label = label;
	}

	public int getColor() {
		return progressColor;
	}

	public void setColor(int color) {
		this.progressColor = color;
	}

	public int getBgColor() {
		return label.getBgColor();
	}
	
	public void setBgColor(int bgColor) {
		label.setBgColor(bgColor);
	}

	public Font getFont() {
		return label.getFont();
	}
	
	public void setFont(Font font) {
		label.setFont(font);
	}

	public int getHeight() {
		return label.getHeight();
	}

	public void setHeight(int h) {
		label.setHeight(h);
		setNeedLayout() ;
	}
	
	public String getString() {
		return label.getString();
	}
	
	public void setString(String text) {
		setOption(OPTION_DISPLAY_TEXT);
		label.setString(text);
		setNeedLayout() ;
	}
	public void setString(int txRscId) {
		setOption(OPTION_DISPLAY_TEXT);
		label.setString(txRscId);
		setNeedLayout() ;
	}

	public int getWidth() {
		return label.getWidth();
	}

	public void setWidth(int w) {
		label.setWidth(w);
		setNeedLayout() ;
	}

	public int getTextColor() {
		return label.getTextColor();
	}
	
	public void setTextColor(int textColor) {
		label.setTextColor(textColor);
	}

	public int getOption() {
		return option;
	}

	public void setOption(int option) {
		this.option = option;
	}

	public void setFrame(int frameThickness, int frameColor) {
		label.setFrameThicknessAndColor(frameThickness, frameColor);
		label.setFrame(true) ;
		setNeedLayout() ;
	}

	public int getLeft() {
		return label.getLeft();
	}

	public int getTop() {
		return label.getTop();
	}

	public void setLeft(int left) {
		label.setLeft(left);
	}

	public void setPosition(int left, int top) {
		label.setPosition(left, top);
	}

	public void setTop(int top) {
		label.setTop(top);
	}

}
