import T from "../constants/ActionTypes";

const initialState = {
  zoom: 13,
  center: {
    lat: 48.7784931,
    lng: 9.1800456
  },
  marker: null,
  bbox: null,
  ownPosition: null,
  ownPositionCancelled: false
};

module.exports = (state = initialState, action = {}) => {

  var newState, point;
  switch (action.type) {
    case T.CLOSE_NEW_ENTRY:
    case T.SHOW_NEW_ENTRY:
      return {
        ...state,
        marker: null
      };
    case T.SET_MARKER:
      if (action.manual) {
        return {
          ...state,
          marker: action.payload
        };
      } else {
        return {
          ...state,
          marker: action.payload,
          center: action.payload
        };
      }
      break;
    case T.EDIT_CURRENT_ENTRY:
      point = {
        lat: action.payload.lat,
        lng: action.payload.lng
      };
      return {
        ...state,
        marker: point,
        center: point
      };
    case T.SET_MAP_CENTER:
      return {
        ...state,
        center: action.payload
      };
    case T.SET_ZOOM:
      return {
        ...state,
        zoom: action.payload
      };
    case T.SET_BBOX:
      return {
        ...state,
        bbox: action.payload
      };
    case T.NEW_ENTRY_RESULT:
      if (!action.error) {
        return {
          ...state,
          marker: null
        };
      } else {
        return state;
      }
      break;
    case T.SHOW_OWN_POSITION:
      return {
        ...state,
        ownPositionCancelled: false
      };
    case T.CANCEL_OWN_POSITION:
      return {
        ...state,
        ownPositionCancelled: true
      };
    case T.OWN_POSITION_RESULT:
      if (action.payload) {
        const newState = {
          ...state,
          ownPosition: {
            lat: action.payload.coords.latitude,
            lng: action.payload.coords.longitude
          }
        };
        if (!state.ownPositionCancelled) {
          return {
            ...newState,
            center: newState.ownPosition
          };
        } else {
          return newState;
        }
      } else {
        return state;
      }
      break;
    default:
      return state;
  }
};

@@ -1020,32 +1020,451 @@
 ayload%0A      %7D;%0A
+    case T.UPDATE_URL:%0A      const %7B params %7D = action.payload;%0A      const value = params%5B%22map-center%22%5D;%0A      if (value && value.length %3E 2) %7B%0A        let %5Blat, lng%5D = value.split(',');%0A        lat = parseFloat(lat);%0A        lng = parseFloat(lng);%0A        if (!(isNaN(lat) %7C%7C isNaN(lng))) %7B%0A          return %7B%0A            ...state,%0A            center: %7B lat, lng %7D%0A          %7D;%0A        %7D%0A      %7D%0A      return state;%0A%0A
     case T.SET_Z

set map center