"use strict"

/**
* B is for BACON
* staalebk
*/

THREE.BaconShower = function (p1, p2, p3) {
  THREE.Object3D.call( this );

  this.type = 'BaconShower';
  this.curve = new THREE.QuadraticBezierCurve3(p1, p2, p3);

  var path = new THREE.Path( this.curve.getPoints( 50 ) );

  var geometry = path.createPointsGeometry( 50 );
  var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );

  //Create the final Object3d to add to the scene
  var curveObject = new THREE.Line( geometry, material );
  //this.add(curveObject);

  this.baconpack = new Array();

 /*
  baconpack[0] = new THREE.Bacon();


  this.axis = new THREE.Vector3();
  var up = new THREE.Vector3(1, 0, 0);
  var tangent = new THREE.Vector3();
  this.bacon.position.copy( this.curve.getPointAt(0.1) );
  tangent = this.curve.getTangentAt(0.1).normalize();
  this.axis.crossVectors( up, tangent ).normalize();
  var radians = Math.acos( up.dot( tangent ) );
  this.bacon.quaternion.setFromAxisAngle( this.axis, radians );


  this.add(this.bacon);
  */

  this.animate = function(num, pct){
    // PIG

    if(num == 0){
      for(i = 0; i < this.baconpack.length; i++){
        this.remove(this.baconpack[i]);
      }
      this.baconpack = [];
    }
    if(num > this.baconpack.length){
      // NEEDS MOAR BACON
      for(i = this.baconpack.length; i < num; i++){
        this.baconpack[i] = new THREE.Bacon();
        this.baconpack[i].randPos = new THREE.Vector3((Math.random()-0.5)*10,(Math.random()-0.8)*10,(Math.random()-0.5)*10);
        this.baconpack[i].getPosition = segmentTransition.bind(null,pct,pct+0.2);

        this.axis = new THREE.Vector3();
        var up = new THREE.Vector3(1, 0, 0);
        var tangent = new THREE.Vector3();
        this.baconpack[i].position.copy( this.curve.getPointAt(this.baconpack[i].getPosition(pct)) );
        tangent = this.curve.getTangentAt(this.baconpack[i].getPosition(pct)).normalize();
        this.axis.crossVectors( up, tangent ).normalize();
        var radians = Math.acos( up.dot( tangent ) );
        this.baconpack[i].quaternion.setFromAxisAngle( this.axis, radians );
        this.add(this.baconpack[i]);
      }
    }
    for(i= 0; i< this.baconpack.length;i++){
      var up = new THREE.Vector3(1, 0, 0);
      var tangent = new THREE.Vector3();
      this.baconpack[i].position.copy( this.curve.getPointAt(this.baconpack[i].getPosition(pct)) );
      tangent = this.curve.getTangentAt(this.baconpack[i].getPosition(pct)).normalize();
      this.axis.crossVectors( up, tangent ).normalize();
      var radians = Math.acos( up.dot( tangent ) );
      this.baconpack[i].quaternion.setFromAxisAngle( this.axis, radians );
      this.baconpack[i].position.x += this.baconpack[i].randPos.x;
      this.baconpack[i].position.y += this.baconpack[i].randPos.y;
      this.baconpack[i].position.z += this.baconpack[i].randPos.z
      if(this.baconpack[i].getPosition(pct) == 1){
        this.baconpack[i].visible = false;
      }
    }

  }
};

THREE.BaconShower.prototype = Object.create( THREE.Object3D.prototype);
THREE.BaconShower.prototype.constructor = THREE.BaconShower;
