const oscCallbacks = {}

// Quaternioni za rotacijo in kalibracijo
let calibrate = true
const qWW = []
const qPrej = []
const qObj = []
const qStart = []

// Parametri rotacije (euler)
const rotacija = []

// Parametri pospeska
const acc = []

// "Napihnuj" kegle? (ko zogice padajo)
let napihuj = 1

// Fraktaliziraj (kegel znotraj kegla manjsanje)
let fraktaliziraj = 0
let fraktal_meje = [
  new THREE.Vector3(0.1, 0.1, 0.1),
  new THREE.Vector3(10, 10, 10)
]
let fraktal_sprememba = new THREE.Vector3(0.98, 0.98, 0.98)
let fraktal_interval = 1200
let fraktal_stevec = 0
const fraktal_limit = 100

for (let i = 1; i <= ST_KEGLOV; i++) {
  qWW[i] = new THREE.Quaternion()
  qPrej[i] = new THREE.Quaternion()
  qObj[i] = new THREE.Quaternion()
  qStart[i] = new THREE.Quaternion()

  rotacija[i] = new THREE.Vector3()
  acc[i] = new THREE.Vector3()
}



// rotacije in pospeski keglov
for (let i = 1; i <= ST_KEGLOV; i++) {
  oscCallbacks[`${PREPEND}${i}/quaternion`] = function (args) {
    // Popravimo osi (w x y z po defaultu HMM)
    qPrej[i].copy(qWW[i])
    const vals = args.map(getVal)
    qWW[i].set(vals[0], vals[1], vals[2], vals[3])
  }
  oscCallbacks[`${PREPEND}${i}/accel`] = function (args) {
    [acc[i].x, acc[i].y, acc[i].z] = args.map(getVal)
    //console.log('ACC', i, acc[i])
  }
  oscCallbacks[`${PREPEND}${i}/gyro`] = function (args) {
    [rotacija[i].x, rotacija[i].y, rotacija[i].z] = args.map(getVal)
  }
}



// Parametri animacije
oscCallbacks['/ww/calibrate'] = function () {
  calibrate = true
}
oscCallbacks['/ww/reload'] = function () {
  window.location.reload()
}
oscCallbacks['/ww/zoom'] = args => {
  const [kolicina] = args.map(getVal)
  spremeniZoom(kolicina)
}
oscCallbacks['/ww/deformiraj'] = args => {
  const [kolicina] = args.map(getVal)
  spremeniDeformiraj(kolicina)
}
oscCallbacks['/ww/kegli'] = args => {
  const [ali] = args.map(getVal)
  dodatniKegli = ali
}
oscCallbacks['/ww/zogice'] = args => {
  const [ali] = args.map(getVal)
  zogice = ali
}
oscCallbacks['/ww/fraktaliziraj'] = args => {
  const [ali] = args.map(getVal)
  if (fraktaliziraj !== ali) {
    fraktalTranzicija(ali)
  }
  fraktaliziraj = ali
}
oscCallbacks['/ww/napihuj'] = args => {
  const [ali] = args.map(getVal)
  napihuj = ali
}
oscCallbacks['/ww/fraktal-interval'] = args => {
  const [faktor] = args.map(getVal)
  fraktal_interval = faktor
}



// Poslusaj OSC evente
const getVal = function (msg) {
  return msg.value;
}

function spremeniDeformiraj (kolicina) {
  /*
  const noviDeformiraj = deformiraj + (kolicina / 2)
  //console.log('deform', noviDeformiraj)
  if (noviDeformiraj > 0) {
    deformiraj = noviDeformiraj
  } else {
    deformiraj = 0
  }
*/
  deformiraj = kolicina
}

function spremeniZoom (kolicina) {
  /*
  const noviZum = camera.position.z * (1 + kolicina / 25)
  //console.log(noviZum)
  if ((noviZum > 0.1) && (noviZum < 300)) {
    camera.position.z = noviZum
  }
  */
  camera.position.z = kolicina
}



// Zogice
function zogicaCB(args) {
  console.log("MAMOMO MIDI!", args[0].value, args[1].value, args[2].value);
  var minus = (IZBRAN == 1) ? -1 : 1
  // kegel.material.color.offsetHSL(minus * args[2].value / 1000, 0, 0);
  objekti[IZBRAN].barvaDodatni.offsetHSL(minus * args[2].value / 1000, 0, 0)

  /*
  if (Math.random() < 0.5) {
    kegel.scale.x *= 2;
  } else {
    kegel.scale.z *= 2;
  }
  */
    
  //kegel.scale.y *= 1 + (args[2] / 100000);
  //kegel.scale.z *= 1 + (args[2] / 100000);

  if (zogice) {
    //console.log('nova krogla!')
    novaKrogla();
    var bId = IZBRAN == 0 ? 2 : 3
    // console.log('id barve krogle:', bId)
    barvnePaleteIdx[bId] = (barvnePaleteIdx[bId] + 1) % barvnePalete[bId].length;
  }

  napihni = 3
}

oscCallbacks['/midi-in/0'] = zogicaCB
oscCallbacks['/midi-in/1'] = zogicaCB
oscCallbacks['/midi-in/2'] = zogicaCB
