const WebSocket = require('ws')
const osc = require('osc')

const wwIP = process.argv[2]
const bwIP = process.argv[3]

const DEBUG = {
  osc: true
}

const oscWS = new osc.WebSocketPort({
  url: `ws://${wwIP}:6676`,
  metadata: false
})

const bwPortUkazi = 8000
const bwPortStanje = 9000

const oscBW = new osc.UDPPort({
  localAddress: bwIP,
  remotePort: bwPortUkazi,
  localPort: bwPortStanje,
  metadata: false
})

oscWS.on('ready', () => {
  console.log(`Povezan sem na WW ${wwIP}`)
  /*
  setInterval(() => {
    console.log('ping')
    oscWS.send({ address: '/ping', args: [true]})
  }, 500)
  */
})

oscBW.on('ready', () => {
  console.log(`Povezan sem na bitwig ${bwIP}`)
})
oscWSstanje.on('error', () => console.log('napaka stanja', arguments))

const STANJE = {
  predvajam: 0,
  track: 1,
  clip: 1,
  trackCount: 2,
  trackAbs: 1,
}

oscWS.on('message', ({ address, args }) => {
  DEBUG.osc && console.log('MSG OSC', address, args.map(x => x.toFixed(4)))

  // Wavey Wind sporočila
  const re = /\/ww\/(\d+)\/([a-zA-Z]+)/
  const m = address.match(re)

  if (m) {
    console.log(address, m)
    let bwUkaz = '/' + address.split('/').splice(3).join('/')

    if (STANJE.predvajam && bwUkaz == '/play') {
      bwUkaz = '/stop'
      oscBW.send({ address: bwUkaz })
    } else {
      oscBW.send({ address: bwUkaz })
    }

    switch (bwUkaz) {
    case '/play':
      if (STANJE.predvajam) {
        bwUkaz = '/stop'
        oscBW.send({ address: bwUkaz })
      } else {
        oscBW.send({ address: bwUkaz })
      }
      break
    case '/track/next':
      console.log("NASLEDNJI TRACK", STANJE.track+1)
      var novTrack = STANJE.track + 1
      if (novTrack > 8) {
        bwUkaz = `/track/bank/+`
        oscBW.send({ address: bwUkaz })
        setTimeout(() => {
          bwUkaz = `/track/${STANJE.track}/select`
          oscBW.send({ address: bwUkaz })
          bwUkaz = `/track/${STANJE.track}/clip/${STANJE.clip}/select`
          oscBW.send({ address: bwUkaz })
        }, 50)
      } else {
        bwUkaz = `/track/${novTrack}/select`
        oscBW.send({ address: bwUkaz })
        bwUkaz = `/track/${novTrack}/clip/${STANJE.clip}/select`
        oscBW.send({ address: bwUkaz })
      }
      break
    case '/track/previous':
      console.log("PREJSNJI TRACK", STANJE.track-1)
      var novTrack = STANJE.track - 1
      if (novTrack >= 1) {
        bwUkaz = `/track/${novTrack}/select`
        console.log(bwUkaz)
        oscBW.send({ address: bwUkaz })
        bwUkaz = `/track/${novTrack}/clip/${STANJE.clip}/select`
        oscBW.send({ address: bwUkaz })
        console.log(bwUkaz)
      } else {
        bwUkaz = `/track/bank/-`
        oscBW.send({ address: bwUkaz })
        setTimeout(() => {
          bwUkaz = `/track/${STANJE.track}/select`
          oscBW.send({ address: bwUkaz })
          bwUkaz = `/track/${STANJE.track}/clip/${STANJE.clip}/select`
          oscBW.send({ address: bwUkaz })
        }, 50)
      }
      break
    case '/clip/next':
      var novClip = STANJE.clip + 1
      console.log("NASLEDNJI CLIP", novClip)
      if (novClip > 8) {
      } else {
        bwUkaz = `/track/${STANJE.track}/clip/${novClip}/select`
        console.log("POSILJAM", bwUkaz)
        oscBW.send({ address: bwUkaz })
      }
      break
    case '/clip/previous':
      var novClip = STANJE.clip - 1
      console.log("PREJSNJI CLIP", novClip)
      if (STANJE.clip - 1 >= 1) {
        bwUkaz = `/track/${STANJE.track}/clip/${novClip}/select`
        oscBW.send({ address: bwUkaz })
      } else {
      }
      break
    }
  }
})
 
oscBW.on('message', ({ address, args }) => {
  DEBUG.osc && console.log('BITWIG OSC', address, args)
  if (address == '/play') {
    console.log('MENJAVA STANJA PLAY', args)
    STANJE.predvajam = args[0]
  }
  let re = /\/track\/(\d+)\/clip\/(\d+)\/isSelected/
  let m = address.match(re)
  if (m) {
    if (args[0]) {
      STANJE.track = parseInt(m[1])
      STANJE.clip = parseInt(m[2])
      console.log('MENJAVA STANJA TRACKA', STANJE.track, STANJE.clip)
      console.log(address, args)
    }
  }

  re = /\/master\/position/
  m = address.match(re)
  if (m) {
    STANJE.trackCount = args[0] - 1
    console.log("STEVILO TRACKOV", STANJE.trackCount)
  }

/*
  re = /\/track\/selected\/clip\/(\d+)\/isSelected/
  m = address.match(re)
  if (m) {
    if (args[0]) {
      STANJE.clip = parseInt(m[1])
      console.log('MENJAVA STANJA CLIPA', STANJE.clip, m)
    }
    console.log(address, args)
  }
  */
})

oscWS.open()
oscBW.open()
