import types from '../mutation-types'
import socket from '../../scripts/socket'

// initial state
const state = {
  streaming: {
    url: ''
  },
  position: 0,
  vumeter: {
    left: 0,
    right: 0
  },
  isActive: false,
  supportsWebPlayer: true,
  webPlayerName: 'streaming'
}

// getters
const getters = {
}

// actions
const actions = {
  getStatus({ commit, state }) {
    socket.emit(`${types.streaming.namespace}/${types.streaming.events.getStatus}`)
  },

  getWebMediaPlayerStatus({ commit, state }) {
    socket.emit(`${types.streaming.namespace}/${types.streaming.events.getWebMediaPlayerStatus}`)
  }
}

const mutations = {

  [types.streaming.events.getWebMediaPlayerStatus](state, data) {
    // Emit to mediaPlayer.addNewPlayer
    let fileSrc = '';
    if (data.src) {
      fileSrc = data.src;
    }
    let mediaPlayerEventData= {
      key: state.webPlayerName,
      src: fileSrc,
      songName: data.songName,
      isPlaying: data.isPlaying,
      position: data.position
    }
    window.app.$emit('mediaPlayer.addNewPlayer', mediaPlayerEventData)
  },

  // Status
  [types.streaming.events.status](state, data) {
    state.isActive = data.isActive
    state.streaming.url = data.streamUrl
    state.position = data.position
  },

  // Vumeter
  [types.streaming.events.vumeter](state, data) {
    state.vumeter.left = data.left
    state.vumeter.right = data.right
  },

  // Position Change
  [types.streaming.events.positionChange](state, data) {
    state.position = data.position

    // Emit to mediaPlayer.changePosition
    let mediaPlayerEventData = {
      key: state.webPlayerName,
      position: data.position
    }
    window.app.$emit('mediaPlayer.changePosition', mediaPlayerEventData)
  },

  // File Changed
  [types.streaming.events.fileChanged](state, data) {
    state.streaming.url = data.streamUrl

    // Emit to mediaPlayer.changeSong
    let mediaPlayerEventData = {
      key: state.webPlayerName,
      src: data.streamUrl,
      songName: data.streamUrl
    }
    window.app.$emit('mediaPlayer.changeSong',mediaPlayerEventData)
  },

  // Started
  [types.streaming.events.started](state, data) {
    state.isActive = data.isPlaying
    
    let mediaPlayerEventData = {
      key: state.webPlayerName,
      isPlaying: data.isPlaying,
      src: data.streamUrl,
      songName: data.streamUrl
    }
    // Emit to mediaPlayer.changeSong
    window.app.$emit('mediaPlayer.changeSong',mediaPlayerEventData)
    // Emit to mediaPlayer.changeState
    window.app.$emit('mediaPlayer.changeState', mediaPlayerEventData);
  },

  // Finished
  [types.streaming.events.finished](state, data) {
    state.isActive = data
    state.streaming.url = null
    state.position = 0
    state.vumeter.left = 0
    state.vumeter.right = 0

    // Emit to mediaPlayer.changeState
    let mediaPlayerEventData = {
      key: state.webPlayerName,
      isPlaying: data
    }
    window.app.$emit('mediaPlayer.changeState', mediaPlayerEventData);
  },

  // URL Change
  [types.streaming.events.urlChange](state, data) {
    state.streaming.url = data.streamUrl
  }

}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
