package cnp.ew.charts;

import java.awt.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;

public class Cp3DStackedBarValuesDisplayer extends CpAbstractChartValuesDisplayer
{
    Color lineColor = Color.black;
    Point slope = new Point(10, 10);


    /**
     * This displayer will be adding the values for each data series
     * together for each datapoint; for that reason, we need to
     * override the generic behavior, and get the maximum value
     * of all summed data points.
     */
    public double getMinimumValueRequiredForDisplay()
    {
        double currentSummedDataPoint;
        double maxSummedDataPoint = 0.0;

        for (int i=0; i < dataModel.getCategoryCount(); i++) {
            currentSummedDataPoint = 0;
            for (int j=0; j < dataModel.getDataSeriesCount(); j++) {
                currentSummedDataPoint += dataModel.getDataSeriesForIndex(j).getValueForIndex(i);
            }
            maxSummedDataPoint = Math.max(maxSummedDataPoint, currentSummedDataPoint);
        }
        return maxSummedDataPoint;

    }

    public void paintIn(CpLightweightComponent lc, Graphics g, int x, int y, int w, int h)
    {
        int slopeY = Math.abs(slope.y);
        int slopeX = Math.abs(slope.x);
        int heightWithout3D = h - slopeY;
        int widthWithout3D = w - slopeX;
        int itemMargin = w/10;
        int numSeries = dataModel.getDataSeriesCount();
        int bottom, top;
        double topValue, bottomValue;

        int startX = x + (itemMargin/2);
        int totalItems = dataModel.getCategoryCount();
        int itemWidth = (widthWithout3D / totalItems) - itemMargin;
        for (int i = 0; i < totalItems; i++) {

            bottomValue = axisModel.getMin();
            bottom = y + h;
            for (int j=0; j < numSeries; j++) {
                Color faceColor = dataModel.getDataSeriesForIndex(j).getColor();
                Color horizontalSideColor = faceColor.darker();
                Color verticalSideColor = horizontalSideColor.darker();

                topValue = bottomValue + dataModel.getDataSeriesForIndex(j).getValueForIndex(i);
                // the slopeY subtraction is for handling overlap of bars...
                top = y + h - (int)(heightWithout3D * axisModel.getPercentageForValue(topValue)) - slopeY;

                Cp3DBarValueDisplayer.drawBarRect(
                    g,
                    lineColor,
                    faceColor,
                    horizontalSideColor,
                    verticalSideColor,
                    startX,
                    top,
                    itemWidth + slopeX,
                    bottom - top,
                    slope,
                    (j == numSeries - 1), // only draw a top for the top item
                    true,
                    true
                );

                bottom = top + slopeY + 1;
                bottomValue = topValue;
            }
            startX += itemWidth + itemMargin;
        }
    }

    Point seriesAndCategoryForPoint(Point mouseLocation, Rectangle rect)
    {
        int slopeY = Math.abs(slope.y);
        int slopeX = Math.abs(slope.x);
        int heightWithout3D = rect.height - slopeY;
        int widthWithout3D = rect.width - slopeX;
        int itemMargin = rect.width/10;
        int numSeries = dataModel.getDataSeriesCount();
        int bottom, top;
        double bottomValue, topValue;

        int startX = rect.x + (itemMargin/2);
        int totalItems = dataModel.getCategoryCount();
        int itemWidth = (widthWithout3D / totalItems) - itemMargin;
        for (int i = 0; i < totalItems; i++) {
            bottom = rect.y + rect.height;
            bottomValue = axisModel.getMin();
            for (int j=0; j < numSeries; j++) {
                if (mouseLocation.x < startX) {
                    return null;
                }
                topValue = bottomValue + dataModel.getDataSeriesForIndex(j).getValueForIndex(i);
                top = rect.y + rect.height - (int)(heightWithout3D * axisModel.getPercentageForValue(topValue));
                if (mouseLocation.x <= startX + itemWidth + slopeX && mouseLocation.y >= top && mouseLocation.y <= bottom) {
                    return new Point(j, i);
                }
                bottomValue = topValue;
                bottom = top + slopeY + 1;
            }
            startX += itemWidth + itemMargin;
        }
        return null;
    }
}

