/*
 * Copyright 2012, kaj dijkstra , 
 * Author, Kaj Dijkstra.
 * All rights reserved.
 *
 */
import transform from './transform.js';
 import {matrix4} from './math.js';
 
 
 
/**
 * Entity object
**/
class entity{
	
	constructor( ) {
		
		this._className = 'entity';
		this.subEntitys = [];
		
		this.name;
		this.mesh;
		this.drawType = gl.TRIANGLES;
		this.transform = new transform();
		this.transform.entity = this;
		this.bone = false;
		
		this.children = [];
		this.parent = false;
		this.objectTransform;
		this.attenuation = 1.0;
		this.sourceRadius = 0.4;
		this.sourceLength = 0.2;
		
		this.attenuation = 1.;
		this.sourceRadius = 0.0;
		this.sourceLength = 0.0;
		this.id = kepler.entityID++;
		
		this.type = "Actor";
	}


	
	
	/**
	 * Translate entity to coordinate
	 * @param {(float x, float y, float z)} Position
	**/
	translateTo( x, y, z ) {
		
		this.transform.world = matrix4.translate(matrix4.identity(), kepler.vector3(x,y,z) );
	}

	addChild( entity ) {
		entity.parent = this;
		this.children.push(entity);
	}

	getChildren() {
		return this.children;
	}

	getChildByName( name ) {
		for(var c=0;c<this.childen.lenght; c++) {
			if(this.childen[c].name == name)
				return this.childen[c];
		}
	}

	getParent() {
		return this.parent;
	}

	/**
	 * add mesh to entity
	 * @param {(meshObject)} mesh
	**/
	addMesh(mesh) {
		
		//entity.setViewport( this.viewport );
		
		this.mesh = mesh;
		
		this.mesh.entityID = this.id;
		this.mesh.material.entityID = this.id;
		
		var subMeshes = this.mesh.subMeshes;
		
		for(var c = 0;c<subMeshes.length; c++) {
			var subMesh = subMeshes[c];
		
			var newSubEntity = {};//kepler.createObject("subEntity");
			newSubEntity.subMesh = subMesh;
			
			this.addSubEntity(newSubEntity);
		}
	};


	/**
	 * add subEntity to entity
	 * @param {(subEntityObject)} subEntity
	**/
	addSubEntity(subEntity) {
		this.subEntitys.push(subEntity);
	};


	/**
	 * get subentity from entity
	**/
	getSubEntitys() {
		return this.subEntitys;
	};


	/**
	 * update Uniforms
	 * @param {(subEntityObject)} subEntity
	**/
	updateUniforms() {
		var shader = this.shader;
		var transform = this.transform;
	}


	/**
	 * get updated 4x4 world matrix
	**/
	getUpdatedWorldMatrix() {
		var children = this.getChildOfNode(this, []).reverse();

		
		var transform =  matrix4.identity();
		
		for(var c = 0; c<children.length;c++) {
			var currentParent = children[c];
			transform = matrix4.mul(currentParent.transform.world, transform);
			
		}
		
		
		
		//transform = transform;//matrix4.scale(this.transform.world, [100, 100, 100]);
		
		return transform;
	}


	getChildOfNode(node, children){
		children.push(node);
		
		if(node.parent)
			return this.getChildOfNode(node.parent, children);
		else
			return children;
	} 
}

export {entity as default};