/********
* KEGEL *
*********/

// Sirina in visina kegla
var width = 16;
var height = 128;

var radialnihSegmentov = 4;
var visinskihSegmentov = 128;
var sirinaSegmentov = 9;

function getKegelGeo() {
  const geo  = new THREE.BufferGeometry()
  const polozaji = [];

  // Najprej "spodnja buba" 
  var faktor = [
    2/sirinaSegmentov,  2/sirinaSegmentov
  ];

  // Pol rocaj (1.4 do 2.8 segmenta, dolgo 62 segmentov)
  var rocajSegmentov = 46;
  for (var i = 0; i <= rocajSegmentov; i++) {
    faktor.push((1.4 + i / rocajSegmentov * 1.4) / sirinaSegmentov);
  }

  // pol stresica dol (2.8 do 7.4 segmenta)
  var stresicaSegmentov = 48;
  for (i = 0; i <= stresicaSegmentov; i++) {
    faktor.push((2.8 + i / stresicaSegmentov * 4.6) / sirinaSegmentov);
  }

  // Pa se zadnji naklon (7.4 do 3 segmente)
  var konecSegmentov = 30;
  for (i = 0; i <= konecSegmentov; i++) {
    faktor.push((7.4 - i / konecSegmentov * 4.4) / sirinaSegmentov);
  }

  // spodnji krog
  for (var s = 0; s < radialnihSegmentov; s++) {
    polozaji.push(
      0.0,
      0.0,
      0.0,
      Math.sin(2 * Math.PI * s / radialnihSegmentov) * width * faktor[0],
      Math.cos(2 * Math.PI * s / radialnihSegmentov) * width * faktor[0],
      0.0,
      Math.sin(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[0],
      Math.cos(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[0],
      0.0
    );
  }
  // vmesni segmenti
  for (var h = 0; h < visinskihSegmentov; h++) {
    for (s = 0; s < radialnihSegmentov; s++) {
      polozaji.push(
        Math.sin(2 * Math.PI * s / radialnihSegmentov) * width * faktor[h],
        Math.cos(2 * Math.PI * s / radialnihSegmentov) * width * faktor[h],
        h * height / visinskihSegmentov,
        Math.sin(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[h],
        Math.cos(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[h],
        h * height / visinskihSegmentov,
        Math.sin(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[h],
        Math.cos(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[h],
        (h + 1) * height / visinskihSegmentov,
      );
    }
  }

  // zgornji krog
  for (s = 0; s < radialnihSegmentov; s++) {
    polozaji.push(
      0,
      0,
      height,
      Math.sin(2 * Math.PI * s / radialnihSegmentov) * width * faktor[31],
      Math.cos(2 * Math.PI * s / radialnihSegmentov) * width * faktor[31],
      height,
      Math.sin(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[31],
      Math.cos(2 * Math.PI * (s + 1) / radialnihSegmentov) * width * faktor[31],
      height
    );
  }

  for (i = 0; i < polozaji.length; i++) {
    if (i % 3 == 2) {
      polozaji[i] -= height / 2;
    }
  }


  // Spremeni v vertexe
  var vertices = new Float32Array(polozaji)
  geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3))

  return geo
}

const refKegelGeo = getKegelGeo()

function getKegel() {
  const geo = getKegelGeo();

  var barva = new THREE.Color(0, 0, 0);

  var mat = new THREE.MeshBasicMaterial({
    //color: 0xff00ff,
    color: barva,
    wireframe: true,
    transparent: true
  });
  window.mat = mat;
  //var mat = new THREE.LineBasicMaterial({ color: 0xff00ff });
  var kegel = new THREE.Mesh(geo.clone(), mat);
  kegel.barvaDodatni = new THREE.Color();
  kegel.barvaDodatni.setHSL(0.6, 1.0, 0.5);

  /*
  kegel.position.z = 20;  //  gor / dol
  kegel.position.y = -30; // levo / desno
  kegel.position.x = 12;  // levo / desno drugic

  if (IZBRAN == 1) {
    kegel.position.x -= 10;
    kegel.position.y += 8;
    kegel.position.z += 3;
  }
  */

  scene.add(kegel);
  return kegel;
}

  // Deformiranje kegla!
function deformirajKegel(obj, fft, faktor) {
  const koti = obj.geometry.attributes.position.array;
  /*
  // Random 500 zamaknemo
  for (var i = 0; i < 500; i++) {
    koti[Math.floor(Math.random() * koti.length)] += (Math.random() - 1) * deformiraj;
  }
    */

  // Deformacija po stolpickih FFT
  const kotov = koti.length / 3
  for (let k = 0; k < kotov; k += 1) {
    const zamik = Math.floor(Math.random() * 3)
    //const neg = (Math.random() > 0.5) ? 1 : -1
    const vrednost = stolpicki[Math.floor(k / kotov * fft.length) + zamik] /* * neg*/ / 30 * faktor
    const kot = koti.length - (k * 3 + zamik)
    /*
    if (k % 30 == 0) {
      console.log(stolpicki)
      console.log(kot, vrednost)
    }
    */
    koti[kot] += vrednost
  }


  // In priblizamo osnovni geometriji
  for (var i = 0; i < koti.length; i++) {
    // Tole zamika cel kegel stran @TODO
    //koti[i] = (geo.attributes.position.array[i] - koti[i]) * 0.75;
    koti[i] = (refKegelGeo.attributes.position.array[i] + koti[i]) / 2;
  }

  obj.geometry.attributes.position.needsUpdate = true;
}
