package cnp.ew.charts;

import java.awt.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;

public class CpLineValuesDisplayer extends CpAbstractChartValuesDisplayer
{

    static int mouseWidth = 5;

    public void paintIn(CpLightweightComponent lc, Graphics g, int x, int y, int w, int h)
    {

        int startX, startY, endX, endY;
        CpDataSeries dataSeries;

        int categoryCount = dataModel.getCategoryCount();
        int dataSeriesCount = dataModel.getDataSeriesCount();
        int pointSpacing = w / (categoryCount - 1);


        for (int i = 0; i < dataSeriesCount; i++) {
            dataSeries = dataModel.getDataSeriesForIndex(i);
            g.setColor(dataSeries.getColor());
            startX = x;
            startY = y + h - (int)(h * axisModel.getPercentageForValue(dataSeries.getValueForIndex(0)));

            for (int j=1; j < categoryCount; j++) {
                endX = startX + pointSpacing;
                endY = y + h - (int)(h * axisModel.getPercentageForValue(dataSeries.getValueForIndex(j)));
                g.drawLine(startX, startY, endX, endY);
                startX = endX;
                startY = endY;
            }
        }
    }

    Point seriesAndCategoryForPoint(Point mouseLocation, Rectangle innerGraphRect)
    {
        CpDataSeries dataSeries;
        int dataPointX, dataPointY;

        int bottom = innerGraphRect.y + innerGraphRect.height;
        int categoryCount = dataModel.getCategoryCount();
        int dataSeriesCount = dataModel.getDataSeriesCount();
        int pointSpacing = innerGraphRect.width / (categoryCount - 1);
        dataPointX = innerGraphRect.x;

        for (int i = 0; i < categoryCount; i++) {
            if (Math.abs(dataPointX - mouseLocation.x) < mouseWidth) {
                for (int j=0; j < dataSeriesCount; j++) {
                    dataSeries = dataModel.getDataSeriesForIndex(j);
                    dataPointY = bottom - (int)(innerGraphRect.height * axisModel.getPercentageForValue(dataSeries.getValueForIndex(i)));
                    if (Math.abs(dataPointY - mouseLocation.y) < mouseWidth) {
                        return new Point(j, i);
                    }
                }
            }
            dataPointX += pointSpacing;
        }
        return null;
    }
}


