import moment from 'moment';
import getServerList from './../../utils/getServerList';

function getInitialState() {
  return {
    time: undefined,
    servers: getServerList().map(server => Object.assign(server, {state: 'loading', delay: 0}))
  };
}

export default function serverReducer(state = getInitialState(), action) {
  switch (action.type) {
    case "server.current.time":
      return setCurrentTime(state);
    case "server.online":
      return modifyServerState(state, action, 'online');
    case "server.offline":
      return modifyServerState(state, action, 'offline');
    default:
      return state;
  }
}

function setCurrentTime(state) {
  return {
    ...state,
    time: moment().unix()
  };
}

function modifyServerState(state, {server, responseTime}, newStateValue) {
  return {
    ...state,
    servers: state.servers.map(server => {
      if (server.name === server.name) {
        server.state = newStateValue;
        server.delay = moment().unix() - state.time;
      }
      return server;
    })
  };
}

@@ -841,24 +841,29 @@
 ervers.map(s
+tateS
 erver =%3E %7B%0A 
@@ -888,16 +888,21 @@
 me === s
+tateS
 erver.na
@@ -918,23 +918,60 @@
     
+ 
- server.
+return %7B%0A          ...stateServer,%0A          
 state
- =
+:
  new
@@ -980,17 +980,17 @@
 ateValue
-;
+,
 %0A       
@@ -994,22 +994,16 @@
     
-server.
+  
 delay
- =
+:
  mom
@@ -1027,16 +1027,26 @@
 ate.time
+%0A        %7D
 ;%0A      
@@ -1061,16 +1061,21 @@
 return s
+tateS
 erver;%0A 

Fix a bug in the reducer