import types from '../mutation-types'
// import { mapGetters, mapActions, mapState } from 'vuex'
// import socket from '../../scripts/socket'
import utils from '../../scripts/utils'

// initial state
const state = {
  schedules: {
    progress: {
      fileName: null,
      totalMb: 0,
      currentMb: 0,
      percentage: 0,
      speed: 0,
      currentBytes: 0,
      totalBytes: 0,
      currentFileIndex: 0,
      totalFiles: null
    },
    completed: {
      pageStatus: null,
      pageMessage: null,
      fileName: null
    },
    error: {
      message: null,
      fileName: null
    },
    listCompleted: false,
    filesSize: '',
    filesSizeDownloaded: ''
  },

  announces: {
    progress: {
      fileName: null,
      totalMb: 0,
      currentMb: 0,
      percentage: 0,
      speed: 0,
      currentBytes: 0,
      totalBytes: 0,
      currentFileIndex: 0,
      totalFiles: null
    },
    completed: {
      pageStatus: null,
      pageMessage: null,
      fileName: null
    },
    error: {
      message: null,
      fileName: null
    },
    listCompleted: false
  }
}

// getters
const getters = {
  // ...mapGetters(['options.schedules'])
}

// actions
const actions = {

}

const mutations = {

  // Schedules Progress
  [types.downloads.events.schedulesProgress] (state, data) {
    // If the download is in progress
    state.schedules.listCompleted = false

    state.schedules.progress.fileName = data.fileName
    state.schedules.progress.totalMb = data.totalMb
    state.schedules.progress.currentMb = data.currentMb
    state.schedules.progress.percentage = parseInt(data.percentage)
    state.schedules.progress.speed = data.speed
    state.schedules.progress.currentBytes = data.currentBytes
    state.schedules.progress.totalBytes = data.totalBytes
    state.schedules.progress.currentFileIndex = data.currentFileIndex
    state.schedules.progress.totalFiles = data.totalFiles
    state.schedules.filesSize = utils.bytesToSize(data.filesSize)
    state.schedules.filesSizeDownloaded = utils.bytesToSize(data.filesSizeDownloaded)
  },

  // Schedules Completed
  [types.downloads.events.schedulesCompleted] (state, data) {
    state.schedules.completed.pageStatus = data.pageStatus
    state.schedules.completed.pageMessage = data.pageMessage
    state.schedules.completed.fileName = data.fileName
  },

  // Schedules Error
  [types.downloads.events.schedulesError] (state, data) {
    state.schedules.error.message = data.message
    state.schedules.error.fileName = data.fileName
  },

  // Schedules List Completed
  [types.downloads.events.schedulesListCompleted] (state, data) {
    state.schedules.listCompleted = data
  },

  // Announces Progress
  [types.downloads.events.announcesProgress] (state, data) {
    // If the download is in progress
    state.announces.listCompleted = false

    state.announces.progress.fileName = data.fileName
    state.announces.progress.totalMb = data.totalMb
    state.announces.progress.currentMb = data.currentMb
    state.announces.progress.percentage = parseInt(data.percentage)
    state.announces.progress.speed = data.speed
    state.announces.progress.currentBytes = data.currentBytes
    state.announces.progress.totalBytes = data.totalBytes
    state.announces.progress.currentFileIndex = data.currentFileIndex
    state.announces.progress.totalFiles = data.totalFiles
    state.announces.filesSize = utils.bytesToSize(data.filesSize)
    state.announces.filesSizeDownloaded = utils.bytesToSize(data.filesSizeDownloaded)
  },

  // Announces Completed
  [types.downloads.events.announcesCompleted] (state, data) {
    state.announces.completed.pageStatus = data.pageStatus
    state.announces.completed.pageMessage = data.pageMessage
    state.announces.completed.fileName = data.fileName
  },

  // Announces Error
  [types.downloads.events.announcesError] (state, data) {
    state.announces.error.message = data.message
    state.announces.error.fileName = data.fileName
  },

  // Announces List Completed
  [types.downloads.events.announcesListCompleted] (state, data) {
    state.announces.listCompleted = data
  }

}

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