import * as THREE from 'three';

import { TaperedLink } from './taperedLink.js';


const HYPERPLANE = 2.0;
const W_FORESHORTENING = 0.04;


class FourDShape extends THREE.Group {

	constructor(node_ms, link_ms, face_ms, structure) {
		super();
		this.node_ms = node_ms;
		this.link_ms = link_ms;
		this.face_ms = face_ms;
		this.nodes4 = structure.nodes;
		this.nodes3 = {};
		this.links = structure.links;
		this.faces = ( "faces" in structure ) ? structure.faces : [];
		this.node_scale = 1;
		this.link_scale = 1;
		this.hyperplane = HYPERPLANE;
		this.foreshortening = W_FORESHORTENING;
		this.initShapes();
	}



	// if a node/link has no label, use the 0th material

	getMaterialLabel(entity) {
    	    if( "label" in entity ) {
    	        return entity.label
    	    } else {
     	        return 0;
    	    }
	}

	makeNode(material, v3, scale) {
		const geometry = new THREE.SphereGeometry(this.node_size);
		const sphere = new THREE.Mesh(geometry, material);
		sphere.position.copy(v3);
		this.add(sphere);
		return sphere;
	}

	makeLink(materialLabel, link) {
		const n1 = this.nodes3[link.source];
		const n2 = this.nodes3[link.target];
		const s1 = this.link_scale * n1.scale;
		const s2 = this.link_scale * n2.scale;
		const basematerial = this.link_ms[materialLabel];
		const edge = new TaperedLink(basematerial, materialLabel, n1, n2, s1, s2);
		this.add( edge );
		return edge;
	}

	updateLink(link, links_show) {
		const n1 = this.nodes3[link.source];
		const n2 = this.nodes3[link.target];
		const s1 = this.link_scale * n1.scale;
		const s2 = this.link_scale * n2.scale;
		link.object.update(n1, n2, s1, s2);
		link.object.visible = (!links_show || links_show.includes(link.label)); 
	}


	setFaceGeometry(face, geometry) {
		const values = [];
		for( const f of face.nodes ) {
			const v3 = this.nodes3[f].v3;
			values.push(v3.x);
			values.push(v3.y);
			values.push(v3.z);
		}
		const v3 = this.nodes3[face.nodes[0]].v3;
		values.push(v3.x);
		values.push(v3.y);
		values.push(v3.z);
		const vertices = new Float32Array(values);
		geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
	}

	makeFace(material, face) {
		const geometry = new THREE.BufferGeometry();
		this.setFaceGeometry(face, geometry)
		const mesh = new THREE.Mesh( geometry, material );
		this.add(mesh);
		return mesh;
	}


	fourDscale(w) {
		return this.hyperplane / ( this.hyperplane + w );
	}
	
	fourDrotate(x, y, z, w, rotations) {
		const v4 = new THREE.Vector4(x, y, z, w);
		for ( const m4 of rotations ) {
			v4.applyMatrix4(m4);
		}
		return v4;
	}

	fourDtoV3(v4) {
		const k = this.fourDscale(v4.w);
		return new THREE.Vector3(v4.x * k, v4.y * k, v4.z * k);		
	}

	initShapes() {
		for( const n of this.nodes4 ) {
			const k = this.fourDscale(n.w);
			const v3 = new THREE.Vector3(n.x * k, n.y * k, n.z * k);
			const material = this.node_ms[this.getMaterialLabel(n)];
			this.nodes3[n.id] = {
				v3: v3,
				scale: k,
				label: n.label,
				object: this.makeNode(material, v3, k)
			};
		}
		for( const l of this.links ) {
			const mLabel = this.getMaterialLabel(l);
			l.object = this.makeLink(mLabel, l);
		}
		for( const f of this.faces ) {
			const material = this.face_ms(this.getMaterialLabel(f));
			f.object = this.makeFace(material, f);
		}
	}


	render3(rotations, nodes_show, links_show) {
		this.scalev3 = new THREE.Vector3(this.node_scale, this.node_scale, this.node_scale);
		for( const n of this.nodes4 ) {
			const v4 = this.fourDrotate(n.x, n.y, n.z, n.w,  rotations);
			const k = this.fourDscale(v4.w);
			const v3 = new THREE.Vector3(v4.x * k, v4.y * k, v4.z * k);
			const s4 = k * this.node_scale * this.foreshortening;
			const s3 = new THREE.Vector3(s4, s4, s4);
			this.nodes3[n.id].v3 = v3;
			this.nodes3[n.id].scale = k * this.foreshortening;
			this.nodes3[n.id].object.position.copy(v3);
			this.nodes3[n.id].object.scale.copy(s3);
			this.nodes3[n.id].object.visible = ( !nodes_show || nodes_show.includes(n.label) );
		}
		for( const l of this.links ) {
			this.updateLink(l, links_show);
		}

		for( const f of this.faces ) {
			this.setFaceGeometry(f, f.object.geometry);
		}
	}

}

export { FourDShape };
