const THREE = require("three");

export class Trajectoire {
  constructor(){
    this._list = [];
    this._score = 0;
  }

  get list(){
    return this._list;
  }

  set list(l){
    this._list = l.slice();
  }

  get score(){
    return this._score;
  }

  add(b){
    this._list.push(b);
  }

  set score(s){
    this._score = s;
  }

  removeLast(){
    return this._list.pop();
  }

  get last(){
    return this._list[this._list.length-1];
  }

  get size(){
    return this._list.length;
  }

  contains(b){
    this._list.forEach(function(bulle){
      if(bulle.id === b.id) return true;
    });
    return false;
  }

  setRGB(r,g,b){
    this._r = r;
    this._g = g;
    this._b = b;

    this._list.forEach(function(bulle){
      bulle.setRGB(r,g,b);
    });
  }

  toString(){
    let ret = "";
    this._list.forEach(function(Bulle){
      ret += Bulle.id + ' ';
    });
    return ret;
  }

  affichage(scene){

    function v(x,y,z){
            return new THREE.Vector3(x,y,z);
    }

    //Create axis (point1, point2, colour)
    function createAxis(p1, p2, color){
        var line, lineGeometry = new THREE.Geometry(),
        lineMat = new THREE.LineBasicMaterial({color: color});
        lineGeometry.vertices.push(p1, p2);
        line = new THREE.Line(lineGeometry, lineMat);
        scene.add(line);
    }

    for(let i = 0; i < this._list.length-1; i++){
      let b1 = this._list[i];
      let b2 = this._list[i+1];

      createAxis(v(b1.x, b1.y, b1.z), v(b2.x, b2.y, b2.z), rgbToHex(this._r,this._g,this._b));
    }

  }
}

function rgbToHex(r, g, b) {
    return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
