import javax.media.j3d.*;
import com.sun.j3d.utils.picking.*;
import java.util.*;
import java.awt.*;
import java.awt.Event;
import java.awt.AWTEvent;
import java.awt.event.MouseEvent;
import javax.vecmath.*;
import java.text.*;

public class PickBehavior extends Behavior {
    Shape3D part;                 // array of Shape3Ds passed in the constructor
    Shape3D shape = null;           // a Shape3D used as a pointer for picking
    private WakeupCriterion AWTEventCondition;      // holds the wakeup criterion to ease resetting the trigger
    WakeupOnAWTEvent awaken;        // holds the event that triggered the wakeup
    PickCanvas pickCanvas;          // allows picking
    AWTEvent[] eventArray;          // allows us to determine which mouseevent triggered the wakeup

    Color3f[][] tileColors;

    public PickBehavior(BranchGroup root, Shape3D part, Canvas3D canvas, Bounds bounds, Color3f[][] tileColors) {
        super();
        this.setSchedulingBounds(bounds);
        this.part = part;
        this.tileColors = tileColors;
        pickCanvas = new PickCanvas(canvas, root);
        // set the pickCanvas object to use geometry-based picking (rather than bounds)
        pickCanvas.setMode(PickTool.GEOMETRY);
        pickCanvas.setTolerance(1.0f);
        // this object's processStimulus method will be called by the system when a "MOUSE_PRESSED" event occurs
        AWTEventCondition = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
System.out.println("PickBehavior constructed...");


    }

    public void initialize() {
        // set initial wakeup condition
        this.wakeupOn(AWTEventCondition);
System.out.println("PickBehavior initialized...");
    }

    // processStimulus is awakened by mouse clicks.  This method is called by the J3D environment only.
    // This method determines which object was clicked, determines whether the click was modified by
    // the control key, and responds by calling the appropriate method.
    public void processStimulus(Enumeration criteria) {

System.out.println("\n...got a click...");

        // get source of event event
        awaken = (WakeupOnAWTEvent)criteria.nextElement();

        // get the array of AWTEvents
        eventArray = awaken.getAWTEvent();

        // pull out the mouse event that triggered the stimulus
        MouseEvent currentEvent = (MouseEvent)eventArray[0];

        // get a reference to the object that was picked
        pickCanvas.setShapeLocation(currentEvent);

        PickResult result = pickCanvas.pickClosest();
        if (result != null) {
//System.out.println("scene graph path to pick location: "+result.getSceneGraphPath().toString());

System.out.println("Number of GeometryArrays returned: "+result.numGeometryArrays());
System.out.println("number of intersections: "+ result.numIntersections());



            if (result.numIntersections() > 0) {
                PickIntersection pickIntersection = result.getIntersection(0);
                int index = pickIntersection.getGeometryArrayIndex();
System.out.println("Index of picked shape: "+ index);

                int vertexCount = ((GeometryArray)part.getGeometry(index)).getVertexCount();

System.out.println("length of color array in the Geometry returned:"+ vertexCount);

                for (int i=0;i<vertexCount;i++) {
                    //newPanelColor = new Color3f(0.0f,1.0f,0.0f);
                    ((GeometryArray)part.getGeometry(index)).setColor(i,new Color3f(0.0f,1.0f,0.0f));
                }

            }
        }
        this.wakeupOn(AWTEventCondition);
    }

}
