package cnp.ew.misc;
import cnp.ew.util.*;
import cnp.ew.displayer.*;
import java.awt.*;
import cnp.ew.lightweight.*;

public class CpEyeballLc extends CpAbstractLc
{
    static int eyeballMargin=2;
    int eyeballRadius;

    Color fillColor = Color.white;

    public void paint(Graphics g, Rectangle rect)
    {
        Rectangle clientRect = getClientRect();

        //g.setColor(getBackground());
        //g.fillRect(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
        //g.setColor(fillColor);
        //g.fillOval(clientRect.x, clientRect.y, clientRect.width - 1, clientRect.height - 1);
        g.setColor(getForeground());
        g.drawOval(clientRect.x, clientRect.y, clientRect.width - 1, clientRect.height - 1);

        g.setColor(Color.black);


        eyeballRadius = Math.min(clientRect.width, clientRect.height)/10;

        //eyeballRadius = eyeballImage.getWidth(CpToolkit.defaultComponent()) / 2;
        //System.out.println("eyeballRadius: " + eyeballRadius);
        Point eyeballCenter = eyeballCenter();
        g.fillOval(eyeballCenter.x - eyeballRadius, eyeballCenter.y - eyeballRadius, eyeballRadius * 2, eyeballRadius * 2);
        //g.drawImage(eyeballImage, eyeballCenter.x - eyeballRadius, eyeballCenter.y - eyeballRadius, CpToolkit.defaultComponent());
        g.setColor(Color.white);
        g.fillOval(eyeballCenter.x - eyeballRadius/4, eyeballCenter.y - eyeballRadius/4, eyeballRadius / 2, eyeballRadius / 2);
    }

    public Point eyeballCenter()
    {
        double radiusX, radiusY, outerX, outerY;
        Dimension size = size();
        Rectangle clientRect = getClientRect();

        int centerX = clientRect.x + size.width / 2;
        int centerY = clientRect.y + size.height / 2;

        if (size.width > size.height) {
            radiusX = size.width/2;
            radiusY = radiusX * size.height / size.width;
        } else {
            radiusY = size.height/2;
            radiusX =radiusY * size.width /size.height;
        }

        Point mouseLocation = mouseLocation();

        int deltaX = mouseLocation.x - centerX;
        int deltaY = mouseLocation.y - centerY;


        double angle = Math.atan(Math.abs(((double)deltaY)/deltaX));

        double eyeballRadiusX = (eyeballRadius + eyeballMargin) * Math.cos(angle);
        double eyeballRadiusY = (eyeballRadius + eyeballMargin) * Math.sin(angle);
        if (deltaX > 0) {
            outerX = centerX + radiusX * Math.cos(angle) - eyeballRadiusX;
        } else {
            outerX = centerX - radiusX * Math.cos(angle) + eyeballRadiusX;
        }
        if (deltaY > 0) {
            outerY = centerY + radiusY * Math.sin(angle) - eyeballRadiusY;
        } else {
            outerY = centerY - radiusY * Math.sin(angle) + eyeballRadiusY;
        }

        return new Point((int)outerX, (int)outerY);
    }
}

