package cnp.ew.charts;

import cnp.ew.diagram.*;
import cnp.ew.util.*;
import java.awt.*;

// TEMPORARY CLASS - SHOULD BE REPLACED BY A PARENT GO THAT
// COVERS THE ENTIRE PROCESS

public class CpChartController implements CpObserver
{

    CpChartBackdropLc backdropLc = new CpChartBackdropLc(66,125,277,194);
    CpVariableAxisLc verticalAxisLc = new CpVariableAxisLc(12, 130, 54, 194);
    // This should be a controlling parent go interface that
    // can have different types of bars...
    Cp3DBarLc valueLc = new Cp3DBarLc(76, 125, 20, 194);
    CpAxisScale axisScale = new CpAxisScale();

    static int valueOffset = 10;

    void setBackdropLc(CpChartBackdropLc newLc)
    {
        backdropLc = newLc;
        backdropLc.setAxisScale(axisScale);
        backdropLc.addObserver(this);
    }

    void setVerticalAxisLc(CpVariableAxisLc newLc)
    {
        verticalAxisLc = newLc;
        verticalAxisLc.setAxisScale(axisScale);
        verticalAxisLc.addObserver(this);
    }

    void setGraphValuesLc(Cp3DBarLc newValueLc)
    {
        valueLc = newValueLc;
        valueLc.setAxisScale(axisScale);
        valueLc.addObserver(this);
    }

    // This is a temporary way to have multiple independent gos
    // move and size together.  When children are supported, this
    // won't be necessary.
    public void update(CpObservable o, int facet, Object arg)
    {
        Rectangle backdropBounds, axisBounds, valueBounds;

        // A Move or size occurred; frame the other gos
        // THIS IS HIDEOUS; CHANGE TO PARENT CHILD RELATIONSHIPS
        if (o == backdropLc) {
            switch (facet) {
            case CpAbstractGraphicObject.BOUNDS:
            case CpAbstractGraphicObject.LOCATION:
            case CpAbstractGraphicObject.RELATIVE_LOCATION:

                backdropBounds = backdropLc.getBoundingBox();
                Dimension preferredSize = verticalAxisLc.preferredSize();

                axisBounds = new Rectangle(backdropBounds.x - preferredSize.width, backdropBounds.y + verticalAxisLc.getTickAlignmentOffset(), preferredSize.width, backdropBounds.height);
                verticalAxisLc.setParameter(CpAbstractGraphicObject.BOUNDS, axisBounds, false, false);

                valueBounds = new Rectangle(backdropBounds.x + valueOffset, backdropBounds.y, valueLc.getBoundingBox().width, backdropBounds.height);
                valueLc.setParameter(CpAbstractGraphicObject.BOUNDS, valueBounds, false, false);
                break;
            }

        } else if (o == verticalAxisLc) {
            switch (facet) {
            case CpAbstractGraphicObject.BOUNDS:
            case CpAbstractGraphicObject.LOCATION:
            case CpAbstractGraphicObject.RELATIVE_LOCATION:
                axisBounds = verticalAxisLc.getBoundingBox();
                Dimension preferredSize = verticalAxisLc.preferredSize();

                backdropBounds = new Rectangle(axisBounds.x + preferredSize.width, axisBounds.y - verticalAxisLc.getTickAlignmentOffset(), backdropLc.getBoundingBox().width, axisBounds.height);
                backdropLc.setParameter(CpAbstractGraphicObject.BOUNDS, backdropBounds, false, false);

                valueBounds = new Rectangle(backdropBounds.x + valueOffset, backdropBounds.y, valueLc.getBoundingBox().width, backdropBounds.height);
                valueLc.setParameter(CpAbstractGraphicObject.BOUNDS, valueBounds, false, false);
                break;
            }
        } else if (o == valueLc) {

            switch (facet) {
            case CpAbstractGraphicObject.BOUNDS:
            case CpAbstractGraphicObject.LOCATION:
            case CpAbstractGraphicObject.RELATIVE_LOCATION:
                valueBounds = valueLc.getBoundingBox();
                Dimension preferredSize = verticalAxisLc.preferredSize();

                backdropBounds = new Rectangle(valueBounds.x - valueOffset, valueBounds.y, backdropLc.getBoundingBox().width, valueBounds.height);
                backdropLc.setParameter(CpAbstractGraphicObject.BOUNDS, backdropBounds, false, false);

                axisBounds = new Rectangle(backdropBounds.x - preferredSize.width, backdropBounds.y + verticalAxisLc.getTickAlignmentOffset(), preferredSize.width, backdropBounds.height);
                verticalAxisLc.setParameter(CpAbstractGraphicObject.BOUNDS, axisBounds, false, false);

                break;
            }
        }
    }
}
