package cnp.ew.layout;

import java.awt.*;
import java.util.*;

import cnp.ew.lightweight.*;

public class CpLabeledLayout implements CpLayoutManager {

    public static final int LABEL_LEFT = 0;
    public static final int LABEL_RIGHT = 1;
    public static final int LABEL_TOP = 2;
    public static final int LABEL_BOTTOM = 3;

    int orientation = LABEL_LEFT;
    int margin = 5;
    CpLightweightComponent label, labelee;

    public void setLabel(CpLightweightComponent newLabel)
    {
        label = newLabel;
    }

    public void setLabelee(CpLightweightComponent newLabelee)
    {
        labelee = newLabelee;
    }

    public void setMargin(int newMargin)
    {
        margin = newMargin;
    }

    public void setOrientation(int newOrientation)
    {
        orientation = newOrientation;
    }

    public int getOrientation()
    {
        return orientation;
    }

    public void addLayoutLc(String name, CpLightweightComponent comp)
    {

    }

    public void removeLayoutLc(CpLightweightComponent comp)
    {
        if (comp == label) {
            label = null;
        }

        if (comp == labelee) {
            labelee = null;
        }
    }

    public Dimension preferredLayoutSize(CpLightweightComponent parent)
    {
        Dimension preferredSize = new Dimension(0, 0);
        Dimension labelSize=null, labeleeSize=null;

        if (label != null) {
            labelSize = label.preferredSize();
        }

        if (labelee != null) {
            labeleeSize = labelee.preferredSize();
        }


        switch (orientation) {
        case LABEL_LEFT:
        case LABEL_RIGHT:
            if (label != null && labelee != null) {
                preferredSize.width = labelSize.width + labeleeSize.width + margin;
                preferredSize.height = Math.max(labelSize.height, labeleeSize.height);
            } else if (label != null) {
                preferredSize = labelSize;
            } else {
                preferredSize = labeleeSize;
            }
            break;
        case LABEL_TOP:
        case LABEL_BOTTOM:
            if (label != null && labelee != null) {
                preferredSize.height = labelSize.height + labeleeSize.height + margin;
                preferredSize.width = Math.max(labelSize.width, labeleeSize.width);
            } else if (label != null) {
                preferredSize = labelSize;
            } else {
                preferredSize = labeleeSize;
            }
            break;
        }
        Insets insets = parent.insets();
        preferredSize.width += insets.left + insets.right;
        preferredSize.height += insets.top + insets.bottom;
        return preferredSize;
    }

    public Dimension minimumLayoutSize(CpLightweightComponent parent)
    {
        return preferredLayoutSize(parent);
    }

    public void layoutLc(CpLightweightComponent parent)
    {
        int width, height;

        Rectangle parentBounds = parent.bounds();
        Insets insets = parent.insets();
        if (label == null) {
            labelee.reshape(insets.left, insets.top, parentBounds.width - insets.left - insets.right, parentBounds.height - insets.top - insets.bottom);
            return;
        }

        if (labelee == null) {
            label.reshape(insets.left, insets.top, parentBounds.width - insets.left - insets.right, parentBounds.height - insets.top - insets.bottom);
            return;
        }

        switch (orientation) {
        case LABEL_LEFT:
            width = label.preferredSize().width;
            label.reshape(insets.left, insets.top, width, parentBounds.height - insets.top - insets.bottom);
            labelee.reshape(insets.left + width + margin, insets.top, parentBounds.width - width - margin - insets.left - insets.right, parentBounds.height - insets.top - insets.bottom);
            break;
        case LABEL_RIGHT:
            width = labelee.preferredSize().width;
            labelee.reshape(insets.left, insets.top, width, parentBounds.height - insets.top - insets.bottom);
            label.reshape(insets.left + width + margin, insets.top, parentBounds.width - width - margin - insets.left - insets.right, parentBounds.height - insets.top - insets.bottom);
            break;
        case LABEL_TOP:
            height = label.preferredSize().height;
            label.reshape(insets.left, insets.top, parentBounds.width - insets.left - insets.right, height);
            labelee.reshape(insets.left, insets.top + height + margin, parentBounds.width - insets.left - insets.right, parentBounds.height - height - margin - insets.top - insets.bottom);
            break;
        case LABEL_BOTTOM:
            height = labelee.preferredSize().height;
            labelee.reshape(insets.left, insets.top, parentBounds.width - insets.left - insets.right, height);
            label.reshape(insets.left, insets.top + height + margin, parentBounds.width - insets.left - insets.right, parentBounds.height - height - margin - insets.top - insets.bottom);
            break;
        }
    }
}
