import { getWorkspace, saveWorkspace, runFilter } from '@/services/storage.service'

export const state = () => {
  return {
    states: [],
    jobs: [],
    sources: [],
    documents: [],
    reports: [],
    user: {},
    filters: [],
    notificationTime: '0',
    filterResults: []
  }
}

const ADD_STATE = 'addState'
const UPDATE_BRANCH = 'updateBranch'
const SET_WORKSPACE = 'setWorkspace'
const SET_PRIMARY_STATE = 'setActiveState'
const ADD_FILTER = 'addFilter'
const SET_FILTER_RESULTS = 'setFilterResults'

export const actions = {
  async fetchWorkspace({ commit }) {
    const workspace = await getWorkspace()
    commit(SET_WORKSPACE, workspace)
  },

  async saveWorkspace({ state, commit, rootState }) {
    // const newStates = Object.values(rootState.data).map((value, index) => ({
    //   id: state.states + index,
    //   ...value
    // }))
    const primaryWorkspace = state.user.workspace.mainCommitId
    const newStates = [
      {
        id: primaryWorkspace + 1,
        ...rootState.data[primaryWorkspace]
      }
    ]
    commit(SET_PRIMARY_STATE, primaryWorkspace + 1)

    /**
     * @todo Specify things to save in workspace
     */
    await saveWorkspace({ ...state, states: [...state.states, ...newStates] })
  },

  async createFilter({ commit }, filter) {
    commit(ADD_FILTER, filter)
  },

  async runFilter({ commit }, filter) {
    const results = await runFilter(filter)
    commit(SET_FILTER_RESULTS, results)
  },

  setNotificationTime({ commit }, pl) {
    commit('SET_NOTIFICATION_TIME', {
      //slotId: pl.slotId,
      timestamp: pl.timestamp
    })
  }
}

export const mutations = {
  [ADD_STATE](state, visualizationState) {
    state.states.push(visualizationState)
  },

  [SET_PRIMARY_STATE](state, id) {
    state.user.workspace.mainCommitId = id
  },

  [SET_WORKSPACE](state, workspace) {
    state.documents = workspace.documents
    state.jobs = workspace.jobs
    state.reports = workspace.reports
    state.states = workspace.states
    state.user = workspace.user
  },

  [UPDATE_BRANCH](state, branch) {
    state.branches = [...state.branches.filter(b => b.id !== branch.id), branch]
  },

  /**
   * Sets new notification time
   *
   * @param {object} pl payload
   * @param {number}   pl.slotId dataset slot
   * @param {String}   pl.timestamp
   */
  SET_NOTIFICATION_TIME(state, pl) {
    state.notificationTime = pl.timestamp
    //Vue.set(state[pl.slotId], 'notificationTime', pl.timestamp)
  },

  [ADD_FILTER](state, filter) {
    state.filters.push(filter)
  },

  [SET_FILTER_RESULTS](state, results) {
    state.filterResults = results
  }
}

export const getters = {
  getBranchById: state => branchId => {
    return state.branches.find(b => b.id === branchId)
  },

  getCommitById: state => commitId => {
    return state.commits.find(c => c.id === commitId)
  },

  getStateById: state => id => {
    return state.states.find(c => c.id === id)
  },

  // getNotificationTime: state => slotId => state[slotId].notificationTime
  getNotificationTime: state => state.notificationTime
}
