package game;

import framework.gameMain.Movable;
import framework.gameMain.OvergroundActor;
import framework.model3D.BaseObject3D;
import framework.model3D.Object3D;
import framework.model3D.Position3D;
import framework.model3D.Quaternion3D;
import framework.physics.Ground;
import framework.physics.Solid3D;
import framework.physics.Velocity3D;
import java3d.Appearance;
import java3d.Box;
import java3d.Cylinder;
import java3d.Material;
import java3d.TransformGroup;
import java3d.Vector3d;

public class LaserTurret implements Movable {
	
	OvergroundActor actor;
	double angle;
	String state;
	
	String sId;
	
	int startTime = 0;	//laserを発射した時間
	
	private Laser laser;
	
	public LaserTurret(double x, double y, double z, double angle, String state, String sId) {
		
		System.out.println("LaserBody.java const in");
		//タレット作成
		Appearance ap1 = new Appearance();
		Material color = new Material();
		color.setEmissiveColor(0.0f, 0.5f, 1.0f);
		ap1.setMaterial(color);


		//タレットの大きさ
		Box b1 = new Box(5.0f, 5.0f, 5.0f, ap1);
		Cylinder c1 = new Cylinder(3.0f, 10.0f, ap1);
		Object3D ob1 = new Object3D("LaserTurretBox", b1);
		ob1.apply(new Position3D(0.0, 0.0, 0.0), false);
		Object3D cy1 = new Object3D("LasserTurretCylin", c1);
		cy1.apply(new Quaternion3D(-1.0, 0.0, 0.0,Math.PI/2), false);

	    cy1.apply(new Position3D(0.0, 0.0, -5.0), false);
		Object3D LaTu = new Object3D("LasserTurret", new Object3D[] {
				cy1,ob1
		});

		LaTu.apply(new Position3D(0.0, 50.0, 0.0), false);
		Solid3D LasTur = new Solid3D(LaTu);
		actor = new OvergroundActor(LasTur,null);

		actor.setPosition(new Position3D(x,y,z));
		
		actor.setInitialDirection(new Vector3d(0.0, 0.0, -1.0));
		setAngle(angle);
		
		setState(state);
		
		this.sId = sId;
		
		System.out.println("LaserBody.java const out");
	}
	
	//angel
	public double getAngle() {
		return angle;
	}
	
	public void setAngle(double angle) {
		Vector3d initialDir = actor.getInitialDirection().clone();
		double x = initialDir.getZ()* Math.sin(angle) + initialDir.getX()*Math.cos(angle);
		double z = initialDir.getZ()* Math.cos(angle) - initialDir.getX()*Math.sin(angle);
		actor.setDirection(new Vector3d(x, 0.0, z));
		
		this.angle = angle;
	}
	
	//laser
	public Laser getLaser() {
		return laser;
	}

	public Laser createLaser(double x,double y,double z) {
		Laser laser = new Laser(x, y, z);
		//setPositionは後で調整
		laser.setPosition(new Position3D(this.getX(), this.getY(), this.getZ()));
		
		Vector3d vec = this.getDirection();
		vec.normalize();
		
		laser.setDirection(vec);
		
		vec.scale(20);
		Velocity3D vel = new Velocity3D(vec);
		
		laser.setVelocity(vel);
		
		this.laser = laser;
		return laser;
	}
	
	//state
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;		
	}
	
	//sId
	public String getsId() {
		return sId;
	}
	
	//startTime
	public void setStartTime(int countTime) {
		startTime = countTime;
	}
	public int getStartTime() {
		return startTime;
	}
	
	//X座標を取得
	public double getX(){
		return ((Object3D)actor.getBody()).getPosition3D().getX();
	}
	
	//Y座標を取得
	public double getY(){
		return ((Object3D)actor.getBody()).getPosition3D().getY();
	}
	
	//Z座標を取得
	public double getZ(){
		return ((Object3D)actor.getBody()).getPosition3D().getZ();
	}
	
	public void setPosition(double x, double y, double z) {
		actor.setPosition(new Position3D(x,y,z));
	}
	
	//LaserTurretの基準の向きを設定
	public void setInitialDirection(Vector3d v){
		actor.setInitialDirection(v);
	}
	
	//LaserTurretの向きを設定
	public void setDirection(Vector3d dir){
		actor.setDirection(dir);
	}

	//LaserTurretの向きを取得
	public Vector3d getDirection(){
		return actor.getDirection();
	}

	@Override
	public void motion(long interval, Ground ground) {
		actor.motion(interval, ground);
		
	}
	
	@Override
	public BaseObject3D getBody() {
		return actor.getBody();
	}
	
	public void setVelocity(Velocity3D velocity3d) {
		actor.setVelocity(velocity3d);
	}

	@Override
	public TransformGroup getTransformGroupToPlace() {
		// TODO Auto-generated method stub
		return null;
	}
}