import moment from 'moment'
import sortBy from 'lodash/sortBy'

const initialState = {
  messages: [],
  savedMessages: [],
  filter: 'all'
}

const toggleSavedMessage = (message) => {
  return {
    type: 'toggle-saved-message',
    message
  }
}

const setMessageFilter = (value) => {
  return {
    type: 'set-message-filter',
    value
  }
}

const getVisibleMessages = (state, filter) => {
  switch(filter) {
    case 'successful':
      return sortBy(state.messages.filter(m => m.hasError === false), 'timestamp')
    case 'failed':
      return sortBy(state.messages.filter(m => m.hasError === true), 'timestamp')
    case 'saved':
      return sortBy(state.savedMessages, 'timestamp')
    default:
      return sortBy(state.messages, 'timestamp')
  }
}

export {
  getVisibleMessages,
  setMessageFilter,
  toggleSavedMessage
}

const insert = (array, index, ...items) => {
  return [
    ...array.slice(0, index),
    ...items,
    ...array.slice(index)
  ]
}

export default (state = initialState, action = {}) => {
  switch(action.type) {
    case 'bus-message-succeeded':
      action.envelope.saved = false
      action.envelope.timestamp = moment().format()
      return Object.assign({}, state, {
        messages: insert(state.messages, 0, action.envelope)
      })
    case 'bus-message-failed':
      action.envelope.saved = false
      action.envelope.timestamp = moment().format()
      return Object.assign({}, state, {
        messages: insert(state.messages, 0, action.envelope)
      })
    case 'set-message-filter':
      return Object.assign({}, state, {
        filter: action.value
      })
    case 'toggle-saved-message': {
      const wasSaved = action.message.saved
      action.message.saved = !action.message.saved

      if (!wasSaved) {
        return Object.assign({}, state, {
          messages: state.messages.filter(m => m.correlationId !== action.message.correlationId).concat([action.message]),
          savedMessages: insert(state.savedMessages, 0, action.message)
        })
      } else {
        return Object.assign({}, state, {
          messages: state.messages.filter(m => m.correlationId !== action.message.correlationId).concat([action.message]),
          savedMessages: state.savedMessages.filter(m => m.headers.id !== action.message.headers.id)
        })
      }
    }
    default: return state
  }
}

@@ -1182,34 +1182,27 @@
 urn 
-Object.assign(%7B%7D, 
+%7B%0A        ...
 state,
- %7B
 %0A   
@@ -1258,33 +1258,32 @@
 nvelope)%0A      %7D
-)
 %0A    case 'bus-m
@@ -1403,34 +1403,27 @@
 urn 
-Object.assign(%7B%7D, 
+%7B%0A        ...
 state,
- %7B
 %0A   
@@ -1479,33 +1479,32 @@
 nvelope)%0A      %7D
-)
 %0A    case 'set-m
@@ -1536,34 +1536,27 @@
 urn 
-Object.assign(%7B%7D, 
+%7B%0A        ...
 state,
- %7B
 %0A   
@@ -1584,25 +1584,24 @@
 alue%0A      %7D
-)
 %0A    case 't
@@ -1640,19 +1640,16 @@
 nst 
-wasS
+s
 ave
-d
  = 
+!
 acti
@@ -1669,124 +1669,72 @@
 ved%0A
+%0A
       
-action.message.
+if (
 save
-d
  =
- !action.message.saved%0A%0A      if (!wasSaved) %7B%0A        return Object.assign(%7B%7D, 
+== true) %7B%0A        return %7B%0A          ...
 state,
- %7B
 %0A   
@@ -1757,38 +1757,35 @@
  state.messages.
-filter
+map
 (m =%3E m.correlat
@@ -1782,33 +1782,32 @@
 m.correlationId 
-!
 == action.messag
@@ -1825,33 +1825,70 @@
 onId
-).concat(%5Baction.message%5D
+ ?%0A            %7B ...m, saved: save%7D :%0A            m%0A          
 ),%0A 
@@ -1907,31 +1907,41 @@
 edMessages: 
-insert(
+%5B%0A            ...
 state.savedM
@@ -1951,28 +1951,130 @@
 ages
-, 0, action.message)
+.filter(m =%3E m.correlationId !== action.message.correlationId),%0A            %7B ...action.message, saved: save %7D%0A          %5D
 %0A   
@@ -2071,33 +2071,32 @@
      %5D%0A        %7D
-)
 %0A      %7D else %7B%0A
@@ -2114,34 +2114,29 @@
 urn 
-Object.assign(%7B%7D, 
+%7B%0A          ...
 state,
- %7B
 %0A   
@@ -2159,38 +2159,35 @@
  state.messages.
-filter
+map
 (m =%3E m.correlat
@@ -2184,33 +2184,32 @@
 m.correlationId 
-!
 == action.messag
@@ -2227,33 +2227,70 @@
 onId
-).concat(%5Baction.message%5D
+ ?%0A            %7B ...m, saved: save%7D :%0A            m%0A          
 ),%0A 
@@ -2309,24 +2309,41 @@
 edMessages: 
+%5B%0A            ...
 state.savedM
@@ -2364,25 +2364,28 @@
 (m =%3E m.
-headers.i
+correlationI
 d !== ac
@@ -2401,30 +2401,44 @@
 age.
-headers.id)%0A        %7D)
+correlationId)%0A          %5D%0A        %7D
 %0A   

Fix redux store for starred messages