import Vue from 'vue'
import Vuex from 'vuex'
import ApiService from '@/common/services/gwells'
import {
  FETCH_CITY_LIST,
  FETCH_DRILLER,
  FETCH_DRILLER_LIST } from './actions.types.js'
import {
  SET_ERROR,
  SET_LOADING,
  SET_LIST_ERROR,
  SET_USER,
  SET_CITY_LIST,
  SET_DRILLER,
  SET_DRILLER_LIST } from './mutations.types.js'

Vue.use(Vuex)

export const store = new Vuex.Store({
  state: {
    user: null,
    loading: false,
    error: null,
    listError: null,
    cityList: [],
    drillerList: [],
    currentDriller: {}
  },
  mutations: {
    [SET_LOADING] (state, payload) {
      state.loading = payload
    },
    [SET_ERROR] (state, payload) {
      state.error = payload
    },
    [SET_LIST_ERROR] (state, payload) {
      state.listError = payload
    },
    [SET_USER] (state, payload) {
      state.user = payload
    },
    [SET_CITY_LIST] (state, payload) {
      state.cityList = payload
    },
    [SET_DRILLER] (state, payload) {
      state.currentDriller = payload
    },
    [SET_DRILLER_LIST] (state, payload) {
      state.drillerList = payload
    }
  },
  actions: {
    [FETCH_CITY_LIST] ({commit}, activity) {
      ApiService.query('cities/' + activity + '/')
        .then((response) => {
          commit(SET_CITY_LIST, response.data)
        })
        .catch((error) => {
          commit(SET_ERROR, error.response)
        })
    },
    [FETCH_DRILLER] ({commit}, guid) {
      commit(SET_LOADING, true)
      ApiService.get('drillers', guid)
        .then((response) => {
          commit(SET_LOADING, false)
          commit(SET_ERROR, null)
          commit(SET_DRILLER, response.data)
        })
        .catch((error) => {
          commit(SET_LOADING, false)
          commit(SET_ERROR, error.response)
        })
    },
    [FETCH_DRILLER_LIST] ({commit}, params) {
      commit(SET_LOADING, true)
      ApiService.query('drillers/', params)
        .then((response) => {
          commit(SET_LOADING, false)
          commit(SET_LIST_ERROR, null)
          commit(SET_DRILLER_LIST, response.data)
        })
        .catch((error) => {
          commit(SET_LOADING, false)
          commit(SET_LIST_ERROR, error.response)
        })
    }
  },
  getters: {
    loading (state) {
      return state.loading
    },
    error (state) {
      return state.error
    },
    listError (state) {
      return state.listError
    },
    user (state) {
      return state.user
    },
    cityList (state) {
      return state.cityList
    },
    drillers (state) {
      return state.drillerList
    },
    currentDriller (state) {
      return state.currentDriller
    }
  }
})

@@ -347,16 +347,38 @@
 (Vuex)%0A%0A
+const logging = true%0A%0A
 export c
@@ -850,32 +850,110 @@
 .user = payload%0A
+      logging && console.log('switched to user', payload && payload.username)%0A
     %7D,%0A    %5BSET_
@@ -1242,24 +1242,78 @@
 activity) %7B%0A
+      logging && console.log('fetching city list...')%0A
       ApiSer
@@ -1420,32 +1420,102 @@
  response.data)%0A
+          logging && console.log('fetched city list:', response.data)%0A
         %7D)%0A     
@@ -1573,32 +1573,107 @@
 error.response)%0A
+          logging && console.log('error fetching cities:', error.response)%0A
         %7D)%0A    %7D
@@ -1709,24 +1709,76 @@
 t%7D, guid) %7B%0A
+      logging && console.log('fetching driller...')%0A
       commit
@@ -1974,32 +1974,100 @@
  response.data)%0A
+          logging && console.log('fetched driller:', response.data)%0A
         %7D)%0A     
@@ -2162,32 +2162,108 @@
 error.response)%0A
+          logging && console.log('error fetching driller:', error.response)%0A
         %7D)%0A    %7D
@@ -2334,32 +2334,89 @@
 _LOADING, true)%0A
+      logging && console.log('fetching driller list...')%0A
       ApiService
@@ -2541,32 +2541,32 @@
 ST_ERROR, null)%0A
-
           commit
@@ -2591,32 +2591,105 @@
  response.data)%0A
+          logging && console.log('fetched driller list:', response.data)%0A
         %7D)%0A     

add simple logging