package cnp.ew.charts;

import java.awt.*;
import cnp.ew.displayer.*;
import cnp.ew.lightweight.*;

public class CpPieValuesDisplayer extends CpAbstractChartValuesDisplayer
{

    public void paintIn(CpLightweightComponent lc, Graphics g, int x, int y, int width, int height)
    {
        CpDataSeries dataSeries;
        int startAngle = 0;
        int filledAngle;
        int size = Math.min(width, height) - 1;
        int centerX  = x + size/2;
        int centerY = y + size/2;
        double percentage;
        int [] startAngles = new int[dataModel.getDataSeriesCount()];

        g.setColor(lc.getBackground());
        g.fillRect(x, y, width, height);

        for (int i = 0; i < dataModel.getDataSeriesCount(); i++) {
            dataSeries = dataModel.getDataSeriesForIndex(i);
            g.setColor(dataSeries.getColor());
            if (i == dataModel.getDataSeriesCount() - 1) {
                filledAngle = 360 - startAngle;
                percentage = 1;
            } else {
                percentage = dataModel.getPercentageValue(i, 0);
                double doubleFilledAngle = (360 * percentage + 0.5);
                filledAngle = (int)doubleFilledAngle;
            }

            g.fillArc(
               x,
               y,
               size,
               size,
               startAngle,
               filledAngle);

            startAngles[i] = startAngle;

           startAngle += filledAngle;
        }

        for (int i = 0; i < dataModel.getDataSeriesCount(); i++) {
            g.setColor(Color.black);
            double filledRadianAngle = Math.PI * 2 * startAngles[i] / 360;
            g.drawLine(
                centerX,
                centerY,
                (int)(centerX + (size/2) * Math.cos(filledRadianAngle)),
                (int)(centerY - (size/2) * Math.sin(filledRadianAngle)));
        }
        g.drawOval(x, y, size, size);
    }

    public Point seriesAndCategoryForPoint(Point mouseLocation, Rectangle rect)
    {

        int size = Math.min(rect.width, rect.height);
        if (mouseLocation.x > rect.x + size || mouseLocation.y > rect.y + size) {
            return null;
        }

        int angle = angleForPoint(new Point(mouseLocation.x, mouseLocation.y), rect);
        int currentAngle = 0;

        for (int i = 0; i < dataModel.getDataSeriesCount(); i++) {
            double percentage = dataModel.getPercentageValue(i, 0);
            currentAngle += (int)((360 * percentage) + 0.5);
            if (currentAngle >= angle) {
                return new Point(i, 0);
            }
        }
        return null;
    }

    int angleForPoint(Point mouseLocation, Rectangle rect)
    {
        int size = Math.min(rect.width, rect.height);
        int centerX = size / 2;
        int centerY = size / 2;
        double x, y;

        if (mouseLocation.y < centerY) {
            y = (centerY - mouseLocation.y);
        } else {
            y = -(mouseLocation.y - centerY);
        }

        if (mouseLocation.x < centerX) {
            x = -(centerX - mouseLocation.x);
        } else {
            x = mouseLocation.x - centerX;
        }

        if (y == 0) {
            if (x > 0) {
                return 0;
            } else {
                return 180;
            }
        }
        if (x == 0) {
            if (y > 0) {
                return 90;
            } else {
                return 270;
            }
        }

        int angle = (int)(Math.atan(Math.abs(y / x))  * 180 / Math.PI);
        if (x < 0 && y < 0) {
            angle += 180;
        } else if (x < 0) {
            angle = 180 - angle;
        } else if (y < 0) {
            angle = 360 - angle;
        }

        return angle;
    }

}


