import io from 'socket.io-client'
import dotenv from 'dotenv'
import winston from 'winston'
import path from 'path'

import Presence from './modules/presence'
import music from './modules/music'

if (process.env.NODE_ENV !== 'production') {
    dotenv.config()
}

let presence = new Presence({
    interval: 5 * 1000
})

let socket = io(process.env.SERVER_URL, {
    reconnection: true
})

socket.on('connect', () => {
    winston.info('connected', { host: process.env.SERVER_URL })
    socket.emit('hello', {
        token: process.env.HOME_SPARK_TOKEN
    })

    socket.on('set-behavior', (behaviors) => {
        for (let behavior of behaviors) {
            if (behavior.type === 'watch-presence') {
                presence.addDevice({
                    device: behavior.device,
                    ip: behavior.device.ip,
                    isPresent: null
                })
                // console.log(behavior)
            }
        }
        winston.info('behavior-ready', { behaviors: behaviors })
    })

    socket.on('start-music', ({ speed }) => {
      winston.info('start-music')
      music.setVolume(0)
      if (speed !== 'none') {
        let volume = 0
        let intervalTimeout = 100
        let pitch = 1
        if (speed === 'fast') {
          intervalTimeout = 50
        } else if (speed === 'medium') {
          intervalTimeout = 150
        } else if (speed === 'slow') {
          intervalTimeout = 250
        }
        let interval = setInterval(() => {
          volume += pitch
          music.setVolume(volume)
          winston.info('setVolume', volume)
          if (volume >= 100) {
            clearInterval(interval)
          }
        }, intervalTimeout)
      }
      music.play(path.join(__dirname, '../', process.env.MUSIC))
    })

    socket.on('stop-music', ({ speed }) => {
      winston.info('stop-music')
      music.stop()
    })

    socket.on('volume-up', () => {
      winston.info('volume-up')
      music.setVolume(music.getVolume() + 20)
    })

    socket.on('volume-down', () => {
      winston.info('volume-down')
      music.setVolume(music.getVolume() - 20)
    })
})
/*
presence.watch()

presence.on('device-presence-changed', ({ device, previous }) => {

    socket.emit('behavior-result', {
        token: process.env.HOME_SPARK_TOKEN,
        type: 'watch-presence',
        payload: {
            device: device.device,
            currentState: device.isPresent,
            previousState: previous
        }
    })
    winston.info('device-presence-changed', { name: device.name, previous: previous, new:device.isPresent })
})*/

@@ -1122,16 +1122,81 @@
 lume(0)%0A
+      music.play(path.join(__dirname, '../', process.env.MUSIC))%0A
       if
@@ -1213,24 +1213,24 @@
 = 'none') %7B%0A
-
         let 
@@ -1505,32 +1505,33 @@
 = 250%0A        %7D%0A
+%0A
         let inte
@@ -1685,18 +1685,17 @@
 lume %3E= 
-10
+9
 0) %7B%0A   
@@ -1779,73 +1779,8 @@
   %7D%0A
-      music.play(path.join(__dirname, '../', process.env.MUSIC))%0A
     
@@ -1997,18 +1997,17 @@
 ume() + 
-20
+5
 )%0A    %7D)
@@ -2125,10 +2125,9 @@
 ) - 
-20
+5
 )%0A  

change order start music