const state = {
  admin: false,
  roomStatus: {
    roomName: '',
    roomCode: '',
    player: {
      timeCode: 0,
      timeLength: 0,
      playing: true
    },
    current: {
      link: '',
      linkID: '',
      title: '',
      votes: 0,
      voters: []
    },
    playlist: []
  },
  roomSettings: {
    userControl: false,
    userLink: true
  },
  localSettings: {
    playLink: false,
    externalSearch: false
  },
  lastUserCommand: null
}

const getters = {
}

const actions = {
  setRoomCode ({ commit }, roomCode) {
    commit('SET_ROOMCODE', roomCode)
  },
  setRoomName ({ commit }, roomName) {
    commit('SET_ROOMNAME', roomName)
  },
  setRoomStatus ({ commit }, roomStatus) {
    commit('SET_ROOMSTATUS', roomStatus)
  },
  setUserCommand ({ commit }, command) {
    commit('SET_USERCOMMAND', command)
  },
  setRoomSettings ({ commit, dispatch, state }, roomSettings) {
    commit('SET_ROOMSETTINGS', roomSettings)
    if (state.admin) dispatch('rtc/broadcast', { message: state.roomSettings, type: 'settings' }, { root: true })
  },
  setLocalSettings ({ commit }, localSettings) {
    commit('SET_LOCALSETTINGS', localSettings)
  },
  setAdmin ({ commit }) {
    commit('SET_ADMIN')
  },
  setTimeCode ({ commit }, timeCode) {
    commit('SET_TIMECODE', timeCode)
  },
  vote ({ commit, dispatch, state }, { title, link, linkID, isPositive, voterName }) {
    console.log('vote on ' + link + ' | ' + linkID + ' (' + isPositive + ') by ' + voterName)
    if (isPositive) {
      commit('ADD_VOTE', {
        title: title,
        linkID: linkID,
        link: link,
        voterName: voterName
      })
    } else {
      commit('REMOVE_VOTE', {
        linkID: linkID,
        voterName: voterName
      })
    }
    dispatch('rtc/broadcast', { message: state.roomStatus, type: 'status' }, { root: true })
  },
  removePlay ({ commit, dispatch, state }, linkID) {
    commit('REMOVE_PLAY', linkID)
    dispatch('rtc/broadcast', { message: state.roomStatus, type: 'status' }, { root: true })
  },
  setCurrent ({ commit, dispatch }, { playerStatus, timeCode, timeLength, title }) {
    switch (playerStatus) {
      case 0:
        commit('CURRENT_END')
        break
      case 1:
        commit('CURRENT_PLAY', timeCode)
        commit('SET_CURRENTTITLE', title)
        commit('SET_TIMELENGTH', timeLength)
        break
      case 2:
        commit('CURRENT_PAUSE', timeCode)
        break
      default:
        return
    }
    dispatch('rtc/broadcast', { message: state.roomStatus, type: 'status' }, { root: true })
  },
  leave ({ commit, dispatch }) {
    dispatch('rtc/leave', null, { root: true })
    commit('SET_ROOMSTATUS', {
      roomName: '',
      roomCode: '',
      player: {
        timeCode: 0,
        timeLength: 0,
        playing: true
      },
      current: {
        link: '',
        linkID: '',
        title: '',
        votes: 0,
        voters: []
      },
      playlist: []
    })
  }
}

const mutations = {
  SET_ROOMCODE (state, code) {
    state.roomStatus.roomCode = code
  },
  SET_ROOMNAME (state, name) {
    state.roomStatus.roomName = name
  },
  SET_ROOMSTATUS (state, roomStatus) {
    state.roomStatus = roomStatus
  },
  SET_ROOMSETTINGS (state, roomSettings) {
    state.roomSettings = roomSettings
  },
  SET_LOCALSETTINGS (state, localSettings) {
    state.localSettings = localSettings
  },
  SET_ADMIN (state) {
    state.admin = true
  },
  SET_TIMECODE (state, timeCode) {
    state.roomStatus.player.timeCode = timeCode
  },
  SET_TIMELENGTH (state, timeLength) {
    state.roomStatus.player.timeLength = timeLength
  },
  SET_CURRENTTITLE (state, title) {
    state.roomStatus.current.title = title
  },
  SET_USERCOMMAND (state, command) {
    state.lastUserCommand = command
  },
  ADD_VOTE (state, { title, link, linkID, voterName }) {
    var play = state.roomStatus.playlist.find(play => play.linkID === linkID)
    if (play === undefined) {
      play = {
        title: title,
        link: link,
        linkID: linkID,
        votes: 1,
        voters: [
          voterName
        ]
      }
      if (state.roomStatus.current.votes === 0) state.roomStatus.current = play
      else state.roomStatus.playlist.push(play)
    } else {
      play.votes++
      play.voters.push(voterName)
    }

    state.roomStatus.playlist.sort((a, b) => {
      return b.votes - a.votes
    })
  },
  REMOVE_VOTE (state, { linkID, voterName }) {
    var play = state.roomStatus.playlist.find(play => play.linkID === linkID)
    play.votes--
    const index = play.voters.indexOf(voterName)
    if (index > -1) {
      play.voters.splice(index, 1)
    }

    if (play.votes === 0) {
      const index = state.roomStatus.playlist.indexOf(play)
      if (index > -1) {
        state.roomStatus.playlist.splice(index, 1)
      }
    }

    state.roomStatus.playlist.sort((a, b) => {
      return b.votes - a.votes
    })
  },
  REMOVE_PLAY (state, linkID) {
    var play = state.roomStatus.playlist.find(play => play.linkID === linkID)
    const index = state.roomStatus.playlist.indexOf(play)
    if (index > -1) {
      state.roomStatus.playlist.splice(index, 1)
    }
  },
  CURRENT_END (state) {
    if (state.roomStatus.playlist.length === 0) {
      state.roomStatus.current = {
        link: '',
        linkID: '',
        title: '',
        votes: 0,
        voters: []
      }
    } else {
      state.roomStatus.playlist.sort((a, b) => {
        return b.votes - a.votes
      })
      state.roomStatus.current = state.roomStatus.playlist.shift()
    }
  },
  CURRENT_PAUSE (state, timeCode) {
    state.roomStatus.player.playing = false
    state.roomStatus.player.timeCode = timeCode
  },
  CURRENT_PLAY (state, timeCode) {
    state.roomStatus.player.playing = true
    state.roomStatus.player.timeCode = timeCode
  }
}

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