package org.xith3d.shapegeom;

import com.xith3d.scenegraph.*;
import com.xith3d.test.Cube;
import com.xith3d.render.RenderPeer;
import com.xith3d.render.CanvasPeer;
import com.xith3d.render.jogl.RenderPeerImpl;

import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Text3DTest extends JFrame implements MouseMotionListener, MouseListener, WindowListener, MouseWheelListener{

  private float rotX;
  private float rotY;
  private float startRotX;
  private float startRotY;
  private Point press;
  private TransformGroup objRotate;

  private Transform3D rotate;
  private boolean running = true;
  private float dist = 20.0f;
  private View view;

  public Text3DTest(){
    super("Text3D Test - mouse drag to rotate, wheel to zoom");
    this.setSize(640, 480);
    this.setLocationRelativeTo(null);
//    this.addMouseListener(this);
//    this.addMouseMotionListener(this);
    this.addWindowListener(this);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.createUniverse();


  }

  public void createUniverse(){
    // create the virtual univers
		VirtualUniverse universe = new VirtualUniverse();

		// add a view to the universe
		view = new View();
		universe.addView(view);

		// add a locale
		Locale locale = new Locale(5.0f, 0.0f, 10.0f);
		universe.addLocale(locale);

		// create a BranchGroup
		BranchGroup scene = new BranchGroup();
		locale.addBranchGraph(scene);

    AmbientLight light = new AmbientLight(true, new Color3f(0.75f, 0.75f, 0.75f));
    DirectionalLight dlight = new DirectionalLight(true, new Color3f(0.75f, 0.75f, 0.75f), new Vector3f(0.0f, -0.3f, -2.0f));
    BoundingSphere bounds = new BoundingSphere(new Vector3f(0f, 0f, 0f), 200.0f);
    dlight.setBounds(bounds);
    light.setBounds(bounds);

    scene.addChild(light);
    scene.addChild(dlight);

		// let objects along this path rotate
		rotate = new Transform3D();

		objRotate = new TransformGroup();
		scene.addChild(objRotate);

		objRotate.addChild(textTest());

  	// create a canvas for our graphics
		RenderPeer rp = new RenderPeerImpl();
		CanvasPeer cp = rp.makeCanvas(this.getContentPane(), 640, 480, 32, false);
    cp.getComponent().addMouseListener(this);
    cp.getComponent().addMouseMotionListener(this);
    cp.getComponent().addMouseWheelListener(this);
		Canvas3D canvas = new Canvas3D();

		canvas.set3DPeer(cp);

		// modify our view so we can see the cube
		view.addCanvas3D(canvas);
    Transform3D t = new Transform3D();
        t.lookAt(new Vector3f(0, 0,    dist),    // location of eye
        new Vector3f( 0, 0, 0),    // center of view
        new Vector3f( 0, 1, 0));    // vector pointing up

		view.setTransform(t);

        this.setVisible(true);
        while (running){
		      view.renderOnce();
        }
        System.exit(0);

  }


  public TransformGroup textTest(){
    TransformGroup tg = new TransformGroup();
    Transform3D trans = new Transform3D();

    Font f = new Font("serif", Font.PLAIN, 15);
    Text3D text = new Text3D(f, "fh%", 0.02, GeometryArray.COORDINATES|GeometryArray.NORMALS, 2.0f);

    Appearance app = new Appearance();
    Material mat = new Material();
    mat.setAmbientColor(new Color3f(0.5f,0.1f,0.1f));

    app.setMaterial(mat);

    PolygonAttributes polygonAttributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL,
        PolygonAttributes.CULL_BACK, 0f);
    app.setPolygonAttributes(polygonAttributes);


    text.setAppearance(app);

    tg.addChild(text);
    double w = text.getSt().getBounds().getWidth();
    trans.setTranslation(new Vector3f((float)-w/2.0f,0.0f,0.0f));
    tg.setTransform(trans);


    return tg;
  }


  public void mouseDragged(MouseEvent e) {
    double yDiff = press.getX() - e.getX();
    double xDiff = press.getY() - e.getY();
    rotX = startRotX + (float)xDiff * 0.01f;
    rotY = startRotY + (float)yDiff * 0.01f;
    rotate.rotXYZ(rotX, rotY, 0.0f);
    objRotate.setTransform(rotate);

  }

  public void mouseMoved(MouseEvent e) {

  }


  public void mouseClicked(MouseEvent e) {

  }

  public void mouseEntered(MouseEvent e) {

  }

  public void mouseExited(MouseEvent e) {

  }

  public void mousePressed(MouseEvent e) {

    press = e.getPoint();
    startRotX = rotX;
    startRotY = rotY;

  }

  public void mouseReleased(MouseEvent e) {

  }

  public static void main(String[] args) {
    new Text3DTest();
  }

  public void windowActivated(WindowEvent e) {

  }

  public void windowClosed(WindowEvent e) {

  }

  public void windowClosing(WindowEvent e) {
    running = false;

  }

  public void windowDeactivated(WindowEvent e) {

  }

  public void windowDeiconified(WindowEvent e) {

  }

  public void windowIconified(WindowEvent e) {

  }

  public void windowOpened(WindowEvent e) {

  }

  public void mouseWheelMoved(MouseWheelEvent e) {
    dist += e.getWheelRotation();
    Transform3D t = new Transform3D();
        t.lookAt(new Vector3f(0, 0,    dist),    // location of eye
        new Vector3f( 0, 0, 0),    // center of view
        new Vector3f( 0, 1, 0));    // vector pointing up
    view.setTransform(t);

  }
}
