
AFRAME.registerComponent('pen-material', {
	init: function () {
	
		var baseColor = new THREE.TextureLoader().load( './images/pen_mat_baseColor.png' );
		baseColor.flipY = false;
		var normal = new THREE.TextureLoader().load('./images/pen_mat_normal.png');
		normal.flipY = false;
		var orm = new THREE.TextureLoader().load('./images/pen_mat_occlusionRoughnessMetallic.png');
		orm.flipY = false;
		// var emissive = new THREE.TextureLoader().load('./images/pen_mat_emissive.png');
		// emissive.flipY = false;
		// var medicineTexture = new THREE.TextureLoader().load('./images/medicine_mat_emissive.png');
		// medicineTexture.flipY = false;
		// var medicineAlpha = new THREE.TextureLoader().load('./images/medicine_mat_alpha.jpg');
		// medicineAlpha.flipY = false;
		var arrowTexture = new THREE.TextureLoader().load('./images/arrowCards_color_v2.jpg');
		arrowTexture.flipY = false;
		var arrowAlpha = new THREE.TextureLoader().load('./images/arrowCards_alpha_v5.jpg');
		arrowAlpha.flipY = false;
		var envMapRoot = "./images/EnvMap/";
		var envMapImages = [ envMapRoot + "posx.jpg", envMapRoot + "negx.jpg",
								envMapRoot + "posy.jpg", envMapRoot + "negy.jpg",
								envMapRoot + "posz.jpg", envMapRoot + "negz.jpg" ];
		var textureCube = new THREE.CubeTextureLoader().load( envMapImages );

		var thighColor = new THREE.TextureLoader().load( './images/thigh_albedo_01.png' );
		thighColor.flipY = false;
	  
	  this.baseMaterial  = new THREE.MeshStandardMaterial({
		map : baseColor,
		metalness : 1,
		metalnessMap : orm,
		normalMap : normal,
		roughnessMap : orm,
		// emissive : new THREE.Color( 1, 1, 1 ),
		// emissiveIntensity: .05,
		// emissiveMap : emissive,
		roughness : 1,
		aoMap : orm,
		aoMapIntensity : 1,
		skinning : true,
		envMap: textureCube,
		envMapIntensity: 1,
	  });

	  this.capMaterial = new THREE.MeshStandardMaterial({
		map : baseColor,
		metalness : 1,
		metalnessMap : orm,
		normalMap : normal,
		roughnessMap : orm,
		emissive : new THREE.Color( .49, .216, .024 ),
		emissiveIntensity: .6,
		roughness : 1,
		aoMap : orm,
		aoMapIntensity : 1,
		skinning : true,
		envMap: textureCube,
		envMapIntensity: 1,
	  });

	  this.glassMaterial = new THREE.MeshPhysicalMaterial({
		color : new THREE.Color( .9, .9, 1 ),
		normalMap : normal,
		roughnessMap : orm,
		roughness : .5,
		envMap : textureCube,
		envMapIntensity : 1,
		metalness : .7,
		reflectivity : 1,
		transparent: true,
		transparency : 1,
		skinning : true,
		depthWrite: false,
		depthTest: true
	  });

	  this.medicineMaterial  = new THREE.MeshBasicMaterial({
		// map : medicineTexture,
		// alphaMap : medicineAlpha,
		transparent: false,
		opacity: 0,
		alphaTest: .5,
		depthWrite: true,
		side: THREE.DoubleSide, 
		skinning : true,
	  });

	  this.arrowLockMaterial = new THREE.MeshLambertMaterial({
		map: arrowTexture,
		alphaMap: arrowAlpha,
		alphaTest: 0,
		transparent: true,
		depthTest: true,
		depthWrite: false,
		skinning : true,
		side: THREE.DoubleSide,
	  });
	  
	  this.spinLockMaterial = new THREE.MeshLambertMaterial({
		map: arrowTexture,
		alphaMap: arrowAlpha,
		transparent: true,
		depthTest: true,
		depthWrite: false,
		skinning : true,
		side: THREE.DoubleSide,
	  });

	  this.arrowCapMaterial = new THREE.MeshLambertMaterial({
		map: arrowTexture,
		alphaMap: arrowAlpha,
		alphaTest: 0,
		transparent: true,
		depthTest: true,
		depthWrite: false,
		skinning : true,
		side: THREE.DoubleSide,
	  });

	  this.spinCapMaterial = new THREE.MeshLambertMaterial({
		map: arrowTexture,
		alphaMap: arrowAlpha,
		transparent: true,
		depthTest: true,
		depthWrite: false,
		skinning : true,
		side: THREE.DoubleSide,
	});

	this.thighMaterial  = new THREE.MeshPhysicalMaterial({
		map : thighColor,
		emissive : new THREE.Color( 1, 1, 1 ),
		emissiveIntensity: .4,
		emissiveMap: thighColor,
		envMap: textureCube,
		envMapIntensity: 1,
		skinning : true,
	});

		// spEnvMap.mapping = THREE.SphericalReflectionMapping;
		// this.newMaterial.envMap = spEnvMap;
		// this.newMaterial.needsUpdate = true;

	  // Wait for model to load.
	  this.el.addEventListener('model-loaded', () => {
		// Grab the mesh / scene.
		const obj = this.el.getObject3D('mesh');
		// Go over the submeshes and modify materials we want.
		obj.traverse(node => {
			if(node.isMesh)
			{
				node.geometry.boundingSphere.radius = Number.MAX_VALUE;
			}
			if (node.isMesh && node.name.includes("medicine")) {
				node.material = this.medicineMaterial;
				node.visible = false;
				node.castShadow = false;
				node.renderOrder = 0;
			} else if(node.isMesh && node.name.includes("vial") || node.name.includes("window") || node.name.includes("cap_innerPipe") || node.name.startsWith("cap_plunger") || node.name.startsWith("needleShield")){
				node.material = this.glassMaterial;
				node.castShadow = false;
				node.renderOrder = 1;
			} else if(node.isMesh && node.name.includes("pcisePen_rigLock_Arrow")){
				node.material = this.arrowLockMaterial;
				node.material.opacity = 0;
				node.castShadow = false;
			} else if(node.isMesh && node.name.includes("pcisePen_rigLock_Spin")){
				node.material = this.spinLockMaterial;
				node.material.opacity = 0;
				node.castShadow = false;
				node.renderOrder = 1;
			} else if(node.isMesh && node.name.includes("pcisePen_rigCap_Arrow")){
				node.material = this.arrowCapMaterial;
				node.material.opacity = 0;
				node.castShadow = false;
			} else if(node.isMesh && node.name.includes("pcisePen_rigCap_Spin")){
				node.material = this.spinCapMaterial;
				node.material.opacity = 0;
				node.castShadow = false;
				node.renderOrder = 1;
			} else if(node.isMesh && node.name.includes("thigh")) {
				node.material = this.thighMaterial;
				node.renderOrder = 1;
			} else if(node.isMesh && node.name.includes("cap_main")) {
				node.material = this.capMaterial;
				node.renderOrder = 1;
			} else if(node.isMesh) {
				node.material = this.baseMaterial;
				node.castShadow = true;
				node.receiveShadow = false;
				node.renderOrder = 1;
			}
		});
	  });

	//   this.el.addEventListener('hide-arrows')
	}
  });