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

// initial state
const state = {
  currentSongIndex: 0,
  currentSong: {
    originalName: null,
    fileName: null
  },
  songCount: 0,
  duration: 0,
  position: 0,
  vumeter: {
    left: 0,
    right: 0
  },
  playerState: null,
  playListFinished: null,
  supportsWebPlayer: true,
  webPlayerName: 'schedules'
}

// getters
const getters = {

}

// actions
const actions = {
  nextSong({ commit, state }) {
    socket.emit(`${types.schedules.namespace}/${types.schedules.events.nextTrack}`)
    // Reset position and duration data
    state.position = 0
    state.duration = 0
  },

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

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

  [types.schedules.events.songChanged](state, data) {
    state.currentSong.originalName = data.originalName
    state.currentSong.fileName = data.fileName

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

  [types.schedules.events.positionChanged](state, data) {
    state.position = data.position
    state.duration = data.duration
    state.currentSongIndex = data.filePosition
    state.songCount = data.totalFiles

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

  [types.schedules.events.vumeterChanged](state, data) {
    state.vumeter.left = data.left
    state.vumeter.right = data.right
  },

  [types.schedules.events.stateChange](state, data) {
    switch (data) {
      case 0:
        state.playerState = 'Paused'
        break
      case 1:
        state.playerState = 'Playing'
        break
      case 2:
        state.playerState = 'Stalled'
        break
      case 3:
        state.playerState = 'Stopped'
        break
      default:
        break
    }
  },

  [types.schedules.events.playListFinished](state, data) {
    state.currentSongIndex = 0
    state.currentSong.originalName = null
    state.currentSong.fileName = null
    state.songCount = 0
    state.duration = 0
    state.position = 0
    state.vumeter.left = 0
    state.vumeter.right = 0
    state.playerState = null
    state.playListFinished = false
  }
}

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