import { Group, Vector3, Object3D } from '../../../../three.js/src/Three';

import { CAMERA_OFFSET, LABEL_STATION, LEG_SPLAY } from '../core/constants';
import { GlyphString } from '../core/GlyphString';
import { Materials } from '../materials/Materials';

var _tmpVector3 = new Vector3();

function StationLabels () {

	Group.call( this );

	this.type = 'CV.StationLabels';
	this.layers.set( LABEL_STATION );

	this.junctionLabelMaterial = Materials.getGlyphMaterial( 'normal helvetica,sans-serif', 0, [ 1, 1, 0 ] );
	this.defaultLabelMaterial = Materials.getGlyphMaterial( 'normal helvetica,sans-serif', 0 );
	this.splayLabelMaterial = Materials.getGlyphMaterial( 'normal helvetica,sans-serif', 0, [ 0.6, 0.6, 0.6 ] );

}

StationLabels.prototype = Object.create ( Group.prototype );

StationLabels.prototype.constructor = StationLabels;

StationLabels.prototype.addStation = function ( station ) {

	var label = new Object3D();

	label.layers.set( LABEL_STATION );

	label.position.copy( station.p );

	label.hitCount = station.hitCount;
	label.station = station;

	label.visible = false;

	this.add( label );

};

StationLabels.prototype.update = function ( camera, target, inverseWorld ) {

	var cameraPosition = _tmpVector3.copy( camera.position );

	if ( camera.isOrthographicCamera ) {

		// if orthographic, calculate 'virtual' camera position

		cameraPosition.sub( target ); // now vector from target

		cameraPosition.setLength( CAMERA_OFFSET / camera.zoom ); // scale for zoom factor
		cameraPosition.add( target ); // relocate in world space

	}

	// transform camera position into model coordinate system

	cameraPosition.applyMatrix4( inverseWorld );

	var label, limit;
	var splaysVisible = camera.layers.mask & 1 << LEG_SPLAY;
	var children = this.children;

	for ( var i = 0, l = children.length; i < l; i++ ) {

		label = children[ i ];

		// only show labels for splay end stations if splays visible

		if ( label.hitCount === 0 && ! splaysVisible ) {

			label.visible = false;

		} else {

			// show labels for network vertices at greater distance than intermediate stations
			limit = ( label.hitCount < 3 ) ? 5000 : 40000;
			label.visible = ( label.position.distanceToSquared( cameraPosition ) < limit );

			if ( label.visible && !label.isGlyphString ) {

				// lazy creation of GlyphStrings

				this.createLabel( label );

			}

		}

	}

};

StationLabels.prototype.createLabel = function ( dummyLabel ) {

	var station = dummyLabel.station;
	var material;

	if ( station.hitCount === 0 ) {

		material = this.splayLabelMaterial;

	} else if ( station.hitCount < 3 ) {

		material = this.defaultLabelMaterial;

	} else {

		material = this.junctionLabelMaterial;

	}

	var label = new GlyphString( station.name, material );

	label.layers.set( LABEL_STATION );

	label.position.copy( station.p );

	label.hitCount = station.hitCount;
	label.visible = false;

	this.remove( dummyLabel );
	this.add( label );

};

export { StationLabels };


@@ -20,18 +20,8 @@
 tor3
-, Object3D
  %7D f
@@ -58,16 +58,16 @@
 Three';%0A
+
 %0Aimport 
@@ -274,16 +274,465 @@
 or3();%0A%0A
+/*%0A%0Alightweight fake Object3D.%0A%0A*/%0A%0Afunction DummyStationLabel( station ) %7B%0A%0A%09this.position = station.p;%0A%09this.station = station;%0A%0A%7D%0A%0ADummyStationLabel.prototype.visible = false;%0ADummyStationLabel.prototype.isObject3D = true;%0ADummyStationLabel.prototype.parent = null;%0ADummyStationLabel.prototype.dispatchEvent = function () %7B%7D;%0ADummyStationLabel.prototype.updateMatrixWorld = function () %7B%7D;%0ADummyStationLabel.prototype.traverse = function () %7B%7D;%0A%0A
 function
@@ -1339,214 +1339,50 @@
 %7B%0A%0A%09
-var label = new Object3D();%0A%0A%09label.layers.set( LABEL_STATION );%0A%0A%09label.position.copy( station.p );%0A%0A%09label.hitCount = station.hitCount;%0A%09label.station = station;%0A%0A%09label.visible = false;%0A%0A%09this.add( label
+this.add( new DummyStationLabel( station )
  );%0A
@@ -1953,16 +1953,26 @@
 l, limit
+, hitCount
 ;%0A%09var s
@@ -2135,16 +2135,54 @@
 n%5B i %5D;%0A
+%09%09hitCount = label.station.hitCount;%0A%0A
 %0A%09%09// on
@@ -2245,22 +2245,16 @@
 %0A%09%09if ( 
-label.
 hitCount
@@ -2422,22 +2422,16 @@
 mit = ( 
-label.
 hitCount
@@ -2563,16 +2563,17 @@
 ble && !
+ 
 label.is
@@ -3093,25 +3093,24 @@
 _STATION );%0A
-%0A
 %09label.posit
@@ -3135,45 +3135,8 @@
 p );
-%0A%0A%09label.hitCount = station.hitCount;
 %0A%09la

use dummy Object3D to reduce heap