// import * as api from '../api'
import * as types from './mutation-types'
import firebase from 'firebase'
import { firebaseAction } from 'vuexfire'
import { db } from '../firebase'

export const setNotesRef = firebaseAction(({ bindFirebaseRef }, { ref }) => {
  bindFirebaseRef('notes', ref)
})

export const setNotesOrderRef = firebaseAction(({ bindFirebaseRef }, { ref }) => {
  bindFirebaseRef('notesOrder', ref)
})

export const createNote = ({ commit }, note) => {
  db.ref('notes').push({
    title: note.title.trim(),
    text: note.text.trim(),
    markdown: note.markdown,
    color: note.color,
    created_at: firebase.database.ServerValue.TIMESTAMP
  }, () => {
    console.log('Note Created!')
    commit(types.CLEAR_NOTE_FORM)
    // reset newNote using some mutation
  })
}

export const updateNote = ({ commit }, note) => {
  db.ref('notes').child(note['.key']).update({
    title: note.title.trim(),
    text: note.text.trim(),
    markdown: note.markdown,
    color: note.color
  }, () => {
    console.log('Note Updated!')
  })
}

export const removeNote = ({ commit }, note) => {
  db.ref('notes').child(note['.key']).remove()
    .then(() => {
      console.log('Note Removed!')
    })
    .catch((error) => {
      console.error('Note Remove Failed', error.message)
    })
}

export const updateNoteColor = ({ commit }, note) => {
  db.ref('notes').child(note['.key']).update({
    color: note.color
  }, () => {
    console.log('Note Color Updated!')
  })
}

export const updateNotesOrder = ({ commit }, order) => {
  db.ref('notesOrder').update(order, () => {
    console.log('update order success')
  })
}

// export const getAllMessages = ({ commit }) => {
//   api.getAllMessages(messages => {
//     commit(types.RECEIVE_ALL, {
//       messages
//     })
//   })
// }

// export const sendMessage = ({ commit }, payload) => {
//   api.createMessage(payload, message => {
//     commit(types.RECEIVE_MESSAGE, {
//       message
//     })
//   })
// }

// export const switchThread = ({ commit }, payload) => {
//   commit(types.SWITCH_THREAD, payload)
// }

@@ -1,37 +1,4 @@
-// import * as api from '../api'%0A
 impo
@@ -117,16 +117,29 @@
 import %7B
+ firebaseApp,
  db %7D fr
@@ -382,36 +382,295 @@
 tesOrder', ref)%0A
-
 %7D)%0A%0A
+export const signInWithEmailAndPassword = (%7B commit %7D, payload) =%3E %7B%0A  return new Promise((resolve, reject) =%3E %7B%0A    firebaseApp.auth().signInWithEmailAndPassword(payload.email, payload.password)%0A      .catch(error =%3E %7B%0A        reject(error)%0A      %7D)%0A  %7D)%0A%7D%0A%0A
 export const cre
@@ -1868,458 +1868,4 @@
 )%0A%7D%0A
-%0A// export const getAllMessages = (%7B commit %7D) =%3E %7B%0A//   api.getAllMessages(messages =%3E %7B%0A//     commit(types.RECEIVE_ALL, %7B%0A//       messages%0A//     %7D)%0A//   %7D)%0A// %7D%0A%0A// export const sendMessage = (%7B commit %7D, payload) =%3E %7B%0A//   api.createMessage(payload, message =%3E %7B%0A//     commit(types.RECEIVE_MESSAGE, %7B%0A//       message%0A//     %7D)%0A//   %7D)%0A// %7D%0A%0A// export const switchThread = (%7B commit %7D, payload) =%3E %7B%0A//   commit(types.SWITCH_THREAD, payload)%0A// %7D%0A

Add sign in action