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

// initial state
const state = {
  playLists: [],
  currentList: null,
  currentSongIndex: 0,
  currentSong: {
    fileName: null,
    originalName: null
  },
  songCount: 0,
  duration: 0,
  position: 0,
  playing: false,
  announce: false,
  looping: false,
  shuffle: false,
  playingList: 0,
  playListFinished: false,
  progressBar: {
    max: 0,
    value: 0
  },
  vumeter: {
    left: 0,
    right: 0
  },
  supportsWebPlayer: true,
  webPlayerName: 'djPlayer'

}

// getters
const getters = {
  enableAnnounces: state => state.announce
}

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

  updatePlayingList({ commit, state }, playingListId) {
    state.playingList = playingListId
  },

  playList({ commit, state }, scheduleID) {
    state.playingList = scheduleID
    if(state.playing){
      socket.emit(`${types.djPlayer.namespace}/${types.djPlayer.events.stopList}`)
    }
    socket.emit(`${types.djPlayer.namespace}/${types.djPlayer.events.playList}`, scheduleID)
  },

  stopList({ commit, state }) {
    socket.emit(`${types.djPlayer.namespace}/${types.djPlayer.events.stopList}`)
    // Stop after get status
    socket.emit(`${types.djPlayer.namespace}/${types.djPlayer.events.getStatus}`)
    state.playing = false
  },

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

  changeAnnounces({ commit, state }) {
    state.announce = !state.announce
    socket.emit(`${types.djPlayer.namespace}/${types.djPlayer.events.changeAnnounce}`)
  },

  changeLooping({ commit, state }) {
    state.looping = !state.looping
    socket.emit(`${types.djPlayer.namespace}/${types.djPlayer.events.changeLooping}`)
  },

  changeShuffle({ commit, state }) {
    state.shuffle = !state.shuffle
    socket.emit(`${types.djPlayer.namespace}/${types.djPlayer.events.changeShuffling}`)
  },

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

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

const mutations = {
  [types.djPlayer.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)
  },

  // Get Songs
  [types.djPlayer.events.getLists](state, data) {
    state.playLists = data
  },

  // Get Status
  [types.djPlayer.events.getStatus](state, data) {
    state.currentSong.fileName = data.currentSong.FileName
    state.currentSong.originalName = data.currentSong.OriginalName
    state.looping = data.looping
    state.announce = data.announce
    state.shuffle = data.shuffle
    state.playing = data.playing
    state.currentList = data.currentList
    if (!data.playing) {
      state.playingList = 0
    }
  },

  // Get Playing
  [types.djPlayer.events.getPlaying](state, data) {
    state.playing = data.playing
  },

  // Song Changed
  [types.djPlayer.events.songChanged](state, data) {
    state.currentSong.fileName = data.fileName
    state.currentSong.originalName = data.originalName
    state.currentSongIndex = data.currentSongIndex
    state.songCount = data.songCount

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

  // Position Change
  [types.djPlayer.events.positionChange](state, data) {
    state.position = data.position
    state.duration = data.duration
    state.progressBar.max = data.duration
    state.progressBar.value = data.position

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

  // State Change
  [types.djPlayer.events.stateChange](state, data) {
    /*
            0 = Stop
            1 = Play
            2 = Stalled
            3 = Paused
        */

    switch (data.state) {
      case 0:
        state.playing = false
        state.playingList = 0
        state.playListFinished = false
        break
      case 1:
        state.playListFinished = false
        break
      default:
        break
    }
  },

  // Playlist Finished
  [types.djPlayer.events.playListFinished](state, data) {
    state.playListFinished = true
  },

  // Refresh Playlist
  [types.djPlayer.events.refreshPlaylist](state, data) {
    state.playLists = data.playLists

  },

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

  // Started
  [types.djPlayer.events.started](state, data) {
    // state.playing = data
    // Emit to mediaPlayer.changeState
    let mediaPlayerEventData ={
      key: state.webPlayerName,
      isPlaying: data.playing
    }
    window.app.$emit('mediaPlayer.changeState', mediaPlayerEventData)
  },

  // Stopped
  [types.djPlayer.events.stopped](state, data) {
    // state.playing = data
    // Emit to mediaPlayer.changeState
    let mediaPlayerEventData = {
      key: state.webPlayerName,
      isPlaying: data.playing
    }
    window.app.$emit('mediaPlayer.changeState', mediaPlayerEventData)
  }

}

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