package cnp.ew.charts;

import java.awt.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;

public class CpBarValuesDisplayer extends CpAbstractChartValuesDisplayer
{

    public void paintIn(CpLightweightComponent lc, Graphics g, int x, int y, int w, int h)
    {
        int startX, top, barHeight;
        CpDataSeries dataSeries;

        int barMargin = w / 40;
        int barGroupMargin = barMargin * 4;
        int categoryCount = dataModel.getCategoryCount();
        int barGroupWidth = (w / categoryCount) - barGroupMargin;
        int dataSeriesCount = dataModel.getDataSeriesCount();
        int barWidth = (barGroupWidth / dataSeriesCount) - barMargin;

        int groupStartX = x + barGroupMargin / 2;

        for (int i = 0; i < categoryCount; i++) {
            startX = groupStartX + barMargin/2;
            for (int j=0; j < dataSeriesCount; j++) {
                dataSeries = dataModel.getDataSeriesForIndex(j);
                g.setColor(dataSeries.getColor());
                barHeight = (int)(h * axisModel.getPercentageForValue(dataSeries.getValueForIndex(i)));
                top = y + h - barHeight;
                g.fillRect(startX, top, barWidth, barHeight);
                g.setColor(Color.black);
                // don't subtract -1 on bottom, since we want it to overlap with the baseline
                // (this is cheating of a sort; didn't want to draw three lines, rather than a rect)
                g.drawRect(startX, top, barWidth - 1, barHeight);

                startX += barWidth + barMargin;
            }

            groupStartX += barGroupWidth + barGroupMargin;
        }
    }

    Point seriesAndCategoryForPoint(Point mouseLocation, Rectangle rect)
    {
        int startX, top, barHeight;
        CpDataSeries dataSeries;

        int barMargin = rect.width / 40;
        int barGroupMargin = barMargin * 4;
        int categoryCount = dataModel.getCategoryCount();
        int barGroupWidth = (rect.width / categoryCount) - barGroupMargin;
        int dataSeriesCount = dataModel.getDataSeriesCount();
        int barWidth = (barGroupWidth / dataSeriesCount) - barMargin;

        int groupStartX = rect.x + barGroupMargin / 2;


        for (int i = 0; i < categoryCount; i++) {
            startX = groupStartX + barMargin/2;
            for (int j=0; j < dataSeriesCount; j++) {

                if (mouseLocation.x < startX) {
                    return null;
                }

                dataSeries = dataModel.getDataSeriesForIndex(j);
                barHeight = (int)(rect.height * axisModel.getPercentageForValue(dataSeries.getValueForIndex(i)));
                top = rect.y + rect.height - barHeight;

                if (mouseLocation.x <= startX + barWidth && mouseLocation.y >= top) {
                    return new Point(j, i);
                }
                startX += barWidth + barMargin;
            }

            groupStartX += barGroupWidth + barGroupMargin;
        }
        return null;
    }
}

