package cnp.ew.displayer;

import cnp.ew.util.*;
import java.awt.*;
import cnp.ew.lightweight.*;

public class CpBarValueDisplayer extends CpAbstractOrientedValueDisplayer
{
    boolean showPercentage=true;
    Color barColor = new Color(0, 0, 128);


    public void setShowPercentage(boolean newShowPercentage)
    {
        showPercentage = newShowPercentage;
    }

    public boolean getShowPercentage()
    {
        return showPercentage;
    }

    /**
     * Display the object using g in the rectangle defined
     * by x,y with width w and height h.
     */
    public void paintIn(CpLightweightComponent c, Graphics g, int x, int y, int w, int h)
    {
        float percentage = getValuePercentage();
        Rectangle r1, r2;
        Color c1, c2;
        Color backColor = c.getBackground();

        switch (getOrientation()) {
        case ORIENT_LEFT_TO_RIGHT:
        case ORIENT_RIGHT_TO_LEFT:
            int firstWidth = (int)(w * percentage);
            if (getOrientation() == ORIENT_RIGHT_TO_LEFT) {
                firstWidth = w - firstWidth;
                c1 = backColor;
                c2 = barColor;
            } else {
                c1 = barColor;
                c2 = backColor;
            }
            r1 = new Rectangle(x, y, firstWidth, h);
            r2 = new Rectangle(x + firstWidth, y, w - firstWidth, h);
            break;
        case ORIENT_TOP_TO_BOTTOM:
        case ORIENT_BOTTOM_TO_TOP:
            int firstHeight = (int)(h * percentage);
            if (getOrientation() == ORIENT_BOTTOM_TO_TOP) {
                firstHeight = h - firstHeight;
                c1 = backColor;
                c2 = barColor;
            } else {
                c1 = barColor;
                c2 = backColor;
            }
            r1 = new Rectangle(x, y, w, firstHeight);
            r2 = new Rectangle(x , y + firstHeight, w, h - firstHeight);
            break;
        default:
            throw new IllegalArgumentException("Invalid orientation");
        }

        g.setColor(c1);
        g.fillRect(r1.x, r1.y, r1.width, r1.height);
        g.setColor(c2);
        g.fillRect(r2.x, r2.y, r2.width, r2.height);

        if (showPercentage) {
            FontMetrics fontMetrics = g.getFontMetrics();
            String text = String.valueOf(percentage * 100) + "%";
            int textX = x + ((w - fontMetrics.stringWidth(text)) / 2);
            int textY = y + ((h - fontMetrics.getHeight()) / 2) + fontMetrics.getAscent();

            g.drawString(text, textX, textY);

            // We create a copy of the original graphics, since we don't want to step on the original cliprect
            Graphics tempGraphics = g.create();
            try {
                tempGraphics.clipRect(r2.x, r2.y, r2.width, r2.height);
                tempGraphics.setColor(c1);
                tempGraphics.drawString(text, textX, textY);
            } finally {
                tempGraphics.dispose();
            }
        }
    }

    public Dimension preferredSize(CpLightweightComponent c)
    {
        int stringWidth = Toolkit.getDefaultToolkit().getFontMetrics(c.getFont()).stringWidth("100% ");

        switch (getOrientation()) {
        case ORIENT_LEFT_TO_RIGHT:
        case ORIENT_RIGHT_TO_LEFT:
            return new Dimension(100, stringWidth);
        case ORIENT_TOP_TO_BOTTOM:
        case ORIENT_BOTTOM_TO_TOP:
            return new Dimension(stringWidth, 100);
        default:
            throw new IllegalArgumentException("Invalid orientation");
        }
    }
}

