package framework.test;

import java.awt.Component;
import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Clip;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.IndexedTriangleArray;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.Transform3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import fight3D.Stage;
import fight3D.StageManager;
import framework.RWT.RWTFrame3D;
import framework.RWT.RWTCanvas3D;
import framework.model3D.BaseObject3D;
import framework.model3D.Model3D;
import framework.model3D.ModelFactory;
import framework.model3D.Object3D;
import framework.model3D.Position3D;
import framework.model3D.Quaternion3D;
import framework.model3D.Universe;
import framework.physics.AngularVelocity3D;
import framework.physics.Force3D;
import framework.physics.Ground;
import framework.physics.PhysicalSystem;
import framework.physics.Solid3D;
import framework.view3D.Camera3D;

public class TestBrick {
	static final double GRAVITY = 9.8;


	public TestBrick() {
		
		long oldTime = System.currentTimeMillis();
//		public ArrayList<Solid3D> arraylist = new ArrayList<S>();
		// TODO Auto-generated constructor stub
		RWTFrame3D frame = new RWTFrame3D();
		RWTCanvas3D canvas = new RWTCanvas3D();
		frame.add(canvas);

		Universe universe = new Universe();
		

		
		PhysicalSystem p_solid = new PhysicalSystem();
		
		// カメラの設定
		Camera3D camera = new Camera3D(universe);
		canvas.attachCamera(camera);
		
		// ライトの設定
		DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f,
				1.0f, 1.0f), new Vector3f(0.0f, -1.0f, -0.1f));
		light.setInfluencingBounds(new BoundingSphere(new Point3d(), 50000.0));
		universe.placeLight(light);

		frame.setSize(720, 480);
		frame.setVisible(true);

		// ステージの設定
//		Object3D stageObject = ModelFactory.loadModel("data\\floor4.3ds").createObject();
//		stageObject = ModelFactory.loadModel("data\\stage3\\stage3.wrl").createObject();
		IndexedTriangleArray groundGeometry = new IndexedTriangleArray(4, 
				IndexedTriangleArray.COORDINATES | IndexedTriangleArray.NORMALS, 6);
		groundGeometry.setCoordinate(0, new Point3d(-1000.0, 0.0, -1000.0));
		groundGeometry.setCoordinate(1, new Point3d(1000.0, 0.0, -1000.0));
		groundGeometry.setCoordinate(2, new Point3d(1000.0, 0.0, 1000.0));
		groundGeometry.setCoordinate(3, new Point3d(-1000.0, 0.0, 1000.0));
		groundGeometry.setNormal(0, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setNormal(1, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setNormal(2, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setNormal(3, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setCoordinateIndices(0, new int[]{0, 3, 2});
		groundGeometry.setCoordinateIndices(3, new int[]{0, 2, 1});
		Appearance ap2 = new Appearance();
		Material m = new Material();
		m.setDiffuseColor(1.0f, 1.0f, 1.0f);
		m.setAmbientColor(0.5f, 0.5f, 0.5f);
		m.setSpecularColor(0.0f, 0.0f, 0.0f);
		m.setShininess(1.0f);
		ap2.setMaterial(m);
		ap2.setColoringAttributes(new ColoringAttributes(0.5f, 0.5f, 0.5f, ColoringAttributes.NICEST));
		BaseObject3D stageObject = new BaseObject3D(groundGeometry, ap2);
		Ground stageGround = new Ground(stageObject);

		// オブジェクトの設定
		Model3D model = ModelFactory.loadModel("data\\cubeBlue.3ds");
		Object3D objBlue = model.createObject();
		Model3D model2 = ModelFactory.loadModel("data\\cubeRed.3ds");
		Object3D objRed = model2.createObject();
		Transform3D s = new Transform3D();
		objBlue.children[0].scale.setTransform(s);
		
		int ARRAY_SIZE_X = 5;
		int ARRAY_SIZE_Y = 5;
		int ARRAY_SIZE_Z = 5;
		
		for(int k = 0 ; k < ARRAY_SIZE_Y ; k++){
			for(int l = 0 ; l < ARRAY_SIZE_X ; l++){
				for(int j = 0 ; j < ARRAY_SIZE_Z ; j++){
			
					Solid3D solid;
					if((j + k + l) % 2 == 0){
						solid = new Solid3D(objBlue);
					} else {
						solid = new Solid3D(objRed);
					}
			
					solid.apply(new Position3D(-5.0 + 2.0*l, 1.0+2.0*k, -5.0+2.0*j), false);
			
					solid.scale(2.0, 2.0, 2.0);
////					Quaternion3D q = new Quaternion3D(0.0, 0, 1.0, 0.7);
////					solid.apply(q, false);
//					solid.apply(new AngularVelocity3D(-0.1, 0.0, -0.2), false);
					universe.place(solid);
			
					camera.addTarget(solid);
			
					p_solid.add(solid);
				}
			}
		}

		universe.place(stageGround);

		// 表示
		universe.compile();

		// CollisionResult cr = new CollisionResult();
		// cr = PhysicsFacade.checkColision(solid, solid, solid2, solid2);

		int id = 0;
		
		for (;;) {
			try {
				Thread.sleep(1);
				camera.adjust(0);
				// w.move(17, stageObject);
				Force3D f = Force3D.ZERO;
//				if (solid.getPosition3D().getY() > -0.1) {
//					// System.out.println(time+","+solid.getPosition3D().getY());
//
//				}
				long newTime = System.currentTimeMillis();
//				p_solid.motion(id, 3, f, p_solid.objects.get(id)
//						.getGravityCenter(), stageGround);
				p_solid.motion(id, newTime - oldTime, f, p_solid.objects.get(id)
						.getGravityCenter(), stageGround);
				oldTime = newTime;
				// if(cr != null){
				// p_solid.move(id, 1, f, cr.collisionPoint, stageObject);
				// }
				// else{
				// p_solid.move(id, 1, f,
				// p_solid.objects.get(id).getGravityCenter(), stageObject);
				// )
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			time++;
		}	

	}

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