/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */
package glass.rain;

import com.sun.glass.ui.*;
import com.sun.glass.events.*;

import com.sun.rain.graphics.*;

public class SparkleEyes implements Launchable
{
    static final String TITLE = "SparkleEyes";
    
    public static void main(String args[])
    {
        Applet.Run(args, TITLE, new SparkleEyes());
    }
    
    public void finishLaunching(String args[])
    {
        try
        {
            Applet applet = Applet.GetApplet();
            
            View view = Application.GetApplication().createView(new MyGraphics());
            view.setEventHandler(new MyViewEventHandler());
            applet.setView(view);

            //TODO: create FPS timer
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
    
    static int EYE_SIZE = 150;
    static int PUPIL_SIZE = 50;
    class MyGraphics extends Graphics2D
    {
        @Override
        public void paint(long time, int width, int height)
        {
            push();
            {
                set(Color.YELLOW);
                fillRect(0, 0, width, height);
                
                int centerX = width/2;
                int centerY = height/2;
                
                int leftEyeX = (centerX - EYE_SIZE)/2;
                int rightEyeX = centerX + leftEyeX;
                
                set(Color.BLACK);
                fillEllipse(leftEyeX, centerY-(EYE_SIZE/2), EYE_SIZE, EYE_SIZE);
                fillEllipse(rightEyeX, centerY-(EYE_SIZE/2), EYE_SIZE, EYE_SIZE);

                // left
                {
                    int mouseSceneX = leftEyeX + (EYE_SIZE/2) - mouseX;
                    int mouseSceneY = centerY - mouseY;

                    double mouseVectorLength = Math.sqrt(mouseSceneX*mouseSceneX + mouseSceneY*mouseSceneY);
                    double mouseVectorX = (double)mouseSceneX / mouseVectorLength;
                    double mouseVectorY = (double)mouseSceneY / mouseVectorLength;
                    
                    push();
                    {
                        translate(leftEyeX+(EYE_SIZE/2)-(PUPIL_SIZE/2), centerY-(EYE_SIZE/2)+PUPIL_SIZE);
                        
                        double distance = EYE_SIZE - 2*PUPIL_SIZE - 10;
                        mouseVectorX *= -distance;
                        mouseVectorY *= -distance;
                        
                        translate((int)mouseVectorX, (int)mouseVectorY);

                        set(Color.WHITE);
                        fillEllipse(0, 0, PUPIL_SIZE, PUPIL_SIZE);
                    }
                    pop();
                }
                
                // right
                {
                    int mouseSceneX = rightEyeX + (EYE_SIZE/2) - mouseX;
                    int mouseSceneY = centerY - mouseY;

                    double mouseVectorLength = Math.sqrt(mouseSceneX*mouseSceneX + mouseSceneY*mouseSceneY);
                    double mouseVectorX = (double)mouseSceneX / mouseVectorLength;
                    double mouseVectorY = (double)mouseSceneY / mouseVectorLength;
                    
                    push();
                    {
                        translate(rightEyeX+(EYE_SIZE/2)-(PUPIL_SIZE/2), centerY-(EYE_SIZE/2)+PUPIL_SIZE);

                        double distance = EYE_SIZE - 2*PUPIL_SIZE - 10;
                        mouseVectorX *= -distance;
                        mouseVectorY *= -distance;

                        translate((int)mouseVectorX, (int)mouseVectorY);

                        set(Color.WHITE);
                        fillEllipse(0, 0, PUPIL_SIZE, PUPIL_SIZE);
                    }
                    pop();
                }
            }
            pop();
        }
    }
    
    int mouseX = 0;
    int mouseY = 0;
    class MyViewEventHandler extends View.EventHandler
    {
        public void handleRunLoop(View view, long time)
        {
            // application paint
            Pen pen = view.getPen();
            pen.begin();
            {
                pen.paint(time, view.getWidth(), view.getHeight());
            }
            pen.end();
        }
        
        @Override
        public void handleKeyEvent(View view, long time, int type, int keyCode, char[] keyChars, int modifiers)
        {
            if ((type == KeyEvent.RELEASE) && (keyCode == KeyEvent.VK_F))
            {
                view.toggleFullscreen(true, true, false);
            }
        }
        
        @Override
        public void handleMouseEvent(View view, long time, int type, int button, int x, int y, int xAbs, int yAbs, int clickCount, int modifiers, boolean isPopupTrigger, boolean isSythesized)
        {
            mouseX = x;
            mouseY = y;
        }
    }
}
