package cnp.ew.button;

import java.awt.*;
import cnp.ew.image.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;
import cnp.ew.util.*;

public class CpButtonImageLc extends CpAbstractLc
{
    public static final int STATE_NOT_PRESSED_ON=0;
    public static final int STATE_NOT_PRESSED_OFF=1;
    public static final int STATE_PRESSED_ON=2;
    public static final int STATE_PRESSED_OFF=3;
    public static final int STATE_DISABLED_ON=4;
    public static final int STATE_DISABLED_OFF=5;
    public static final int STATE_PRESSED=6;
    public static final int STATE_NOT_PRESSED=7;

    public Image notPressedOnImage, notPressedOffImage, pressedOnImage, pressedOffImage, disabledOffImage, disabledOnImage;
    public int currentState=STATE_NOT_PRESSED_OFF;
    public boolean shouldShiftImage=true;
    boolean showFocus=false;

    int getShiftAmountForCurrentState()
    {
        if (!shouldShiftImage) {
            return 0;
        }
        switch(currentState) {
        case STATE_NOT_PRESSED_ON:
            return 1;
        case STATE_NOT_PRESSED_OFF:
            return 0;
        case STATE_PRESSED_ON:
            return 2;
        case STATE_PRESSED_OFF:
            return 2;
        case STATE_DISABLED_ON:
            return 1;
        case STATE_DISABLED_OFF:
            return 0;
        case STATE_PRESSED:
            return 1;
        case STATE_NOT_PRESSED:
            return 0;
        default:
            throw new IllegalArgumentException("Illegal Button Image Displayer State");
        }
    }

    Image getImageForCurrentState()
    {
        return getImageForState(currentState);
    }

    public void setShowFocus(boolean showFocus)
    {
        this.showFocus = showFocus;
        repaint();
    }


    Image getImageForState(int state)
    {
        switch (state) {
        case STATE_NOT_PRESSED_ON:
            if (notPressedOnImage != null) {
                return notPressedOnImage;
            } else {
                return notPressedOffImage;
            }
        case STATE_NOT_PRESSED:
        case STATE_NOT_PRESSED_OFF:
            return notPressedOffImage;
        case STATE_PRESSED_ON:
            if (pressedOnImage != null) {
                return pressedOnImage;
            } else if (notPressedOnImage != null) {
                return notPressedOnImage;
            } else {
                return notPressedOffImage;
            }
        case STATE_PRESSED:
        case STATE_PRESSED_OFF:
            if (pressedOffImage != null) {
                return pressedOffImage;
            } else {
                return notPressedOffImage;
            }
        case STATE_DISABLED_ON:
            if (disabledOnImage == null) {
                disabledOnImage = CpImageGetter.getDefault().getGrayedImage(
                    CpToolkit.defaultComponent(), getImageForState(STATE_NOT_PRESSED_ON)
                );
            }

            return disabledOnImage;
        case STATE_DISABLED_OFF:
            if (disabledOffImage == null) {
                disabledOffImage = CpImageGetter.getDefault().getGrayedImage(
                    CpToolkit.defaultComponent(), getImageForState(STATE_NOT_PRESSED_OFF)
                );
            }

            return disabledOffImage;
        default:
            throw new IllegalArgumentException("Illegal Button Image Displayer State");
        }
    }


    int getImageWidth()
    {
        return notPressedOffImage.getWidth(CpToolkit.defaultComponent());
    }

    int getImageHeight()
    {
        return notPressedOffImage.getHeight(CpToolkit.defaultComponent());
    }

    public void paint(Graphics g, Rectangle rect)
    {
        Rectangle clientRect = getClientRect();

        int shiftAmount = getShiftAmountForCurrentState();
        g.drawImage(
            getImageForCurrentState(),
            clientRect.x + (clientRect.width - getImageWidth()) / 2 + shiftAmount,
            clientRect.y + (clientRect.height - getImageHeight()) / 2 + shiftAmount,
            CpToolkit.defaultComponent());

        if (showFocus) {
            CpFocusRect.drawFocusRect(g, clientRect);
        }
    }

    public Dimension preferredSize()
    {
        if (notPressedOffImage == null) {
            return new Dimension(0,0);
        }
        return borderedPreferredSize(new Dimension(getImageWidth(), getImageHeight()));
    }
}

